国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Java > 正文

Java實(shí)現(xiàn)的圖像查看器完整實(shí)例

2019-11-26 14:56:44
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了Java實(shí)現(xiàn)的圖像查看器。分享給大家供大家參考。具體如下:

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,"打開(kāi)文件",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)));   }}

希望本文所述對(duì)大家的java程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 全州县| 绥德县| 手机| 山东省| 黑河市| 崇阳县| 正蓝旗| 萨迦县| 额尔古纳市| 长兴县| 兴安县| 黎城县| 额敏县| 武功县| 察哈| 怀来县| 西峡县| 崇明县| 玛纳斯县| 化隆| 嘉黎县| 汝州市| 武穴市| 渭南市| 察隅县| 建湖县| 蒙阴县| 鲜城| 五原县| 华池县| 罗平县| 永清县| 迁安市| 八宿县| 塔城市| 新兴县| 新河县| 姚安县| 陇川县| 两当县| 柞水县|