(完整word版)Java课程设计的图片浏览器的讲解
java课程设计 图片查看器.

课程设计报告课程名称面向对象程序设计课程设计专班级、设计题目指导教师设计起止时间:2013 年1月7日至2013年1月18日目录1、课程设计概述 (3)2、系统功能描述 (3)3、系统需求分析与设计 (3)3.1系统需求分析 (3)3.2系统设计 (3)3.2.1 系统总体设计 (3)3.2.2 程序详细设计 (4)4、系统测试和使用说明 (7)4.1系统测试 (7)4.2系统使用说明 (13)5、课程设计总结 (13)5.1遇到的问题和解决方法 (13)5.2心得体会 (13)参考文献 (14)附录 (14)1、课程设计概述鉴于目前当今各种繁杂的图片查看器,我们组根据自身的java知识水平,合力开发了一款相对简单的图片查看器软件。
在深入理解java语言的语法规则及其优势的同时,掌握类与方法的构造方法,掌握基于java的各种操作的实现方法,训练对基础知识和基本方法的综合运用能力,增强对算法的理解能力,提高软件设计能力。
在实践中培养独立分析问题和共同解决问题的作风和能力。
2、系统功能描述本次设计的图片查看器实现了在用户选定的文件目录下循环浏览指定后缀的图片文件,并且可以对选择的图片进行放大,缩小,左旋,右旋,另存,彻底删除等操作。
3、系统需求分析与设计3.1 系统需求分析数字产品的更新日新月异,网络通信技术、图像处理技术以及多媒体技术的发展,使我们的生活变得丰富多彩,让我们的生活变得更便捷,图片的保存和查看已经被越来越多的人所重视,并由此得到了长足的发展。
本图片查看器以其简洁明了易操作的特点实现图片查看的基本功能,从而也就满足了用户的基本要求。
3.2 系统设计3.2.1 系统总体设计(1)系统功能分析本系统要用到以下的Java编程技术实现的功能:图像显示功能:用户需要显示图片,这将由图像显示功能来完成。
显示图片的时候,用到了双缓冲技术来消除闪烁问题。
(2)类图系统中用到了一些基本类、实现了一些接口。
Java图片浏览器的制作与实现

Java图片浏览器的制作与实现作者:蒋鑫陈潇怡来源:《今日湖北·中旬刊》2014年第02期摘要在Java语言的学习中,初学者为了进一步提高的编程能力,窗体的编写实现与实际文件直接的交换运作编写是必学之路。
首先我们需要熟悉awt类和swing类的基本属性和方法,方便我们在编写程序时候合理调用。
熟悉implements接口和类继承调用,具体过程看正文。
关键词 Java 浏览器图片图片浏览器一、对线程的理解同个应用中,多个任务同时进行。
就像QQ聊天应用一样,每打开一个聊天窗口就启动了一个线程。
线程可以有多个,但CPU每时每刻只做一件事。
但是由于cpu处理速度很快,在极短的时间内,CPU在多线程快速跳转处理,我们就感觉是同时进行的。
所以宏观上,线程时并发进行的;从微观角度看,线程是异步执行的。
使用线程的目的是最大限度的利用CPU 资源。
想想QQ聊天的时候,如果没有多线程,一个人的信息没有发完另一个人的信息发不过来,会是什么情况。
在java中要想实现多线程,有两种手段,一种是继续Thread类,另外一种是实现Runable接口。
这里浏览器的整体构造分为上下两个场景,上面是显示图片的主要场景,下面是放置按钮的次要场景。
共有7个主要按钮原件,分别是选择图片,上一张,下一张,顺序播放,循环播放,暂停和另存为。
二、具体实现方法首先要写一个公共类,接口监听事件,写私有定义private JFrame 窗体和private MyCanvas mc 的空白矩形区域,应用程序可以在该区域内绘图,或者可以从该区域捕获用户的输入事件。
再定义字符串private String 文件地址、文件名字;定义文件private File[] files和整型int findex用来表示图片文件第几张的数目。
定义文件窗口FileDialog fd_load,fd_save和MyFilter filter;定义按钮Button select,previous,next,start,restart,stop,save;最后定义时间控制器Timer timer。
Java实现的图片查看器

