Java课程设计的图片浏览器的原代码.docx

上传人:牧羊曲112 文档编号:3159740 上传时间:2023-03-11 格式:DOCX 页数:10 大小:40.30KB
返回 下载 相关 举报
Java课程设计的图片浏览器的原代码.docx_第1页
第1页 / 共10页
Java课程设计的图片浏览器的原代码.docx_第2页
第2页 / 共10页
Java课程设计的图片浏览器的原代码.docx_第3页
第3页 / 共10页
Java课程设计的图片浏览器的原代码.docx_第4页
第4页 / 共10页
Java课程设计的图片浏览器的原代码.docx_第5页
第5页 / 共10页
亲,该文档总共10页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《Java课程设计的图片浏览器的原代码.docx》由会员分享,可在线阅读,更多相关《Java课程设计的图片浏览器的原代码.docx(10页珍藏版)》请在三一办公上搜索。

1、Java课程设计的图片浏览器的原代码一.课程设计的任务及要求 任务:图形化界面编程,编写一个图片浏览器程序 1.1 可以单张打开图片 1.2 可以将同一目录下的图片按缩略图打开 1.3 按“上一张”“下一张”按钮可以显示相应图片 二.需求分析 图形化界面编程,编写一个图片浏览器程序可以支持“.GIF”, “.JPEG”,“.jpeg”,“.TGA”,“.JPG”,“.jpg”等格式,单张打开图片,可以将同一目录下的图片按缩略图打开按“上一张”“下一张”按钮可以显示相应图片。运行Applet时,图像不是一气呵成的,因为方法不是吧图像完整的装入内存再显示的。于此相反,方法创建一个线程,该线程与Ap

2、plet的原有线程并发执行,一边装入一边显示,从而产生上了不联需显示的现象。为了提高图像才显示效果,可以采用双缓冲技术:首先把图像装入内存,然后再显示在屏幕上。 三. 设计思路 3.1界面设计 选择图片按钮:主要用dir函数实现图片的遍历。 上一张,下一张:通过做标轴回调函数实现。 由于本软件为单机软件,不需要大量的数据读写和数据交换,实现上、下功能要求只能读取PictureBox控件当前加载的目录,读取当前路径,创建一维数组。 frame = new Frame(PictureViewer); Panel pb = new Panel; Button select = new Button(

3、选择图片); previous = new Button(上一张); next = new Button(下一张); select.addActionListener(this); previous.addActionListener(this); 3.2.图像加载: Applet常用来显示储存在文件中的图像,多数Applet使用的是GIF或JPEG格式的图像文件。需Applet加载图像只需首先定义Image对象,然后使用getImage方法把图像和文件结合起来即可。 image_width = bi.getWidth(this); 1 image_height = bi.getHeight(

4、this); double image_proportion = 1.0 * image_height / image_width; System.out.println(image: w +image_width+ ,h +image_height+ ,p1 +image_proportion); if(image_proportion screen_proportion) image_height = screen_height; image_width = (int)(image_height / image_proportion); System.out.println( p1p0 w

5、= +image_width); else image_width = screen_width; image_height = (int)(image_width * image_proportion); System.out.println( p0p1 h= +image_height); 四.详细设计 4.1.程序设计流程图 查找文件 查找文件查找盘符 开始 图片 上一张 图片 下一张 结束 2 4.2.源程序代码 package C; import java.io.File; import java.io.FilenameFilter; public class MyFilter im

