图片浏览器(java版).doc_第1页
图片浏览器(java版).doc_第2页
图片浏览器(java版).doc_第3页
图片浏览器(java版).doc_第4页
图片浏览器(java版).doc_第5页
已阅读5页,还剩21页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

图片浏览器(后面附有完整代码)一、需求分析1、首先。因为要显示图片所以要先有一个界面;用JFrame;2、因为要能显示选定文件夹内的图片,所以要有一个文件夹选择器;用JTree;3、显示图片的滚动面板;用JScrollPane;4、因为要对图片进行分类,所以要设计分类模块;5、因为要对图片进行备份,所以要设计备份模块;6、因为要对图片进行幻灯播放,所以要设计幻灯播放模块;二、概要设计本次课程设计模块图、系统流程图:幻灯片播放分类备份文件选择地点JTree文件选择器相册任务其它备份还原暂停播放开始播放继续播放三、运行环境、开发语言操作系统:Windows XP 专业版 32位 SP3 ( DirectX 9.0c )开发工具:MyEclipse开发语言:java四、详细设计1 程序清单String pt;鼠标点击的图片的绝对路径File files2;面板上正在显示的图片文件JLabel imageLabel;图片绑定的JLabel控件组JLabel jlabel,jimageLabel; 图片绑定的JLabel控件JPanel jp1,jp2,jp3;显示不同内容的面板JButton jb;按钮数组private JTree tree;文件夹选择树private JScrollPane jsp1,jsp2,jsp3;滚动面板Timer timer;计时器public PictureDir()类的构造函数WindowAction窗体事件TreeSelect文件夹选择树事件FileDidianListener分类为地点事件FileRenwuListener分类为人物事件FileXiangceListener分类为相册事件FileQitaListener分类为其它事件FileZipListener备份事件FileBackListener还原事件MouseAction鼠标点击事件FileStartListener幻灯片播放事件JCAction显示分类图片事件read(String)读文件函数save(String,String)写文件函数unzip(String,String)解压缩函数resizeIcon(ImageIcon,JLabel)控制图片显示大小的函数deleteFile(File)删除文件函数main(String)主函数2 主要代码整个程序的界面如下图:2.1 选择文件夹功能class TreeSelect implements TreeSelectionListenerpublic void valueChanged(TreeSelectionEvent e) TreePath path=e.getPath();DefaultMutableTreeNode node=(DefaultMutableTreeNode)path.getLastPathComponent();Object userObject=node.getUserObject();if(!(userObject instanceof File)return;File folder=(File)userObject;if(!folder.isDirectory()return;File files1=initImageSets1(folder);for(File file:files1)node.add(new DefaultMutableTreeNode(file);files2=initImageSets2(folder);for(int i=0;iimageLabel.length;i+)imageLabeli.setIcon(null);imageLabeli.setBorder(null);for(int i=0;ifiles2.length;i+)imageLabeli.setIcon(new ImageIcon(files2i.toString();imageLabeli.addMouseListener(new MouseAction();imageLabeli.setName(String.valueOf(i);resizeIcon(new ImageIcon(files2i.toString(),imageLabeli);private File initImageSets1(File pictureDir)return pictureDir.listFiles(new FilenameFilter()public boolean accept(File dir,String name)if(!new File(dir,name).isFile()return true;elsereturn false;);private File initImageSets2(File pictureDir)return pictureDir.listFiles(new FilenameFilter()public boolean accept(File dir,String name)if(!new File(dir,name).isFile()return false;String lowserName=name.toLowerCase();if(lowserName.endsWith(jpg)|lowserName.endsWith(jpeg)|lowserName.endsWith(gif)|lowserName.endsWith(png)|lowserName.endsWith(GIF)|lowserName.endsWith(TIFF)|lowserName.endsWith(ICO)|lowserName.endsWith(BMP)return true;return false;);结果:2.2图片分类功能class FileDidianListener implements ActionListenerpublic void actionPerformed(ActionEvent e) try save(pt,didian.txt);JOptionPane.showMessageDialog(null,分类为“地点”完成!); catch (Exception e1) e1.printStackTrace();class FileRenwuListener implements ActionListenerpublic void actionPerformed(ActionEvent e) try save(pt,renwu.txt);JOptionPane.showMessageDialog(null,分类为“人物”完成!); catch (Exception e1) e1.printStackTrace();private DefaultMutableTreeNode rootNode;class FileXiangceListener implements ActionListenerpublic void actionPerformed(ActionEvent e) try save(pt,xiangce.txt);JOptionPane.showMessageDialog(null,分类为“相册”完成!); catch (Exception e1) e1.printStackTrace();class FileQitaListener implements ActionListenerpublic void actionPerformed(ActionEvent e) try save(pt,qita.txt);JOptionPane.showMessageDialog(null,分类为“其它”完成!); catch (Exception e1) e1.printStackTrace();结果:2.3图片备份和还原功能class FileZipListener implements ActionListenerpublic void actionPerformed(ActionEvent e) try int a=0;File file1=new File(pt);File file2=new File(imagesdata.zip);File f=new File(D:/1/3/5);f.mkdirs();if(!file2.exists()file2.createNewFile();a=1;elseunzip(imagesdata.zip,D:/1/3/5);FileOutputStream fos= new FileOutputStream(file2);ZipOutputStream zos=new ZipOutputStream(fos);byte buffer=new byte1024;if(a=0)File files=f.listFiles();File filess=new Filefiles.length+1;for(int i=0;ifiles.length;i+)filessi=filesi;filessfiless.length-1=file1;for (int i = 0; i filess.length; i+) String st=filessi.getName().split(.);for(int j=0;j 0) zos.write(buffer, 0, len); zos.closeEntry(); in.close();if(a=1)ZipEntry entry=new ZipEntry(new Date().getTime()+file1.getName();FileInputStream fis=new FileInputStream(file1);zos.putNextEntry(entry);int read=0;while(read=fis.read(buffer)!=-1)zos.write(buffer,0,read);zos.closeEntry();fis.close();zos.close();fos.close();JOptionPane.showMessageDialog(null,备份完成!);deleteFile(f); catch (Exception e1) e1.printStackTrace();JOptionPane.showMessageDialog(null,备份失败!);File f=new File(D:/1/2/3);deleteFile(f);class FileBackListener implements ActionListenerpublic void actionPerformed(ActionEvent e) JFileChooser chooser1;chooser1=new JFileChooser();chooser1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);chooser1.setCurrentDirectory(new File(.);int result=chooser1.showOpenDialog(new JFrame();File file=chooser1.getSelectedFile(); if(result=JFileChooser.APPROVE_OPTION); try unzip(imagesdata.zip ,file.toString(); catch (IOException e1) e1.printStackTrace(); 结果:2.4 幻灯片播放功能class FileStartListener implements ActionListenerpublic void actionPerformed(ActionEvent e)JFrame f;JPanel j;JButton startBtn;JButton stopBtn;startBtn=new JButton(继续);startBtn.addActionListener(new ActionListener()public void actionPerformed(ActionEvent ee)if(timer!=null)timer.start(););stopBtn=new JButton(暂停);stopBtn.addActionListener(new ActionListener()public void actionPerformed(ActionEvent ee)if(timer!=null)timer.stop(););f=new JFrame();j=new JPanel();f.setSize(800,700);f.setLocation(300,50);f.setVisible(true);Container c=f.getContentPane();c.add(new JScrollPane(jimageLabel);j.add(startBtn);j.add(stopBtn);c.add(j,South);if(files2.length=0)return;timer=null;timer=new Timer(200,new ActionListener()int index=0;public void actionPerformed(ActionEvent el)jimageLabel.setIcon(new ImageIcon(new ImageIcon(files2index.toString().getImage().getScaledInstance(750,600, Image.SCALE_SMOOTH);index+;index%=files2.length;);timer.setCoalesce(true);timer.setDelay(3*500);timer.start();结果:五、调试与分析1、首先遇到的问题是,如何在同一个面板上显示多张图片?解决办法:将多个用于显示图片的JLabel控件,添加到JScollPanel控件中。2、如何对图片进行分类?解决办法:当鼠标点击某一图片时,记录该图片的物理地址,并保存到特定的文件中。3、如何向压缩文件中添加新的压缩文件?解决办法:先将原有的压缩文件解压到临时文件夹,然后将新文件与解压活的文件一起压缩。六、功能测试1、分类:其它分类功能类同,不再一一显示;2、备份:3、幻灯片播放:七、结论与心得写这个程序时,遇到了很多问题,但是通过查阅资料以及网络搜索,大多都解决了;最后只剩一个问题没有解决,那就是如何想一个压缩文件中添加新的文件;我的方法是先将原有的压缩文件解压到临时文件夹,然后将新文件与解压活的文件一起压缩;但是这样程序会浪费很多系统资源,我感觉这种方法不是太好,但是又找不到更好的方法,希望老师帮我解答。八、参考资料【1】Java 开发实战1200例(第一卷) 李钟尉 陈丹丹 清华大学出版社 2011年1月【2】Java项目开发案例全程实录(第二版) 李钟尉 陈丹丹 清华大学出版社 2011年1月【3】Java程序设计案例教程 王成端 清华大学出版社 2011年1月【4】美Bruce Eckel Prentice Hal PTR著、陈昊鹏 等译 Thinking In Java(第3版)机械工业出版社 2005附录(代码):import javax.swing.*;import javax.swing.Timer;import javax.swing.border.*;import javax.swing.event.*;import javax.swing.tree.*;import java.awt.event.*;import java.awt.*;import java.io.*;import .URI;import java.util.*;import java.util.zip.*;public class PictureDir extends JFrameString pt;File files2;JLabel imageLabel;JLabel jlabel,jimageLabel;JPanel jp1,jp2,jp3;JButton jb;private JTree tree;private JScrollPane jsp1,jsp2,jsp3;Timer timer;public PictureDir()JMenuBar menuBar=new JMenuBar();setJMenuBar(menuBar);JMenu menu=new JMenu(文件);menuBar.add(menu);JMenuItem exitItem=new JMenuItem(退出);menu.add(exitItem);exitItem.addActionListener(new ActionListener()public void actionPerformed(ActionEvent event)System.exit(0););JMenu menu1=new JMenu(分类);menuBar.add(menu1);JMenuItem c1=new JMenuItem(地点);menu1.add(c1);c1.addActionListener(new FileDidianListener();JMenuItem c2=new JMenuItem(人物);menu1.add(c2);c2.addActionListener(new FileRenwuListener();JMenuItem c3=new JMenuItem(相册);menu1.add(c3);c3.addActionListener(new FileXiangceListener();JMenuItem c4=new JMenuItem(其它);menu1.add(c4);c4.addActionListener(new FileQitaListener();JMenu menu2=new JMenu(数据库);menuBar.add(menu2);JMenuItem s1=new JMenuItem(备份);menu2.add(s1);s1.addActionListener(new FileZipListener();JMenuItem s2=new JMenuItem(还原);menu2.add(s2);s2.addActionListener(new FileBackListener();JMenu menu3=new JMenu(幻灯片播放);menuBar.add(menu3);JMenuItem h1=new JMenuItem(开始播放);menu3.add(h1);h1.addActionListener(new FileStartListener();jp1=new JPanel();jp2=new JPanel();jp3=new JPanel();jlabel=new JLabel(属性类别);jp3.add(jlabel);imageLabel=new JLabel50;for(int i=0;iimageLabel.length;i+)imageLabeli=new JLabel();jp2.add(imageLabeli);jb=new JButton4;for(int i=0;i4;i+)jbi=new JButton();jbi.addActionListener(new JCAction();jp3.add(jbi);jb0.setText(地点);jb1.setText(人物);jb2.setText(相册);jb3.setText(其它);jimageLabel=new JLabel();jimageLabel.setHorizontalAlignment(SwingConstants.CENTER);tree=new JTree();jsp1=new JScrollPane();jsp2=new JScrollPane();jsp1.setViewportView(tree);jsp2.getViewport().add(jp2);add(jsp1,West);add(jsp2,Center);add(jp1,North);add(jp3,East);jp2.setLayout(new GridLayout(10,5,20,20);jp3.setLayout(new GridLayout(10,1,20,20);rootNode=new DefaultMutableTreeNode(我的电脑);addWindowListener(new WindowAction();tree.addTreeSelectionListener(new TreeSelect();setTitle(图片浏览器);setSize(1210,610);setLocation(100,50);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setVisible(true);class WindowAction implements WindowListenerpublic void windowOpened(WindowEvent e)File disks=File.listRoots();for(File file:disks)DefaultMutableTreeNode node=new DefaultMutableTreeNode(file);rootNode.add(node);tree.setModel(new DefaultTreeModel(rootNode);tree.expandPath(new TreePath(rootNode);public void windowActivated(WindowEvent e) public void windowClosed(WindowEvent e) public void windowClosing(WindowEvent e) public void windowDeactivated(WindowEvent e) public void windowDeiconified(WindowEvent e) public void windowIconified(WindowEvent e) class TreeSelect implements TreeSelectionListenerpublic void valueChanged(TreeSelectionEvent e) TreePath path=e.getPath();DefaultMutableTreeNode node=(DefaultMutableTreeNode)path.getLastPathComponent();Object userObject=node.getUserObject();if(!(userObject instanceof File)return;File folder=(File)userObject;if(!folder.isDirectory()return;File files1=initImageSets1(folder);for(File file:files1)node.add(new DefaultMutableTreeNode(file);files2=initImageSets2(folder);for(int i=0;iimageLabel.length;i+)imageLabeli.setIcon(null);imageLabeli.setBorder(null);for(int i=0;ifiles2.length;i+)imageLabeli.setIcon(new ImageIcon(files2i.toString();imageLabeli.addMouseListener(new MouseAction();imageLabeli.setName(String.valueOf(i);resizeIcon(new ImageIcon(files2i.toString(),imageLabeli);private File initImageSets1(File pictureDir)return pictureDir.listFiles(new FilenameFilter()public boolean accept(File dir,String name)if(!new File(dir,name).isFile()return true;elsereturn false;);private File initImageSets2(File pictureDir)return pictureDir.listFiles(new FilenameFilter()public boolean accept(File dir,String name)if(!new File(dir,name).isFile()return false;String lowserName=name.toLowerCase();if(lowserName.endsWith(jpg)|lowserName.endsWith(jpeg)|lowserName.endsWith(gif)|lowserName.endsWith(png)|lowserName.endsWith(GIF)|lowserName.endsWith(TIFF)|lowserName.endsWith(ICO)|lowserName.endsWith(BMP)return true;return false;);class FileDidianListener implements ActionListenerpublic void actionPerformed(ActionEvent e) try save(pt,didian.txt);JOptionPane.showMessageDialog(null,分类为“地点”完成!); catch (Exception e1) e1.printStackTrace();class FileRenwuListener implements ActionListenerpublic void actionPerformed(ActionEvent e) try save(pt,renwu.txt);JOptionPane.showMessageDialog(null,分类为“人物”完成!); catch (Exception e1) e1.printStackTrace();private DefaultMutableTreeNode rootNode;class FileXiangceListener implements ActionListenerpublic void actionPerformed(ActionEvent e) try save(pt,xiangce.txt);JOptionPane.showMessageDialog(null,分类为“相册”完成!); catch (Exception e1) e1.printStackTrace();class FileQitaListener implements ActionListenerpublic void actionPerformed(ActionEvent e) try save(pt,qita.txt);JOptionPane.showMessageDialog(null,分类为“其它”完成!); catch (Exception e1) e1.printStackTrace();class FileZipListener implements ActionListenerpublic void actionPerformed(ActionEvent e) try int a=0;File file1=new File(pt);File file2=new File(imagesdata.zip);File f=new File(D:/1/3/5);f.mkdirs();if(!file2.exists()file2.createNewFile();a=1;elseunzip(imagesdata.zip,D:/1/3/5);FileOutputStream fos= new FileOutputStream(file2);ZipOutputStream zos=new ZipOutputStream(fos);byte buffer=new byte1024;if(a=0)File files=f.listFiles();File filess=new Filefiles.length+1;for(int i=0;ifiles.length;i+)filessi=filesi;filessfiless.length-1=file1;for (int i = 0; i filess.length; i+) String st=filessi.getName().split(.);for(int j=0;j 0) zos.write(buffer, 0, len); zos.closeEntry(); in.close();if(a=1)ZipEntry entry=new ZipEntry(new Date().getTime()+file1.getName();FileInputStream fis=new FileInputStream(file1);zos.putNextEntry(entry);int read=0;while(read=fis.read(buffer)!=-1)zos.write(buffer,0,read);zos.closeEntry();fis.close();zos.close();fos.close();JOptionPane.showMessageDialog(null,备份完成!);deleteFile(f); catch (Exception e1) e1.printStackTrace();JOptionPane.showMessageDialog(null,备份失败!);File f=new File(D:/1/2/3);deleteFile(f);class FileBackListener implements ActionListenerpublic void actionPerformed(ActionEvent e) JFileChooser chooser1;chooser1=new JFileChooser();chooser1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);chooser1.setCurrentDirectory(new File(.);int result=chooser1.showOpenDialog(new JFrame();File file=chooser1.getSelectedFile(); if(result=JFileChooser.APPROVE_OPTION); try unzip(imagesdata.zip ,file.toString(); catch (IOException e1) e1.printStackTrace(); class MouseAction implements MouseListenerpublic void mouseClicked(MouseEvent e) for(int i=0;iimageLabel.length;i+)imageLabeli.setBorder(null);JLabel jl=(JLabel)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论