《Java实现的图片查看器》实验报告目录实验报告..................................................... 错误!未定义书签。
一、实验目的............................................. 错误!未定义书签。
二、实验原理............................................. 错误!未定义书签。
三、实验环境............................................. 错误!未定义书签。
四、指导教师............................................. 错误!未定义书签。
五、小组成员............................................. 错误!未定义书签。
五、功能需求............................................. 错误!未定义书签。
六、实验总结............................................. 错误!未定义书签。
系统方案书................................................... 错误!未定义书签。
一、引言................................................. 错误!未定义书签。
1、编写目的.......................................... 错误!未定义书签。
2、背景.............................................. 错误!未定义书签。
3、参考资料.......................................... 错误!未定义书签。
java方向课程设计报告—瀑布流图片浏览器

目录1. 概述 (2)1.1 设计目标 (2)1.2 开发工具的选择 (2)2 需求分析 (2)2.1 系统需求 (2)2.2 功能需求 (2)2.3 性能需求 (3)2.3.1 硬件环境 (3)2.3.2软件环境 (3)3 系统设计 (3)3.1 设计思想与处理流程 (3)3.2 系统层次模块图 (3)4数据结构设计 (4)4.1. 数据分析 (4)4.2. 数据库设计 (4)4.2.1. 设计原则 (4)4.2.2. 面向对象数据库设计方法 (4)4.2.3 表信息 (4)5 系统模块实现 (5)5.1登录与注册模块 (5)5.2学生信息添加模块 (5)5.3学生信息浏览模块 (6)5.4学生信息修改模块 (7)6 心得体会 (7)7 参考文献 (8)1. 概述1.1 设计目标为方便用户浏览本地图片资源,设计并开发基于android的图片浏览器。
为了寻求更好的使用体验,使用瀑布流的布局,以及异步加载的方式。
系统目标如下:1.1.1实现自动加载本地SD卡的所有图片,并按照文件夹分类1.1.2界面简洁,操作简单1.1.3使用时不占用系统太大内存1.1.4实现瀑布流的图片布局1.1.5点击一张图片时放大图片1.2 开发工具的选择1.2.1 Eclipse + ADT1.2.2 JDK1.2.3 android SDK2 需求分析2.1 系统需求如今android操作系统的手机人手一台,基于android的多媒体应用快速发展,本项目正式在这样的环境下开发,使用更人性化的瀑布流的布局方式展现图片列表,本项目基于浏览图片为目标,不具备图片编辑美化处理2.2 功能需求根据上述的信息管理系统的业务流程、要求以及所要实现的目标,我们可以拟出以下需求目标:(1)图片按文件夹的分类查看(2)图片的异步加载机制,和动态内存回收机制(3)图片的瀑布流布局,更加优雅的显示图片资源2.3 性能需求系统的运行对于运行环境的要求是;2.3.1 硬件环境目前市面上的android操作系统的手机基本满足要求●内存: 200M或以上●硬盘空间:20M或以上●需要插入SD卡2.3.2软件环境●操作系统:Android 4.2.2●开发软件:Eclipse3 系统设计3.1 设计思想与处理流程本系统采用面向对象方法进行分析和设计,使用纯面向对象的java语言作为开发语言,使用Android作为开发平台,能够很好的实现系统的开发及测试。
图片浏览器课程设计报告.docx

