Java实现的图像查看器完整实例
程序员文章站
2024-03-06 09:05:43
本文实例讲述了java实现的图像查看器。分享给大家供大家参考。具体如下:
1. mycanvas.java:
package pictureviewer;
i...
本文实例讲述了java实现的图像查看器。分享给大家供大家参考。具体如下:
1. mycanvas.java:
package pictureviewer; import java.awt.*; import java.awt.event.*; import java.awt.image.*; public class mycanvas extends canvas implements componentlistener{ 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,(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 componenthidden(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_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)(image_height / image_proportion); system.out.println(" p1>p0 w= "+image_width); }else{ image_width = screen_width; image_height = (int)(image_width * image_proportion); system.out.println(" p0>p1 h= "+image_height); } im = bi.getscaledinstance(image_width,image_height,image.scale_smooth); } }
2. myfilter.java:
package pictureviewer; 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", ".png", ".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; } }
3. pictureviewer.java:
package pictureviewer; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.*; import javax.imageio.*; public class pictureviewer 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 myfilter filter; private button previous ; private button next ; public static void main( string args[]) throws exception { new pictureviewer().init(); } public void init(){ frame = new frame("pictureviewer"); panel pb = new panel(); button select = new button("选择图片"); previous = new button("上一张"); next = new button("下一张"); select.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); frame.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 void 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); this.setindex(); } }else if(command.equals("上一张")){ findex--; if(findex<0) findex = 0; this.display(files[findex]); }else if(command.equals("下一张")){ findex++; if(findex >= files.length) findex = files.length-1; this.display(files[findex]); } this.validatebutton(); } public void display(file f){ try{ bufferedimage bi = imageio.read(f); mc.setimage(bi); frame.settitle("pictureviewer - [" + f.getname() + "]"); }catch(exception e){ e.printstacktrace(); } mc.repaint(); } public void setindex(){ file current = new file(fpath + fname); if(files != null){ for(int i=0;i<files.length;i++){ if(current.equals(files[i])){ findex = i; } } } } public void validatebutton(){ previous.setenabled((files!=null) && (findex > 0)); next.setenabled((files!=null) && (findex<(files.length-1))); } }
希望本文所述对大家的java程序设计有所帮助。
上一篇: 使用Java编写图形化的菜单的教程