Java实现的图像查看器完整实例
本文实例讲述了Java实现的图像查看器。分享给大家供大家参考。具体如下:
1.MyCanvas.java:
packagePictureViewer; importjava.awt.*; importjava.awt.event.*; importjava.awt.image.*; publicclassMyCanvasextendsCanvasimplementsComponentListener{ 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); } publicvoidcomponentResized(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_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); } }
2.MyFilter.java:
packagePictureViewer; importjava.io.File; importjava.io.FilenameFilter; publicclassMyFilterimplementsFilenameFilter{ privateString[]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; } }
3.PictureViewer.java:
packagePictureViewer; importjava.awt.*; importjava.awt.event.*; importjava.awt.image.*; importjava.io.*; importjavax.imageio.*; publicclassPictureViewerimplementsActionListener{ privateFrameframe; privateMyCanvasmc; privateStringfpath; privateStringfname; privateFile[]files; privateintfindex; privateFileDialogfd_load; privateMyFilterfilter; privateButtonprevious; privateButtonnext; publicstaticvoidmain(Stringargs[])throwsException{ newPictureViewer().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(); fd_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(findex<0) 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;i<files.length;i++){ if(current.equals(files[i])){ findex=i; } } } } publicvoidvalidateButton(){ previous.setEnabled((files!=null)&&(findex>0)); next.setEnabled((files!=null)&&(findex<(files.length-1))); } }
希望本文所述对大家的java程序设计有所帮助。