图片浏览器设计报告学院土木工程学院班级测绘C111 学号 117544 姓名戴健健成绩一、设计思路1.要达到的目的①培养学生综合利用VB语言进行程序设计的能力,主要是利用VB的标准控件进行设计。
②能够打开常见类型的图片,自动进行缩放,适应PictureBox的大小。
③在FileListBox中只列出图片文件。
④使用StatusBar 控件状态条显示图片文件相关信息,包括图片尺寸(单位:象素)、文件大小和日期等。
⑤使用HscrollBar控件对图片进行缩放。
⑤可以轮流显示FileListBox中列出图片文件。
⑥当图片放大超过窗口大小后,可以提供鼠标移动图片显示。
2.关键问题的解决①使用Loadpicture方法在图片框里加载符合格式要求的图片。
②使用FileListBox方法来显示图片。
③使用PaintPicture方法和改变滚动条的Value属性,使图片能够缩放。
④使用Timer事件更改FileListBox的ListCount属性,使图片能自动浏览。
⑤使用MouseMove事件,在图片过大时,通过鼠标移动使图片显示。
二、模块之间的调用关系,或程序流程图Private Sub Form_Load()Drive1.Drive = App.PathDir1.Path = App.Path'App 是当前的应用程序对象File1.Pattern = "*.bmp;*.jpg;*.ico;*.wmf" '在FileListBox中只列出图片文件。
Picture1.AutoSize = TruePicture2.AutoSize = True'设定自动缩放,适应PictureBox的大小。
'设定StatusBar的数量和属性End SubPrivate Sub Dir1_Change()File1.Path = Dir1.PathEnd subPrivate Sub HScroll1_Change()Picture1.ClsMax = 200Min = 1'设定滚动条的最大、最小值。
Java图片浏览器代码

