Java课程设计报告的图片浏览器的_第1页
Java课程设计报告的图片浏览器的_第2页
Java课程设计报告的图片浏览器的_第3页
Java课程设计报告的图片浏览器的_第4页
Java课程设计报告的图片浏览器的_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

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

文档简介

-.z.一.课程设计的任务及要求二.需求分析图形化界面(GUI)编程,编写一个图片浏览器程序可以支持““.GIF”,“.JPEG”,“.jpeg”,“.TGA”,“.JPG”,“.jpg”等格式,单打开图片,可以将同一目录下的图片按缩略图打开按“上一”“下一”按钮可以显示相应图片。运行Applet时,图像不是一气呵成的,因为方法不是吧图像完整的装入存再显示的。于此相反,方法创建一个线程,该线程与Applet的原有线程并发执行,一边装入一边显示,从而产生上了不联需显示的现象。为了提高图像才显示效果,可以采用双缓冲技术:首先把图像装入存,然后再显示在屏幕上。三.设计思路3.1界面设计选择图片按钮:主要用dir函数实现图片的遍历。上一,下一:通过做标轴回调函数实现。由于本软件为单机软件,不需要大量的数据读写和数据交换,实现上、下功能要求只能读取PictureBo*控件当前加载的目录,读取当前路径,创建一维数组。frame=newFrame("PictureViewer");Panelpb=newPanel();Buttonselect=newButton("选择图片");previous=newButton("上一");ne*t=newButton("下一");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); doubleimage_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>p0w="+image_width); }else{ image_width=screen_width; image_height=(int)(image_width*image_proportion); System.out.println("p0>p1h="+image_height);}四.详细设计4.1.程序设计流程图开始开始图片图片上一*上一*查找盘符查找盘符图片图片下一*查找文件下一*查找文件夹结束查找文件结束查找文件4.2.源程序代码packageC;importjava.io.File;importjava.io.FilenameFilter;publicclassMyFilterimplementsFilenameFilter{privateString[]e*tension;publicMyFilter(){e*tension=newString[]{".jpg",".JPG",".gif",".GIF",".png",".PNG",".jpeg",".JPEG"};}publicMyFilter(String[]e*tension){this.e*tension=e*tension;}publicbooleanaccept(Filedir,Stringname){for(Strings:e*tension){if(name.endsWith(s)){returntrue;}}returnfalse;}}packageC;importjava.awt.*; importjava.awt.event.*; importjava.awt.image.*; publicclassMyCanvase*tendsCanvasimplementsponentListener{ /** * */ privatestaticfinallongserialVersionUID=1L; privateBufferedImagebi; privateImageim; privateintimage_width; privateintimage_height; publicvoidsetImage(BufferedImagebi){ this.bi=bi; this.zoom(); } publicvoidpaint(Graphicsg){ g.drawImage(im,(this.getWidth()-image_width)/2,(this.getHeight()-image_height)/2,this); } publicvoidponentResized(ponentEvente){ if(bi!=null){ System.out.println("resize!!"); this.zoom(); this.repaint(); } } publicvoidponentMoved(ponentEvente){} publicvoidponentShown(ponentEvente){} publicvoidponentHidden(ponentEvente){} publicvoidzoom(){ if(bi==null) return; intscreen_width=this.getWidth(); intscreen_height=this.getHeight(); doublescreen_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); doubleimage_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>p0w="+image_width); }else{ image_width=screen_width; image_height=(int)(image_width*image_proportion); System.out.println("p0>p1h="+image_height); } im=bi.getScaledInstance(image_width,image_height,Image.SCALE_SMOOTH); } }packageC;importjava.awt.*;importjava.awt.event.*;importjava.awt.image.*;importjava.io.*;importjava*.imageio.*;publicclassTimplementsActionListener{privateFrameframe;privateMyCanvasmc;privateStringfpath;privateStringfname;privateFile[]files;privateintfinde*;privateFileDialogfd_load;privateMyFilterfilter;privateButtonprevious;privateButtonne*t;publicstaticvoidmain(Stringargs[])throwsE*ception{newT().init();}publicvoidinit(){frame=newFrame("PictureViewer");Panelpb=newPanel();Buttonselect=newButton("选择图片");previous=newButton("上一");ne*t=newButton("下一");select.addActionListener(this);previous.addActionListener(this);ne*t.addActionListener(this);pb.add(select);pb.add(previous);pb.add(ne*t);mc=newMyCanvas();mc.setBackground(newColor(200,210,230));mc.addponentListener(mc);frame.add(pb,"North");frame.add(mc,"Center");frame.setSize(360,360);frame.setLocation(400,200);frame.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.e*it(0);}});frame.setVisible(true);this.validateButton();filter=newMyFilter();fd_load=newFileDialog(frame,"打开文件",FileDialog.LOAD);fd_load.setFilenameFilter(filter);}publicvoidactionPerformed(ActionEvente){Stringmand=e.getActionmand();if(mand.equals("选择图片")){fd_load.setVisible(true);fpath=fd_load.getDirectory();fname=fd_load.getFile();if((fpath!=null)&&(fname!=null)){this.display(newFile(fpath+fname));files=newFile(fpath).listFiles(filter);this.setInde*();}}elseif(mand.equals("上一")){finde*--;if(finde*<0)finde*=0;this.display(files[finde*]);}elseif(mand.equals("下一")){finde*++;if(finde*>=files.length)finde*=files.length-1;this.display(files[finde*]);}this.validateButton();}publicvoiddisplay(Filef){try{BufferedImagebi=ImageIO.read(f);mc.setImage(bi);frame.setTitle("PictureViewer-["+f.getName()+"]");}catch(E*ceptione){e.printStackTrace();}mc.repaint();}publicvoidsetInde*(){Filecurrent=newFile(fpath+fname);if(files!=null){for(inti=0;i<files.length;i++){if(current.equals(files[i])){finde*=i;}}}}publicvoidvalidateButton(){previous.setEnabled((files!=null)&&(finde*>0));ne*t.setEnabled((files!=null)&&(finde*<(files.length-1)));}}五.运行调试与分析讨论5.1.将同一目录下的图片按缩略图打开5.2.单打开图片5.3.按”上一”,”下一”按钮打开图片六.设计体会与小结我通过这次编程实践学习到了Image和Griphics相关的类的使用。首先我通过网上搜集资料和自己看jdkapi对Image、graphics、swing中的类有了实质

温馨提示

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

评论

0/150

提交评论