图片查看器

介绍

博主因近日无聊,突发奇想想做一个图片查看器,经过博主在网上查询的资料,成功写成了一段这样的小程序,接下来我就一 一介绍

首先是图片管理类

用于遍历文件和判断格式

import java.io.File;
import java.io.FilenameFilter;

public class MyFilter implements FilenameFilter{

	@Override
	public boolean accept(File dir, String name) {
		// TODO Auto-generated method stub
		 for(String s : extension){
	            if(name.endsWith(s)){
	                return true;

	            }
	        }
		return false;
	}
	
	private String[] extension;

    public MyFilter(){
        extension = new String[]{".jpg", ".JPG", ".gif", ".GIF", ".png", ".PNG", ".jpeg", ".JPEG"}; 
    }
    public MyFilter(String[] extension){
        this.extension = extension; 
    }

}

窗体控制类

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);
    }

	
	@Override
	public void componentResized(ComponentEvent e) {
		// TODO Auto-generated method stub
		   if(bi != null){
	            System.out.println("resize!!");
	        this.zoom();
	        this.repaint();
		   }
	}

	@Override
	public void componentMoved(ComponentEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void componentShown(ComponentEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void componentHidden(ComponentEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	
	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);
	}

}

操作类

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();
    }
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		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 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 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))); 
	}


}

介绍语

新人创作不易请多多点赞支持
博主qq 2381499728 问题 18003538625
谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值