当前位置:文档之家› (完整word版)Java课程设计的图片浏览器的讲解

(完整word版)Java课程设计的图片浏览器的讲解

一.课程设计的任务及要求二.需求分析图形化界面(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(" p1>p0 w= "+image_width);}else{image_width = screen_width;image_height = (int)(image_width * image_proportion);System.out.println(" p0>p1 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(" p1>p0 w= "+image_width);}else{image_width = screen_width;image_height = (int)(image_width * image_proportion);System.out.println(" p0>p1 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<0)findex = 0;this.display(files[findex]);}else if(command.equals("下一张")){findex++;if(findex >= files.length)findex = files.length-1;this.display(files[findex]);}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<files.length;i++){if(current.equals(files[i])){findex = i;}}}}public void validateButton(){previous.setEnabled((files!=null) && (findex > 0));next.setEnabled((files!=null) && (findex<(files.length-1))); }}五.运行调试与分析讨论5.1.将同一目录下的图片按缩略图打开5.2.单张打开图片5.3.按”上一张”,”下一张”按钮打开图片六. 设计体会与小结我通过这次编程实践学习到了Image和Griphics相关的类的使用。

相关主题