Java实现的图像查看器完整实例

 更新时间:2015年10月08日 11:07:01   作者:神仙  
这篇文章主要介绍了Java实现的图像查看器,以完整实例形式较为详细的分析了java处理图片的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了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 类的加载、连接和初始化

    详解Java 类的加载、连接和初始化

    这篇文章主要介绍了Java 类的加载、连接和初始化的的相关资料,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-06-06
  • java通过HTTP接收json详细实例代码

    java通过HTTP接收json详细实例代码

    Java作为一门广泛使用的编程语言,很多开发人员会用它来进行http请求,获取json数据,这篇文章主要给大家介绍了关于java通过HTTP接收json的相关资料,需要的朋友可以参考下
    2023-11-11
  • Java热门笔试试题整理

    Java热门笔试试题整理

    给大家整理了现在很热门的java程序员面试时候的笔试试题以及答案,希望能帮助到你。
    2017-11-11
  • JDK的具体安装步骤(带图带解释巨详细)

    JDK的具体安装步骤(带图带解释巨详细)

    Java是一种广泛使用的编程语言,许多应用程序和系统都依赖于它,如果您想进行Java编程或运行Java应用程序,首先需要安装Java开发工具包(JDK),这篇文章主要给大家介绍了关于JDK具体安装步骤的相关资料,文中介绍的方法带图带解释巨详细,需要的朋友可以参考下
    2024-05-05
  • Spring为什么要用三级缓存解决循环依赖呢

    Spring为什么要用三级缓存解决循环依赖呢

    本文主要介绍了Spring如何使用三级缓存解决循环依赖问题,本文为了方便说明,先设置两个业务层对象,命名为AService和BService,结合示例给大家介绍的非常详细,感兴趣的朋友一起看看吧
    2025-01-01
  • springboot publish event 事件机制demo分享

    springboot publish event 事件机制demo分享

    这篇文章主要介绍了springboot publish event 事件机制demo,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-10-10
  • Java微信二次开发(三) Java微信各类型消息封装

    Java微信二次开发(三) Java微信各类型消息封装

    这篇文章主要为大家详细介绍了Java微信二次开发第三篇,Java微信各类型消息封装,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04
  • java中pdf转图片的实现方法

    java中pdf转图片的实现方法

    下面小编就为大家带来一篇java中pdf转图片的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • java springmvc乱码解决归纳整理详解

    java springmvc乱码解决归纳整理详解

    本篇文章介绍了java 中spring mvc 解决乱码的问题方法实例,需要的朋友可以参考下
    2017-04-04
  • Tomcat启动分析(我们为什么要配置CATALINA_HOME环境变量)

    Tomcat启动分析(我们为什么要配置CATALINA_HOME环境变量)

    本文主要介绍Tomcat启动分析的知识,这里整理了相关资料及分析原因和如何实现的方法,有兴趣的小伙伴可以参考下
    2016-09-09

最新评论