Java图片浏览器代码如下:import javax.swing.*;import java.io.*;public class PictureFrame extends javax.swing.JFrame {/****/private static final long serialVersionUID = 1L;/** Creates new form PictureFrame */public PictureFrame() {initComponents();}private void initComponents() {jPanel2 = new javax.swing.JPanel();jLabel1 = new javax.swing.JLabel();jButton1 = new javax.swing.JButton();jButton2 = new javax.swing.JButton();jMenuBar1 = new javax.swing.JMenuBar();jMenu1 = new javax.swing.JMenu();jMenuItem1 = new javax.swing.JMenuItem();jMenuItem2 = new javax.swing.JMenuItem();jMenuItem4 = new javax.swing.JMenuItem();jMenu2 = new javax.swing.JMenu();jMenuItem5 = new javax.swing.JMenuItem();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);setTitle("java PictureView Manager");setBackground(new java.awt.Color(255, 255, 255));setBounds(new java.awt.Rectangle(200, 50, 500, 500));setMaximizedBounds(new java.awt.Rectangle(100, 200, 800, 800));setState(1);jPanel2.setEnabled(false);jLabel1.setIcon(newjavax.swing.ImageIcon("C:\\Java\\Wallpaper1349590065739.jpg"));jLabel1.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(255, 0, 0)));jButton1.setText("\u7ee7\u7eed\u6d4f\u89c8");jButton1.addActionListener(new java.awt.event.ActionListener() { @Overridepublic void actionPerformed(java.awt.event.ActionEvent evt) {JButton1Listener(evt);}});jButton2.setText("\u53d6\u6d88\u6d4f\u89c8");jButton2.addMouseListener(new java.awt.event.MouseAdapter() {public void mouseClicked(java.awt.event.MouseEvent evt) {jButton2MouseClicked(evt);}});javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);jPanel2.setLayout(jPanel2Layout);jPanel2Layout.setHorizontalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 417, Short.MAX_VALUE).addGroup(jPanel2Layout.createSequentialGroup().addGap(88, 88, 88).addComponent(jButton1).addGap(60, 60, 60).addComponent(jButton2).addContainerGap(107, Short.MAX_VALUE)));jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel2Layout.createSequentialGroup().addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 559, Short.MAX_VALUE).addPreferredGap(ponentPlacement.REL ATED).addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLa yout.Alignment.BASELINE).addComponent(jButton1).addComponent(jButton2)).addContainerGap()));jMenu1.setText("\u6587\u4ef6(F)");jMenuItem1.setText("\u65b0\u5efa(N)");jMenuItem1.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {MenuItem1Listener(evt);}});jMenu1.add(jMenuItem1);jMenuItem2.setText("\u6253\u5f00(O)");jMenuItem2.addActionListener(new java.awt.event.ActionListener() { @Overridepublic void actionPerformed(java.awt.event.ActionEvent evt) {MenuItem2Listener(evt);}});jMenu1.add(jMenuItem2);jMenuItem4.setText("\u9000\u51fa(E)");jMenuItem4.addActionListener(new java.awt.event.ActionListener() { @Overridepublic void actionPerformed(java.awt.event.ActionEvent evt) {MenuItem4Listener(evt);}});jMenu1.add(jMenuItem4);jMenuBar1.add(jMenu1);jMenu2.setText("\u5e2e\u52a9(H)");jMenuItem5.setText("\u5173\u4e8e(about)");jMenuItem5.addActionListener(new java.awt.event.ActionListener() { @Overridepublic void actionPerformed(java.awt.event.ActionEvent evt) {aboutListener(evt);}});jMenu2.add(jMenuItem5);jMenuBar1.add(jMenu2);setJMenuBar(jMenuBar1);javax.swing.GroupLayout layout = newjavax.swing.GroupLayout(getContentPane());getContentPane().setLayout(layout);layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(15, Short.MAX_VALUE)));layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(42, Short.MAX_VALUE)));pack();}// </editor-fold>private void aboutListener(java.awt.event.ActionEvent evt) {JOptionPane.showMessageDialog(jMenuItem5,"嗖嗖嗖,谢谢大家学习使用","PictureView",JOptionPane.YES_NO_OPTION);}private void MenuItem4Listener(java.awt.event.ActionEvent evt) {// TODO将在此处添加您的处理代码:System.exit(1);}private void MenuItem2Listener(java.awt.event.ActionEvent evt) {// TODO将在此处添加您的处理代码:JFileChooser chooser = new JFileChooser();filename=chooser.getSelectedFile().getName();openPath=chooser.getCurrentDirectory().getPath();System.out.println(openPath);ImageIcon imag=new ImageIcon(openPath+"\\"+filename);jLabel1.setIcon(imag);}private void MenuItem1Listener(java.awt.event.ActionEvent evt) {// TODO将在此处添加您的处理代码:ImageIcon imag=new ImageIcon();jLabel1.setIcon(imag);}private void jButton2MouseClicked(java.awt.event.MouseEvent evt) { System.exit(1);}private void JButton1Listener(java.awt.event.ActionEvent evt) { File file=new File(openPath);String[] str=file.list();str=file.list();ImageIcon imag=new ImageIcon(openPath+"\\"+str[i]);jLabel1.setIcon(imag);i++;}/*** @param args the command line arguments*/public static void main(String args[]) {java.awt.EventQueue.invokeLater(new Runnable() {@Overridepublic void run() {new PictureFrame().setVisible(true);}});}// 变量声明 - 不进行修改private javax.swing.JButton jButton1;private javax.swing.JButton jButton2;private javax.swing.JLabel jLabel1;private javax.swing.JMenu jMenu1;private javax.swing.JMenu jMenu2;private javax.swing.JMenuBar jMenuBar1;private javax.swing.JMenuItem jMenuItem1;private javax.swing.JMenuItem jMenuItem2;private javax.swing.JMenuItem jMenuItem4;private javax.swing.JMenuItem jMenuItem5;private javax.swing.JPanel jPanel2;private String filename;private String openPath;private static int i=0;// 变量声明结束}运行后如下图。
Java课程设计报告原代码图片查看器图片进行放大缩小左旋右旋另存彻底删除

