关于仿AcdSee软件的源代码以及解说

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

软件为偶参加系里的一个软件设计大赛而写的, 然而,很遗憾,偶只拿到个第二名。 我发誓以后写软件我一定在界面上下最多的工夫! 请用delphi6.0分别编译四个工程,然后将生成的 .exe或.dll文件 放置同一目录就可以了。 任何人都可以随意复制,传播本软件。但若您修改了 其中的代码,我希望您能够寄给我一份。谢谢! 姜亮 2002.05.30于山东曲阜 jiangliang@163.com 保留所有版权。如果您对本程序有任何看法,欢迎来信交流。 ImageSee 1.0说明文档   --------------------------------------------------------------------------------     1.软件名称:ImageSee 2.运行环境:Windows98及其更高版本。 3.开发环境:Delphi6.0 4.主要功能说明:   (1)以缩略图方式显示某一目录下所有图片文件。   (2)支持显示.txt .html文件并能够以语法高亮方式显示.pas文件(用Html和Xml技术分别实现)。   (3)软件界面任意组合,图片预览大小任意可调。   (4)以幻灯片方式(特技效果)显示某一目录下所有图片文件。   (5)放大、缩小及全屏显示图片。   (6)设置墙纸功能。   (7)打包生成html相册。   (8)图象编辑功能,包括:调整图片的亮度、对比度、饱和度,RGB调整,转换为灰度图及柔化、锐化、浮雕、雕刻、底片、曝光等多种滤镜。   (9)支持滤镜插件编程(目前仅能用Delphi和Borlan C++ builder开发)。   (10)提供打包功能。可以把任意目录下的所有图片文件打包成一个文件(*.ssp),并加密码予以保护。   (11)支持提取.exe和.dll文件中的图标。并且以缩略图方式显示。 5.主要技巧算法:   (1)缩略图    实现原理:动态创建Tbitmap对象,设定大小并画上两个边框使其像一个panel,最后把按比例缩小后的图片bitblt于其上。   (2)语法高亮方式显示.pas文件    实现原理:该功能其实就是编译原理中的词法分析器所要实现的功能。针对不同的token予以不同的字体样式。   (3)打包生成html相册    实现原理:保存缩略图和原图片文件到指定目录,动态生成html文件。   (4)图象编辑功能    实现原理:有关图象处理方面的算法都是借鉴自一些书籍,详见附录。   (5)打包功能    实现原理:采用微软的结构化存储技术实现。   (6)滤镜插件    实现原理:定义滤镜接口函数,动态加载.dll文件。 6.版权说明:    本软件为作者独立开发完成,且未使用任何第三方控件,保留所有版权。 7.附录    参考资料:    姚海根编著,《图象处理》,上海科学技术出版社,2000    张远鹏等著,《计算机图象处理技术基础》,北京大学出版社,1996    李兰友等编著,《Visual Basic 6图象处理开发与实例》,电子工业出版社,2000    Marco Cantu著,王辉等译,《Delphi 4从入门到精通》,电子工业出版社,1999    Eric Harmon著,陈旭等译,《Delphi COM 深入编程》,机械工业出版社,2000    David J.Kruglinski著,潘爱民等译,《Visual C++技术内幕》(第四版),1999    大富翁论坛,www.delphibbs.com    efg计算机实验室,www.efg2.com    community.borland.com  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值