您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > Java课程设计的图片浏览器的讲解
1一.课程设计的任务及要求二.需求分析图形化界面(GUI)编程,编写一个图片浏览器程序可以支持““.GIF”,“.JPEG”,“.jpeg”,“.TGA”,“.JPG”,“.jpg”等格式,单张打开图片,可以将同一目录下的图片按缩略图打开按“上一张”“下一张”按钮可以显示相应图片。运行Applet时,图像不是一气呵成的,因为方法不是吧图像完整的装入内存再显示的。于此相反,方法创建一个线程,该线程与Applet的原有线程并发执行,一边装入一边显示,从而产生上了不联需显示的现象。为了提高图像才显示效果,可以采用双缓冲技术:首先把图像装入内存,然后再显示在屏幕上。三.设计思路3.1界面设计选择图片按钮:主要用dir函数实现图片的遍历。上一张,下一张:通过做标轴回调函数实现。由于本软件为单机软件,不需要大量的数据读写和数据交换,实现上、下功能要求只能读取PictureBox控件当前加载的目录,读取当前路径,创建一维数组。frame=newFrame(PictureViewer);Panelpb=newPanel();Buttonselect=newButton(选择图片);previous=newButton(上一张);next=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);2if(image_proportionscreen_proportion){image_height=screen_height;image_width=(int)(image_height/image_proportion);System.out.println(p1p0w=+image_width);}else{image_width=screen_width;image_height=(int)(image_width*image_proportion);System.out.println(p0p1h=+image_height);}四.详细设计4.1.程序设计流程图4.2.源程序代码packageC;importjava.io.File;importjava.io.FilenameFilter;publicclassMyFilterimplementsFilenameFilter{开始上一张结束图片查找文件查找文件夹查找盘符图片下一张3privateString[]extension;publicMyFilter(){extension=newString[]{.jpg,.JPG,.gif,.GIF,.png,.PNG,.jpeg,.JPEG};}publicMyFilter(String[]extension){this.extension=extension;}publicbooleanaccept(Filedir,Stringname){for(Strings:extension){if(name.endsWith(s)){returntrue;}}returnfalse;}}packageC;importjava.awt.*;importjava.awt.event.*;importjava.awt.image.*;publicclassMyCanvasextendsCanvasimplementsComponentListener{/****/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);}4publicvoidcomponentResized(ComponentEvente){if(bi!=null){System.out.println(resize!!);this.zoom();this.repaint();}}publicvoidcomponentMoved(ComponentEvente){}publicvoidcomponentShown(ComponentEvente){}publicvoidcomponentHidden(ComponentEvente){}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_proportionscreen_proportion){image_height=screen_height;image_width=(int)(image_height/image_proportion);System.out.println(p1p0w=+image_width);}else{image_width=screen_width;image_height=(int)(image_width*image_proportion);System.out.println(p0p1h=+image_height);}im=bi.getScaledInstance(image_width,image_height,Image.SCALE_SMOOTH);}}packageC;importjava.awt.*;importjava.awt.event.*;5importjava.awt.image.*;importjava.io.*;importjavax.imageio.*;publicclassTimplementsActionListener{privateFrameframe;privateMyCanvasmc;privateStringfpath;privateStringfname;privateFile[]files;privateintfindex;privateFileDialogfd_load;privateMyFilterfilter;privateButtonprevious;privateButtonnext;publicstaticvoidmain(Stringargs[])throwsException{newT().init();}publicvoidinit(){frame=newFrame(PictureViewer);Panelpb=newPanel();Buttonselect=newButton(选择图片);previous=newButton(上一张);next=newButton(下一张);select.addActionListener(this);previous.addActionListener(this);next.addActionListener(this);pb.add(select);pb.add(previous);pb.add(next);mc=newMyCanvas();mc.setBackground(newColor(200,210,230));mc.addComponentListener(mc);frame.add(pb,North);frame.add(mc,Center);frame.setSize(360,360);frame.setLocation(400,200);frame.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});frame.setVisible(true);this.validateButton();filter=newMyFilter();6fd_load=newFileDialog(frame,打开文件,FileDialog.LOAD);fd_load.setFilenameFilter(filter);}publicvoidactionPerformed(ActionEvente){Stringcommand=e.getActionCommand();if(command.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.setIndex();}}elseif(command.equals(上一张)){findex--;if(findex0)findex=0;this.display(files[findex]);}elseif(command.equals(下一张)){findex++;if(findex=files.length)findex=files.length-1;this.display(files[findex]);}this.validateButton();}publicvoiddisplay(Filef){try{BufferedImagebi=ImageIO.read(f);mc.setImage(bi);frame.setTitle(PictureViewer-[+f.getName()+]);}catch(Exceptione){e.printStackTrace();}mc.repaint();}publicvoidsetIndex(){Filecurrent=newFile(fpath+fname);if(files!=null){for(inti=0;ifiles.length;i++){if(curre
本文标题:Java课程设计的图片浏览器的讲解
链接地址:https://www.777doc.com/doc-5609486 .html