版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、一.课程设计的任务及要求二.需求分析图形化界面(GUI)编程,编写一个图片浏览器程序可以支持“.GIF”, “.JPEG”,“.jpeg”,“.TGA”,“.JPG”,“.jpg”等格式,单张打开图片,可以将同一目录下的图片按缩略图打开按“上一张”“下一张”按钮可以显示相应图片。运行Applet时,图像不是一气呵成的,因为方法不是吧图像完整的装入内存再显示的。于此相反,方法创建一个线程,该线程与Applet的原有线程并发执行,一边装入一边显示,从而产生上了不联需显示的现象。为了提高图像才显示效果,可以采用双缓冲技术:首先把图像装入内存,然后再显示在屏幕上。三. 设计思路3.1界面设计选择图片按
2、钮:主要用dir函数实现图片的遍历。上一张,下一张:通过做标轴回调函数实现。由于本软件为单机软件,不需要大量的数据读写和数据交换,实现上、下功能要求只能读取PictureBox控件当前加载的目录,读取当前路径,创建一维数组。frame = new Frame(PictureViewer); Panel pb = new Panel(); Button select = new Button(选择图片); previous = new Button(上一张); next = new Button(下一张); select.addActionListener(this); previous.add
3、ActionListener(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_widt
4、h+ ,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); Sys
5、tem.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, .P
6、NG, .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 exten
7、ds 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,
8、(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 compo
9、nentHidden(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
10、_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)(imag
11、e_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.aw
12、t.*;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 MyF
13、ilter 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(下一张); sele
14、ct.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); fr
15、ame.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 vo
16、id 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); t
17、his.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(Except
18、ion 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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年江西省农村信用社联合社客户服务中心座席招聘初试笔试历年典型考题及考点剖析附带答案详解
- 2025年春季安徽淮南市寿县联合村镇银行招聘笔试历年典型考题及考点剖析附带答案详解
- 2026电子厂升职考试题及答案
- 2026电网考试题库及答案
- 设备部5月月度测评测试卷及答案
- 2026电气专业考试题库及答案
- 2026电脑维修考试题及答案
- 衡阳师范学院“共筑保密防线公民人人有责”保密安全知识竞赛测试卷及答案
- 危险化学品出入库登记制度
- 文旅市场执法检查工作实施方案
- 八年级物理上册(人教版2024)-新教材解读培训课件
- 心理咨询服务个人隐私保密协议
- JJF2095-2024压力数据采集仪校准规范
- 《模拟电子技术》课件-加减运算电路
- 教师信息技术应用能力培训课件
- 国家地质公园规划编制技术要求
- GB/T 17469-2024汽车制动器衬片摩擦性能评价小样台架试验方法
- 供应商来料质量报告(年度与月度)
- 巨人通力电梯NOVA GKE调试说明书故障代码GPN15 GVN15-GKE - 51668093D01-2022
- 简约劳务合同范本
- JT-T-776.3-2010公路工程玄武岩纤维及其制品第3部分:玄武岩纤维土工隔栅
评论
0/150
提交评论