`
zhuwei2008
  • 浏览: 81902 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

javaAwt 实现照片浏览(初学者)

    博客分类:
  • java
阅读更多

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FilenameFilter;

import javax.imageio.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) {
  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,BorderLayout.NORTH);
  frame.add(mc,BorderLayout.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);
 }
 @Override
 public void actionPerformed(ActionEvent e) {
  // TODO Auto-generated method stub
  String commond=e.getActionCommand();
  if(commond.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(commond.equals("上一张")){
   findex--;
   if(findex<0)
    findex=0;
   this.display(files[findex]);
  }else if(commond.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))));
 }
}
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){
   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; 
  image_width = bi.getWidth(this);
  image_height = bi.getHeight(this);
  double image_proportion = 1.0 * image_height / image_width;
  if(image_proportion > screen_proportion){
   image_height = screen_height;
   image_width = (int)(image_height / image_proportion); 
  }else{
   image_width = screen_width;
   image_height = (int)(image_width * image_proportion);  
  }
  im = bi.getScaledInstance(image_width,image_height,Image.SCALE_SMOOTH);
 }


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

分享到:
评论
1 楼 yexin218 2010-04-20  
代码要格式化下,用编辑器里面代码工具
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FilenameFilter;

import javax.imageio.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) {
  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,BorderLayout.NORTH);
  frame.add(mc,BorderLayout.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);
 }
 @Override
 public void actionPerformed(ActionEvent e) {
  // TODO Auto-generated method stub
  String commond=e.getActionCommand();
  if(commond.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(commond.equals("上一张")){
   findex--;
   if(findex<0)
    findex=0;
   this.display(files[findex]);
  }else if(commond.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))));
 }
}
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){
   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; 
  image_width = bi.getWidth(this);
  image_height = bi.getHeight(this);
  double image_proportion = 1.0 * image_height / image_width;
  if(image_proportion > screen_proportion){
   image_height = screen_height;
   image_width = (int)(image_height / image_proportion); 
  }else{
   image_width = screen_width;
   image_height = (int)(image_width * image_proportion);  
  }
  im = bi.getScaledInstance(image_width,image_height,Image.SCALE_SMOOTH);
 }

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

相关推荐

Global site tag (gtag.js) - Google Analytics