Java课程设计的图片浏览器的原代码.doc_第1页
Java课程设计的图片浏览器的原代码.doc_第2页
Java课程设计的图片浏览器的原代码.doc_第3页
Java课程设计的图片浏览器的原代码.doc_第4页
Java课程设计的图片浏览器的原代码.doc_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

一.课程设计的任务及要求任务:图形化界面(GUI)编程,编写一个图片浏览器程序1.1 可以单张打开图片1.2 可以将同一目录下的图片按缩略图打开1.3 按“上一张”“下一张”按钮可以显示相应图片二.需求分析图形化界面(GUI)编程,编写一个图片浏览器程序可以支持“.GIF”, “.JPEG”,“.jpeg”,“.TGA”,“.JPG”,“.jpg”等格式,单张打开图片,可以将同一目录下的图片按缩略图打开按“上一张”“下一张”按钮可以显示相应图片。运行Applet时,图像不是一气呵成的,因为方法不是吧图像完整的装入内存再显示的。于此相反,方法创建一个线程,该线程与Applet的原有线程并发执行,一边装入一边显示,从而产生上了不联需显示的现象。为了提高图像才显示效果,可以采用双缓冲技术:首先把图像装入内存,然后再显示在屏幕上。三. 设计思路3.1界面设计选择图片按钮:主要用dir函数实现图片的遍历。上一张,下一张:通过做标轴回调函数实现。由于本软件为单机软件,不需要大量的数据读写和数据交换,实现上、下功能要求只能读取PictureBox控件当前加载的目录,读取当前路径,创建一维数组。frame = new Frame(PictureViewer); Panel pb = new Panel(); Button select = new Button(选择图片); 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); 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 +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= +image_height); 四.详细设计4.1.程序设计流程图开始图片上一张查找盘符图片下一张查找文件夹结束查找文件4.2.源程序代码package C;import java.io.File;import java.io.FilenameFilter;public class MyFilter implements 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 true; 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 image_height; public void setImage(BufferedImage bi) this.bi = bi; this.zoom(); 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(); this.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_height / 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 +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= +image_height); im = bi.getScaledInstance(image_width,image_height,Image.SCALE_SMOOTH); 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 ; private 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); 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(200,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) System.exit(0); ); frame.setVisible(true); this.validateButton(); filter = new MyFilter(); 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) & (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 = ImageIO.read(f); mc.setImage(bi); frame.setTitle(PictureViewer - + f.getName() + ); catch(Exception e) e.printStackTrace(); mc.repaint(); 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.1.将同一目录下的图片按缩略图打开 5.2.单张打开图片5.3.按”上一张”,”下一张”按钮打开图片六. 设计体会与小结我通过这次编程实践学习到了Image和Griphics相关的类的使用。首先我通过网上搜集资料和自己看jdk api对Image、graphics、swing中的类有了实质的操练,对它们的理解有了进一步的理解。不象以前只有模糊的记忆,根本不会运用到实际的情况中,仅仅照着课本超代码。不过由于技

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论