Fork me on GitHub

Java开发图片浏览器--记录

效果

  

  

设计思路

需求分析

  图片浏览,上/下一张,放大缩小等基本功能。可以继续拓展的功能:缩略图、旋转,画笔修改等。此外,缩放实现较为简单,所以会出现失真。设计此类软件功能可参考ACDSee或irfanview等看图软件。

知识点

  1.文件过滤、文件IO:FileFilter

  2.文件对话框:JFileChooser(添加文件过滤功能)

  3.浏览器主界面:JToolBar/JMenuBar/JScrollPane

  4.响应事件:ActionListener/AbstractAction

  5.展示图片:ImageIcon

设计模式

  1.单例:业务处理类并不是无状态的Java对象,而是保存着浏览目录、缩放比例等信息。

  具体代码:

    public static ViewerService getInstance() {
        if (null == service) {
            service = new ViewerService();
        }
        return service;
    }

 

  2.反射:通过反射创建实例,避免if...else if.....else...,增强程序的可拓展性。

  具体代码:

 1     private Action getAction(String actionName) {
 2         try {
 3             if (this.action == null) {
 4                 Action action = (Action) Class.forName("crazyit.action." +
 5                         actionName).newInstance();
 6                 this.action = action;
 7             }
 8             return this.action;
 9         } catch (Exception e) {
10             return null;
11         }
12     }

 

总结

  在实践中深入学习,重要的是在实战过程中仔细研究文档、积累经验,写出性能(算法)优良、设计(模式)合理的程序。

源代码

  https://github.com/zhaoyu1995/CrazyJava/tree/master/ImageViewer

posted @ 2017-02-03 15:55  赵裕(vimerzhao)  阅读(2399)  评论(0编辑  收藏  举报