package Picviewer;
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;
public 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();
}
//===============================================================================
//设置frame的布局,添加组件,
public void init(){
frame = new Frame("PictureViewer");
//设置panel中的组件,每个组件添加监听器
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);
//添加panel和canvs
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();
//FileDialog(Frame parent,String name,int mode)
//创建一个具有指定标题的文件对话框窗口,用于加载或保存文件。
//load常量的作用:查找要读取的文件
//FileDialog(Dialog parent)
//FileDialog(Dialog parent,String name)
//FileDialog(Dialog parent,String name,int mode)
//FileDialog(Frame parent,String name)
//FileDialog(Frame parent)
fd_load = new FileDialog(frame,"打开文件",FileDialog.LOAD);
//此文件对话框窗口的文件名过滤器设置为指定的过滤器
fd_load.setFilenameFilter(filter);
}
@Override
//=========================================================================
//重写actionperformed方法
public void actionPerformed(ActionEvent e){
//command为按钮事件的返回值,返回按钮中的内容
String command = e.getActionCommand();
//判断按钮是否为选图片
if(command.equals("选择图片")){
//如果是,则让文件对话框显示出来
fd_load.setVisible(true);
//String fpath获得此文件对话框的目录
fpath = fd_load.getDirectory();
// 获得此文件对话框的选定文件
fname = fd_load.getFile();
//在些可以把文件对话框想像为一个文件夹,它有路径,及在文件夹内存在子文件
//如果文件对话框存在路径以及子文件存在
if((fpath != null) && (fname != null)){
//
this.display(new File(fpath + fname));
//文件File[] files
//File(String pathname)通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例
//listFile(FilenameFilter filter)返回抽象路径名数组,这些路径名表示此抽象路径名表示的目录中满足指定过滤器的文件和目录。
files = new File(fpath).listFiles(filter);
//将选中的文件的位置作为file[]的下标
this.setIndex();
}
}else if(command.equals("上一张")){
findex--;
//如果不用下面的判断语句,当鼠标按得慢的时候,是不会出现display异常的,所谓异常也就是图片的下标出现负数
//而正常情况下是不会出现负数的,因为当findex为0的时候还要运行velidateButton此时“上一张”变得不可用,所以不会出现负数的情况
//但如果你按得太快,就会造成多线程,几个子程序同时执行,从而velidateButton得不到执行,就会出现异常
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 子类描述具有可访问图像数据缓冲区的 Image。BufferedImage 由图像数据的 ColorModel 和 Raster 组成。
//Raster 的 SampleModel 中 band 的数量和类型必须与 ColorModel 所要求的数量和类型相匹配,
//以表示其颜色和 alpha 分量。
//所有 BufferedImage 对象的左上角坐标都为 (0, 0)。因此,用来构造 BufferedImage 的任何 Raster 都必须满足:minX=0 且 minY=0。
BufferedImage bi = ImageIO.read(f);
//将图像bi放在canvs中
mc.setImage(bi);
//设置frame的标题
frame.setTitle("PictureViewer - [" + f.getName() + "]");
}catch(Exception e){
e.printStackTrace();
}
mc.repaint();
}
//============================================================================
public void setIndex(){
//current是当前在文件对话框中选定的文件
File current = new File(fpath + fname);
if(files != null){
//files.length文件目录中存在的文件个数
//将当前文件的位置作为文件下标
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;
//设置canvs中的图像
public void setImage(BufferedImage bi){
//将类变量中的bi设为从文件路径中的文件
this.bi = bi;
//??
this.zoom();
}
//重写paint方法,可以把graphics设想为画笔
//这里有些疑问,如果canvas被创建时就自动调用paint方法,那为什么当image_width,image_height没有有赋初值但可以运算
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){
//当窗口大小改变时调用zoom方法重新设置图片的大小
this.zoom();
//经验证,当窗口改变大小的,图片会被重画
//repaint方法重新调用paint方法
//this.repaint();
}
}
//组件位置更改时调用
public void componentMoved(ComponentEvent e){}
//组件变得可见时调用
public void componentShown(ComponentEvent e){}
//组件变得不可见时调用
public void componentHidden(ComponentEvent e){}
public void zoom(){
//如果没有图片刚返回一个空值,也是就canvas背景
if(bi == null)
return;
//取得当前窗口的大小,并赋值给screen_width
//和screen_height
int screen_width = this.getWidth();
int screen_height = this.getHeight();
//窗口的比例
double screen_proportion = 1.0 * screen_height / screen_width;
//将图片的初始宽高赋给image_width和image_height
image_width = bi.getWidth(this);
image_height = bi.getHeight(this);
//image_proportion是图片的初始比例,也是最佳比例
double image_proportion = 1.0 * image_height / image_width;
//比较图片与窗口的比例,如果图片的初始比例大于当前窗口的比例,则说明窗口的screen_width相对高screen_height增大了
//为了保持图片能够在窗口中全部都能看到,所以要改变图片的大小,但比例不能变,所以将canvas的高给image的高,用比例算出image的宽
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
//创建此图像的缩放版本。返回一个新的 Image 对象,默认情况下,该对象按指定的 width 和 height 呈现图像
//hints - 指示用于图像重新取样的算法类型的标志
//SCALE_SMOOTH选择图像平滑度比缩放速度具有更高优先级的图像缩放算法
//scale是比例的意思,smooth是光滑的意思
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",".png", ".GIF", ".PNG", ".jpeg", ".JPEG"};
}
public MyFilter(String[] extension){
this.extension = extension;
}
//accept方法是固有的,是抽像类的方法
//测试指定文件是否应该包含在某一文件列表中。
//dir - 被找到的文件所在的目录。
//name - 文件的名称。
public boolean accept(File dir,String name){
for(String s : extension){
if(name.endsWith(s)){
return true;
}
}
return false;
}
}