6、plements FilenameFilter private String extension; public MyFilter extension = new String.jpg, .JPG, .gif, .GIF, .png, .PNG, .jpeg, .JPEG; public MyFilter(String extension) this.extension = extension; public boolean accept(File dir,String name) for(String s : extension) if(name.endsWith(s) return tru

7、e; return false; package C; import java.awt.*; import java.awt.event.*; import java.awt.image.*; public class MyCanvas extends Canvas implements ComponentListener /* * */ private static final long serialVersionUID = 1L; private BufferedImage bi; private Image im; private int image_width; private int

8、 image_height; public void setImage(BufferedImage bi) this.bi = bi; this.zoom; 3 public void paint(Graphics g) g.drawImage(im,(this.getWidth-image_width)/2,(this.getHeight-image_height)/2,this); public void componentResized(ComponentEvent e) if(bi != null) System.out.println(resize!); this.zoom; thi

9、s.repaint; public void componentMoved(ComponentEvent e) public void componentShown(ComponentEvent e) public void componentHidden(ComponentEvent e) public void zoom if(bi = null) return; int screen_width = this.getWidth; int screen_height = this.getHeight; double screen_proportion = 1.0 * screen_heig

10、ht / screen_width; System.out.println(screen: w +screen_width+ ,h +screen_height+ ,p0 +screen_proportion); image_width = bi.getWidth(this); image_height = bi.getHeight(this); double image_proportion = 1.0 * image_height / image_width; System.out.println(image: w +image_width+ ,h +image_height+ ,p1 +

11、image_proportion); if(image_proportion screen_proportion) image_height = screen_height; image_width = (int)(image_height / image_proportion); System.out.println( p1p0 w= +image_width); else image_width = screen_width; image_height = (int)(image_width * image_proportion); System.out.println( p0p1 h=

12、+image_height); im = bi.getScaledInstance(image_width,image_height,Image.SCALE_SMOOTH); 4 package C; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.*; import javax.imageio.*; public class T implements ActionListener private Frame frame; private MyCanvas mc ; priv

13、ate String fpath; private String fname; private File files; private int findex ; private FileDialog fd_load; private MyFilter filter; private Button previous ; private Button next ; public static void main( String args) throws Exception new T.init; public void init frame = new Frame(PictureViewer);

14、Panel pb = new Panel; Button select = new Button(选择图片); previous = new Button(上一张); next = new Button(下一张); select.addActionListener(this); previous.addActionListener(this); next.addActionListener(this); pb.add(select); pb.add(previous); pb.add(next); mc = new MyCanvas; mc.setBackground(new Color(20

15、0,210,230); mc.addComponentListener(mc); frame.add(pb,North); frame.add(mc,Center); frame.setSize(360,360); frame.setLocation(400,200); frame.addWindowListener(new WindowAdapter public void windowClosing(WindowEvent e) 5 System.exit(0); ); frame.setVisible(true); this.validateButton; filter = new My

16、Filter; fd_load = new FileDialog(frame,打开文件,FileDialog.LOAD); fd_load.setFilenameFilter(filter); public void actionPerformed(ActionEvent e) String command = e.getActionCommand; if(command.equals(选择图片) fd_load.setVisible(true); fpath = fd_load.getDirectory; fname = fd_load.getFile; if(fpath != null)

17、& (fname != null) this.display(new File(fpath + fname); files = new File(fpath).listFiles(filter); this.setIndex; else if(command.equals(上一张) findex-; if(findex= files.length) findex = files.length-1; this.display(filesfindex); this.validateButton; public void display(File f) try BufferedImage bi =

18、ImageIO.read(f); mc.setImage(bi); frame.setTitle(PictureViewer - + f.getName + ); catch(Exception e) e.printStackTrace; mc.repaint; 6 public void setIndex File current = new File(fpath + fname); if(files != null) for(int i=0;i 0); next.setEnabled(files!=null) & (findex(files.length-1); 五.运行调试与分析讨论 5

19、.1.将同一目录下的图片按缩略图打开 7 5.2.单张打开图片 5.3.按”上一张”,”下一张”按钮打开图片 8 六. 设计体会与小结 我通过这次编程实践学习到了Image和Griphics相关的类的使用。 首先我通过网上搜集资料和自己看jdk api对Image、graphics、swing中的类有了实质的操练,对它们的理解有了进一步的理解。不象以前只有模糊的记忆,根本不会运用到实际的情况中,仅仅照着课本超代码。不过由于技术水平限制,写的代码有点乱,类的层次不清晰。类之间的调用不明朗。程序中的代码出现累赘,重复创建实例,浪费系统资源。这是很早的了,现在我修改了里面的一些如:如果图片文件中有不

20、是图片格式的他不会显示,我让它自动跳过去。 还有当图片到达文件末尾,上下页按钮不能用并给出提示。改观了软件的外观,稍微美观了些通过该课程设计,使我们掌握了程序设计的基本思路和方法,并使我们巩固和提高了java编程技术。通过完成具有一定难度的课程设计题目的编写,调试运行工作,使我们进一步掌握了面向对象程序设计的基本方法和编译技巧,巩固所学的理论知识,使理论与实际相结合,从而提高自我分析问题 ,和解决问题的 能力。培养研发和独立分析以及独立设计的能力 。具备初步的独立分析和设计能力;软件开发过程得问题分析,系统设计,程序编码,测试等基本方发和技巧;综合运用所学的理论知识和方法进行独立分析和解决问题的能力。 而且在这次设计中,把死板的课本知识变得生动有趣,激发了学习的积极性, 强化了所学的知识。把课堂上的知识通过自己设计的程序表示出来,加深了对 理论知识的理解。 通过本次课程设计,我们还学到了不少动手实践的经验,对以后的学习,工作都是很有用的。 9 七.设计的主要参考文献 丁振凡.Java语言实用教程北京邮电大学出版社 耿祥义.Java2实用教程清华大学出版社 赵付青.Java面向对象程序设计国防工业出版社. 耿祥义.Java课程设计清华大学出版社 10

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号