湖南商学院告报设计程课》序设计象《面向对程题目图片查看器姓名: 蒋颖颖070920101 : 号学业: 计算机科学与技术专0703班:级计科志指导教师: 杭师称:讲职计算机与电子工程学院2009年6月课程设计评审表录目................................................................................................ 5 .1、课程设计概述................................................................................................ 5 .2、系统功能描述.................................................................................... 5 .3、系统需求分析与设计........................................................................................... 5 .系统需求分析3.1 (5).系统设计3.2 .............................................................................. 5 .3.2.1 系统总体设计.............................................................................. 6 .3.2.2 程序详细设计.................................................................................... 9 . 4、系统测试和使用说明...................................................................................................9 .4.1系统测试......................................................................................... 11 .系统使用说明4.2 .............................................................................................. 11 .5、课程设计总结......................................................................... 11 .遇到的问题和解决方法5.1 ................................................................................................. 11 .心得体会5.2 . (11)参考文献.................................................................................................................. (12)附录图片查看器1、课程设计概述为提高学生理论与实践相结合的能力,计电学院于2009年6月开展了为期两周的面向对象课程设计,针对对象为07级全体计科专业学生。
java编windows图片浏览器

package viewer;import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.Image;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import java.util.ArrayList;import javax.swing.AbstractAction;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JToolBar;import javax.swing.filechooser.FileFilter;interface Action{void execute(V iewerService service,V iewerFrame frame);}public class V iewer {public static void main(String[] args) {// TODO code application logic hereV iewerFrame f = new V iewerFrame();f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}}class ViewerFrame extends JFrame//主界面类{private int width = 800;private int height = 600;private JLabel label = new JLabel();V iewerService service = new V iewerService();public V iewerFrame(){super();init();}ActionListener menuListener = new ActionListener(){public void actionPerformed(ActionEvent e){service.menuDo(V iewerFrame.this ,e.getActionCommand());}};public void createMenuBar()//创建文件、工具、帮助菜单{JMenuBar menuBar = new JMenuBar();String[] menuArr = { "文件(F)","工具(T)","帮助(H)"};String[][] menuItemArr ={{"打开(O)","-","退出(X)"},{"放大(M)","缩小(O)","-","上一个(X)","下一个(P)"},{"帮助主题","关于"}};for(int i = 0; i < menuArr.length;i++){JMenu menu = new JMenu(menuArr[i]);for(int j = 0;j < menuItemArr[i].length;j++){if(menuItemArr[i][j].equals("-"))menu.addSeparator();else{JMenuItem menuItem = new JMenuItem(menuItemArr[i][j]);menuItem.addActionListener(menuListener);menu.add(menuItem);}}menuBar.add(menu);}this.setJMenuBar(menuBar);}void init()//初始化界面{this.setTitle("看图程序");this.setPreferredSize(new Dimension(width,height));createMenuBar();JPanel toolBar = createToolPanel();this.add(toolBar,BorderLayout.NORTH);this.add(new JScrollPane(label),BorderLayout.CENTER);this.setV isible(true);this.pack();}JLabel getLabel()//获取显示图片的JLabel{return bel;}JPanel createToolPanel()//创建放大、缩小、上一张、下一张等工具按钮{JPanel panel = new JPanel();JToolBar toolBar = new JToolBar("工具");toolBar.setFloatable(false);panel.setLayout(new FlowLayout( FlowLayout.LEFT));//org.crazyit.viewer.action.String[] toolarr = {"viewer.OpenAction","stAction","viewer.NextAction","viewer.BigAction","viewer.SmallAction"};for(int i =0; i < toolarr.length;i++){V iewerAction action = new V iewerAction(new ImageIcon("img/"+toolarr[i]+".gif"),toolarr[i],this);JButton button = new JButton(action);toolBar.add(button);}panel.add(toolBar);return panel;}}class ViewerFileChooser extends JFileChooser{private void addFilter()this.addChoosableFileFilter(new MyFileFilter(new String[]{".BMP"},"BMP (*.BMP)"));this.addChoosableFileFilter(new MyFileFilter(new String[]{".JPG",".JPEG",".JPE",".JFIF"},"JPEG (*.JPG;*.JPEG;*.JPE;*.JFIF)"));this.addChoosableFileFilter(new MyFileFilter(new String[]{".GIF"},"GIF (*.GIF)"));this.addChoosableFileFilter(new MyFileFilter(new String[]{".TIF",".TIFF"},"TIFF (*.TIF;*.TIFF)"));this.addChoosableFileFilter(new MyFileFilter(new String[]{".PNG"},"PNG (*.PNG)"));this.addChoosableFileFilter(new MyFileFilter(new String[]{".ICO"},"ICO (*.ICO)"));this.addChoosableFileFilter(new MyFileFilter(new String[]{".BMP",".JPG",".JPEG",".JPE",".JFIF",".GIF","TIF",".TIFF",".PNG",".ICO"},"所有图形文件"));}public V iewerFileChooser(){super();setAcceptAllFileFilterUsed(false);addFilter();}class MyFileFilter extends FileFilter{String[] suffarr;String description;public MyFileFilter(String[] suffarr,String description){super();this.suffarr = suffarr;this.description = description;}public boolean accept(File file){for(String s:suffarr){if(file.getName().toUpperCase().endsWith(s))return true;}return file.isDirectory();}@Overridepublic String getDescription() {return this.description;}}class ViewerService //业务处理类{private static V iewerService service = null;private V iewerFileChooser fileChooser = new ViewerFileChooser();private File currentFile = null;private File currentDirectory = null;private ArrayList<File> currentFiles = null;private double range = 0.1;private boolean isIcon = true;private ImageIcon icon = null;private double enLargeRange = 1;static V iewerService getInstance(){if (service == null) {service = new V iewerService();}return service;}void open(V iewerFrame frame){if(fileChooser.showOpenDialog(frame) == V iewerFileChooser.APPROVE_OPTION)//给打开的文件赋值{this.currentFile = fileChooser.getSelectedFile();String name = this.currentFile.getPath();File cd = fileChooser.getCurrentDirectory();if(cd != this.currentDirectory || this.currentDirectory == null){FileFilter[] fileFilters = fileChooser.getChoosableFileFilters();File files[] = cd.listFiles();this.currentFiles = new ArrayList<File>();for(File file: files){if(!file.isHidden())for(FileFilter filter : fileFilters){if(filter.accept(file)){this.currentFiles.add(file);break;}}}ImageIcon icon = new ImageIcon(name);frame.getLabel().setIcon(icon);}}void zoom(V iewerFrame frame,boolean isEnlarge){if(enLargeRange > 0.2)enLargeRange = isEnlarge ? enLargeRange + range :enLargeRange -range;elseif(isEnlarge)enLargeRange = enLargeRange + range ;if(isIcon||icon == null){icon = (ImageIcon)frame.getLabel().getIcon();isIcon = false;}if(icon != null){int width = (int)(icon.getIconWidth() * enLargeRange);int height = (int)(icon.getIconHeight() * enLargeRange);ImageIcon newIcon = new ImageIcon(icon.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT));frame.getLabel().setIcon(newIcon);}}void last(ViewerFrame frame){if(this.currentFiles != null && !this.currentFiles.isEmpty()){isIcon = true;File file;ImageIcon icon ;int index = this.currentFiles.indexOf(this.currentFile);if(index > 0){file = (File)this.currentFiles.get(index-1);icon = new ImageIcon(file.getPath());frame.getLabel().setIcon(icon);this.currentFile = file;}elseif(index == 0)file = (File)this.currentFiles.get(currentFiles.size()-1);icon = new ImageIcon(file.getPath());frame.getLabel().setIcon(icon);this.currentFile = file;}}}void next(V iewerFrame frame){if(this.currentFiles != null && !this.currentFiles.isEmpty()){isIcon = true;File file;ImageIcon icon ;int index = this.currentFiles.indexOf(this.currentFile);if(index < currentFiles.size()-1){file = (File)this.currentFiles.get(index+1);icon = new ImageIcon(file.getPath());frame.getLabel().setIcon(icon);this.currentFile = file;}elseif(index == currentFiles.size()-1){file = (File)this.currentFiles.get(0);icon = new ImageIcon(file.getPath());frame.getLabel().setIcon(icon);this.currentFile = file;}}}void menuDo(V iewerFrame frame,String cmd){if(cmd.equals("打开(O)"))open(frame);if(cmd.equals("放大(M)"))zoom(frame,true);if(cmd.equals("缩小(O)"))zoom(frame,false);if(cmd.equals("上一个(X)"))last(frame);if(cmd.equals("退出(X)"))System.exit(0);}}class ViewerAction extends AbstractAction{private Action action = null;private V iewerFrame frame = null;private String actionName= "";public V iewerAction(ImageIcon icon,String actionName,ViewerFrame frame){super("",icon);this.frame = frame;this.actionName = actionName;}private Action getAction(String actionName){try{if(this.action == null){Action action = (Action)Class.forName(actionName).newInstance();this.action = action;}return this.action;}catch(Exception e){return null;}}public void actionPerformed(ActionEvent ae){V iewerService service = V iewerService.getInstance();Action action = getAction(this.actionName);action.execute(service, frame);}}class OpenAction implements Action{public void execute(ViewerService service, V iewerFrame frame){service.open(frame);}}class NextAction implements Action{@Overridepublic void execute(ViewerService service, V iewerFrame frame){service.next(frame);}}class LastAction implements Action{public void execute(ViewerService service, V iewerFrame frame){st(frame);}}class BigAction implements Action{@Overridepublic void execute(ViewerService service, V iewerFrame frame){service.zoom(frame, true);}}class SmallAction implements Action{@Overridepublic void execute(ViewerService service, V iewerFrame frame) {service.zoom(frame, false);}}各图片命名按顺序如下:del viewer.BigAction stAction viewer.NextAction viewer.OpenAction viewer.SmallActionPS:代码参考自疯狂java。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
一.课程设计的任务及要求二.需求分析图形化界面(GUI)编程,编写一个图片浏览器程序可以支持““.GIF”,“.JPEG”,“.jpeg”,“.TGA”,“.JPG”,“.jpg”等格式,单张打开图片,可以将同一目录下的图片按缩略图打开按“上一张”“下一张”按钮可以显示相应图片。
运行Applet时,图像不是一气呵成的,因为方法不是吧图像完整的装入内存再显示的。
于此相反,方法创建一个线程,该线程与Applet的原有线程并发执行,一边装入一边显示,从而产生上了不联需显示的现象。
为了提高图像才显示效果,可以采用双缓冲技术:首先把图像装入内存,然后再显示在屏幕上。
三. 设计思路3.1界面设计选择图片按钮:主要用dir函数实现图片的遍历。
上一张,下一张:通过做标轴回调函数实现。
由于本软件为单机软件,不需要大量的数据读写和数据交换,实现上、下功能要求只能读取PictureBox控件当前加载的目录,读取当前路径,创建一维数组。
frame = new Frame("PictureViewer");Panel pb = new Panel();Button select = new Button("选择图片");previous = new Button("上一张");next = new Button("下一张");select.addActionListener(this);previous.addActionListener(this);3.2.图像加载:Applet常用来显示储存在文件中的图像,多数Applet使用的是GIF或JPEG 格式的图像文件。
需Applet加载图像只需首先定义Image对象,然后使用getImage()方法把图像和文件结合起来即可。
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); }四.详细设计4.1.程序设计流程图4.2.源程序代码package C;import java.io.File;import java.io.FilenameFilter;public 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;}}package C;import java.awt.*;import java.awt.event.*;import java.awt.image.*;public class MyCanvas extends Canvas implements ComponentListener{ /****/private static final long serialVersionUID = 1L;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){System.out.println("resize!!");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;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);}}package C;import java.awt.*;import java.awt.event.*;import java.awt.image.*;import java.io.*;import javax.imageio.*;public class T 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 T().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,"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 actionPerformed(ActionEvent e){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 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))); }}五.运行调试与分析讨论5.1.将同一目录下的图片按缩略图打开5.2.单张打开图片5.3.按”上一张”,”下一张”按钮打开图片六. 设计体会与小结我通过这次编程实践学习到了Image和Griphics相关的类的使用。