欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2015年10月08日 11:07:01   作者:神仙  
這篇文章主要介紹了Java實現(xiàn)的圖像查看器,以完整實例形式較為詳細的分析了java處理圖片的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了Java實現(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,"打開文件",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程序設(shè)計有所幫助。

相關(guān)文章

  • 用intellij Idea加載eclipse的maven項目全流程(圖文)

    用intellij Idea加載eclipse的maven項目全流程(圖文)

    這篇文章主要介紹了用intellij Idea加載eclipse的maven項目全流程(圖文),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • SpringCloud @FeignClient參數(shù)的用法解析

    SpringCloud @FeignClient參數(shù)的用法解析

    這篇文章主要介紹了SpringCloud @FeignClient參數(shù)的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java單元測試Powermockito和Mockito使用總結(jié)

    Java單元測試Powermockito和Mockito使用總結(jié)

    公司單元測試框架選用了Junit 4.12,Mock框架選用了Mockito和PowerMock,本文主要介紹了Java單元測試Powermockito和Mockito使用總結(jié),感興趣的可以了解一下
    2021-09-09
  • 新版SpringSecurity安全配置說明

    新版SpringSecurity安全配置說明

    這篇文章主要介紹了新版SpringSecurity安全配置說明,在 Spring Security 5.7.0-M2 中,我們棄用了WebSecurityConfigurerAdapter,因為我們鼓勵用戶轉(zhuǎn)向基于組件的安全配置,需要的朋友可以參考下
    2023-07-07
  • java實現(xiàn)簡單的計算器類實例

    java實現(xiàn)簡單的計算器類實例

    這篇文章主要介紹了java實現(xiàn)簡單的計算器類,涉及java針對鍵盤監(jiān)聽及數(shù)字運算的處理技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-10-10
  • 詳解Spring+Hiernate整合

    詳解Spring+Hiernate整合

    這篇文章主要介紹了詳解Spring+Hiernate整合,spring整合hibernate主要介紹以xml方式實現(xiàn),有興趣的可以了解一下。
    2017-04-04
  • IntelliJ IDEA(2019)之mybatis反向生成的實現(xiàn)

    IntelliJ IDEA(2019)之mybatis反向生成的實現(xiàn)

    這篇文章主要介紹了IntelliJ IDEA(2019)之mybatis反向生成,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • 使用mybatis插件PageHelper實現(xiàn)分頁效果

    使用mybatis插件PageHelper實現(xiàn)分頁效果

    這篇文章主要為大家詳細介紹了使用mybatis插件PageHelper實現(xiàn)分頁效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Java實現(xiàn)郵件找回密碼功能

    Java實現(xiàn)郵件找回密碼功能

    這篇文章主要為大家詳細介紹了Java實現(xiàn)郵件找回密碼功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • 多線程Thread,Runnable,Callable實現(xiàn)方式

    多線程Thread,Runnable,Callable實現(xiàn)方式

    這篇文章主要為大家詳細介紹了Java多線程如何實現(xiàn)Thread,Runnable,Callable的方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08

最新評論