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

上传人:tia****nde 文档编号:36844236 上传时间:2018-04-03 格式:DOC 页数:10 大小:377.50KB
返回 下载 相关 举报
Java课程设计的图片浏览器的原代码_第1页
第1页 / 共10页
Java课程设计的图片浏览器的原代码_第2页
第2页 / 共10页
Java课程设计的图片浏览器的原代码_第3页
第3页 / 共10页
Java课程设计的图片浏览器的原代码_第4页
第4页 / 共10页
Java课程设计的图片浏览器的原代码_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《Java课程设计的图片浏览器的原代码》由会员分享,可在线阅读,更多相关《Java课程设计的图片浏览器的原代码(10页珍藏版)》请在金锄头文库上搜索。

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

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

3、 newnew Panel();Button select = newnew Button(“选择图片“);previous = newnew Button(“上一张“);next = newnew Button(“下一张“);select.addActionListener(thisthis);previous.addActionListener(thisthis);3.2.3.2.图像加载图像加载:Applet 常用来显示储存在文件中的图像,多数 Applet 使用的是 GIF 或 JPEG 格式的图像文件。需 Applet 加载图像只需首先定义 Image 对象,然后使 用 getIma

4、ge()方法把图像和文件结合起来即可。2image_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

5、;image_width = (int)(image_height / image_proportion); System.out.println(“ p1p0 w= “+image_width);elseimage_width = screen_width;image_height = (int)(image_width * image_proportion); System.out.println(“ p0p1 h= “+image_height); 四四.详细设计详细设计4.1.4.1.程序设计流程图程序设计流程图开始上一张结束图片查找文件查找文件夹夹查找盘符图片下一张34.2.4.2.

6、源程序代码源程序代码package C; import java.io.File; import java.io.FilenameFilter; public class MyFilter implements FilenameFilterprivate String extension; public MyFilter()extension = new String“.jpg“, “.JPG“, “.gif“, “.GIF“, “.png“, “.PNG“, “.jpeg“, “.JPEG“; public MyFilter(String extension)this.extension =

7、 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 lo

8、ng serialVersionUID = 1L; private BufferedImage bi;private Image im;private int image_width;private int image_height;public void setImage(BufferedImage bi)4this.bi = bi;this.zoom();public void paint(Graphics g)g.drawImage(im,(this.getWidth()-image_width)/2,(this.getHeight()- image_height)/2,this);pu

9、blic 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_wi

10、dth = 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_propor

11、tion = 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);elseim

12、age_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);5 packagepackage C; importimport java.awt.*; importimport java.awt.event.*; importimport java.awt.image.*

13、; importimport java.io.*; importimport javax.imageio.*; publicpublic classclass T implementsimplements ActionListenerprivateprivate Frame frame;privateprivate MyCanvas mc ;privateprivate String fpath;privateprivate String fname;privateprivate File files;privateprivate intint findex ;privateprivate F

14、ileDialog fd_load; privateprivate MyFilter filter;privateprivate Button previous ;privateprivate Button next ;publicpublic staticstatic voidvoid main( String args) throwsthrows Exception newnew T().init();publicpublic voidvoid init()frame = newnew Frame(“PictureViewer“);Panel pb = newnew Panel();But

15、ton select = newnew Button(“选择图片“);previous = newnew Button(“上一张“);next = newnew Button(“下一张“);select.addActionListener(thisthis);previous.addActionListener(thisthis);next.addActionListener(thisthis);pb.add(select);pb.add(previous);pb.add(next); mc = newnew MyCanvas();mc.setBackground(newnew Color(2

16、00,210,230);mc.addComponentListener(mc);frame.add(pb,“North“);frame.add(mc,“Center“);frame.setSize(360,360);frame.setLocation(400,200);frame.addWindowListener(newnew WindowAdapter()6publicpublic voidvoid windowClosing(WindowEvent e)System.exit(0); ); frame.setVisible(truetrue); thisthis.validateButton();filter = newnew MyFilter();fd_load = newnew FileDialog(frame,“打开文件“,FileDialog.LOAD);fd_load.se

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 试题/考题

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号