Java-幻灯片形式的图片浏览器

直接上图
Java幻灯片形式的图片浏览器

Java幻灯片形式的图片浏览器

Java幻灯片形式的图片浏览器

Java幻灯片形式的图片浏览器

Java幻灯片形式的图片浏览器

代码实现如下

package photo;

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 java.net.URI;
import java.util.*;
import java.util.zip.*;
@SuppressWarnings("unused")
public class pictureWatcher extends JFrame {
	private static final long serialVersionUID = 1L;
	//实现序列化,serialVersionUID 用来表明实现序列化类的不同版本间的兼容性

int t=5;
String pt;
File[] files2;
JLabel[] imageLabel;
JLabel jlabel, jimageLabel;
JPanel jp1, jp2, jp3;
JMenuItem b1 = new JMenuItem("分类一");
JMenuItem b2 = new JMenuItem("分类二");
JMenuItem b3 = new JMenuItem("分类三");
JMenuItem b4 = new JMenuItem("分类四");
private JTree tree;
private JScrollPane jsp1, jsp2, jsp3;
Timer timer,timer1,timer2,timer3;
public pictureWatcher() {
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu open = new JMenu("按类打开");
JMenu menu = new JMenu("文件"); // 文件menu
menuBar.add(menu);
menu.add(open);
open.add(b1);
b1.addActionListener(new JCAction());
open.add(b2);
b2.addActionListener(new JCAction());
open.add(b3);
b3.addActionListener(new JCAction());
open.add(b4);
b4.addActionListener(new JCAction());
JMenu menu1 = new JMenu("分类"); // 分类menu
menuBar.add(menu1);
JMenuItem c1 = new JMenuItem("分类一"); // 函数 4.分类实现
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("数据库");// 数据库按钮 //数据库menu
menuBar.add(menu2);
JMenuItem s1 = new JMenuItem("备份");
menu2.add(s1);
s1.addActionListener(new FileZipListener());
JMenu menu3 = new JMenu("幻灯片播放");// 播放按钮 //幻灯片menu
menuBar.add(menu3);
JMenuItem h1 = new JMenuItem("自动播放");
menu3.add(h1);
h1.addActionListener(new FileStartListener());
JMenuItem h2 = new JMenuItem("手动播放");
menu3.add(h2);
h2.addActionListener(new StartListener());

JMenuItem colourOpen = new JMenuItem("复制");
menu2.add(colourOpen);
colourOpen.addActionListener(new ColourListener());
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
imageLabel = new JLabel[50]; // 图片显示界面创建
for (int i = 0; i < imageLabel.length; i++) {
imageLabel[i] = new JLabel();
jp2.add(imageLabel[i]);
}
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());                 // 函数 1.
tree.addTreeSelectionListener(new TreeSelect());    // “我的电脑”路径检测、函数 2.
setTitle("图片浏览器"); // 基本属性设置
setSize(1210, 610);
setLocation(100, 50);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

class WindowAction implements WindowListener { // 从根节点遍历路径
public 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 TreeSelectionListener { // 绝对路径方式打开文件
public 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; i < imageLabel.length; i++) {
imageLabel[i].setIcon(null);
imageLabel[i].setBorder(null);
}
for (int i = 0; i < files2.length; i++) {
imageLabel[i].setIcon(new ImageIcon(files2[i].toString()));
imageLabel[i].addMouseListener(new MouseAction());
imageLabel[i].setName(String.valueOf(i));
resizeIcon(new ImageIcon(files2[i].toString()), imageLabel[i]);
}
}
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;
else
return 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 ActionListener {            // 分类模块实现
public void actionPerformed(ActionEvent e) {
try {
save(pt, "分类一.txt");
JOptionPane.showMessageDialog(null, "分类为“分类一”完成!");
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
class FileRenwuListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
save(pt, "分类二.txt");
JOptionPane.showMessageDialog(null, "分类为“分类二”完成!");
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
private DefaultMutableTreeNode rootNode;
class FileXiangceListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
save(pt, "分类三.txt");
JOptionPane.showMessageDialog(null, "分类为“分类三”完成!");
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
class FileQitaListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
save(pt, "分类四.txt");
JOptionPane.showMessageDialog(null, "分类为“分类四”完成!");
} catch (Exception e1) {
e1.printStackTrace();
}
}
}

class FileZipListener implements ActionListener {                 // 文件备份(转成压缩文件)
public void actionPerformed(ActionEvent e) {
try {
int a = 0;
File file1 = new File(pt);
File file2 = new File("备份信息.zip");
File f = new File("D:/1/3/5");
f.mkdirs();
if (!file2.exists()) {
file2.createNewFile();
a = 1;
} else
unzip("备份信息.zip", "D:/1/3/5");
FileOutputStream fos = new FileOutputStream(file2);
ZipOutputStream zos = new ZipOutputStream(fos);
byte[] buffer = new byte[1024];
if (a == 0) {
File[] files = f.listFiles();
File[] filess = new File[files.length + 1];
for (int i = 0; i < files.length; i++) {
filess[i] = files[i];
}
filess[filess.length - 1] = file1;
for (int i = 0; i < filess.length; i++) {
String[] st = filess[i].getName().split(".");
for (int j = 0; j < st.length; j++) {
JOptionPane
.showMessageDialog(null, j + "=" + st[j]);
}
ZipEntry entry = new ZipEntry(new Date().getTime()
+ filess[i].getName());
FileInputStream in = new FileInputStream(filess[i]);
zos.putNextEntry(entry);
int len;
while ((len = in.read(buffer)) > 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("F:\1/2/3");
deleteFile(f);
}
}
}

class MouseAction implements MouseListener {                 // 确定点击图片的绝对路径
public void mouseClicked(MouseEvent e) {
for (int i = 0; i < imageLabel.length; i++) {
imageLabel[i].setBorder(null);
}
JLabel jl = (JLabel) e.getSource();
jl.setBorder(new MatteBorder(5, 5, 5, 5, Color.GREEN));
for (int i = 0; i < 100; i++) {
if (i == Integer.parseInt(jl.getName())) {
pt = files2[i].toURI().toString().replace("file:/", "")
.replace("%20", " ");
break;
}
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}

class FileStartListener implements ActionListener {      // 幻灯片自动播放模式实现
public 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();
JButton sure=new JButton("减速");
sure.addActionListener(new Listener());
JButton sure1=new JButton("加速");
sure1.addActionListener(new Listener1());
f.setSize(800, 600);
f.setLocation(300, 50);
f.setVisible(true);
Container c = f.getContentPane();
c.add(new JScrollPane(jimageLabel));
j.add(startBtn);
j.add(stopBtn);
j.add(sure);
j.add(sure1);
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(
files2[index].toString()).getImage()
.getScaledInstance(750, 600, Image.SCALE_SMOOTH)));
index++;
index %= files2.length;
}
});
timer.setCoalesce(false);
timer.setDelay(t* 500);                        // 设置幻灯片播放时间
timer.start();
}
}

class JCAction implements ActionListener { // 显示分类图片事件
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
File[] files = new File[50];
String address = null;
for (int i = 0; i < imageLabel.length; i++) {
imageLabel[i].setIcon(null);
imageLabel[i].setBorder(null);
}
if (source == b1) {
address = "分类一.txt";
}
if (source == b2) {
address = "分类二.txt";
}
if (source == b3) {
address = "分类三.txt";
}
if (source == b4) {
address = "分类四.txt";
}
try {
String[] path = read(address);
for (int i = 0; i < path.length; i++) {
files[i] = new File(path[i]);
}
files2 = files;
for (int i = 0; i < path.length; i++) {
imageLabel[i].setIcon(new ImageIcon(files2[i].toString()));
imageLabel[i].addMouseListener(new MouseAction());
imageLabel[i].setName(String.valueOf(i));
resizeIcon(new ImageIcon(files2[i].toString()),
imageLabel[i]);
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
}

class StartListener implements ActionListener {             // 幻灯片手动播放模式实现
public void actionPerformed(ActionEvent e) {
JFrame f = new JFrame("手动播放");
JPanel j = new JPanel();
final JButton big = new JButton("放大");
final JButton small = new JButton("缩小");
final JButton down = new JButton("下一张");
final int index = 0;

down.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ee) {
if (timer != null) {
timer.start();
}
}
});
big.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ee) {
if (timer1 != null) {
timer1.start();
}
}
});
small.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ee) {
if (timer2 != null) {
timer2.start();
}
}
});
f.setSize(800, 600);
f.setLocation(300, 50);timer.stop();
f.setVisible(true);
Container c = f.getContentPane();
c.add(new JScrollPane(jimageLabel));
j.add(big);
j.add(small);
j.add(down);
c.add(j, "South");
if (files2.length == 0)
return;
timer = null;
timer = new Timer(200, new ActionListener() {
int index=-1,width=750,height=600;
public void actionPerformed(ActionEvent el) {
index++;
index%=files2.length;
jimageLabel.setIcon(new ImageIcon(new
ImageIcon(files2[index].toString()).getImage().getScaledInstance(750,
600, Image.SCALE_SMOOTH)));
timer1=null;
timer1 = new Timer(200, new ActionListener() {
public void actionPerformed(ActionEvent el) {
width*=1.1;height*=1.1;
jimageLabel.setIcon(new ImageIcon(new
ImageIcon(files2[index].toString()).getImage().getScaledInstance(width,
height, Image.SCALE_SMOOTH)));
timer1.stop();
}
});
timer2 = new Timer(200, new ActionListener() {
public void actionPerformed(ActionEvent el) {
width/=1.1;height/=1.1;
jimageLabel.setIcon(new ImageIcon(new
ImageIcon(files2[index].toString()).getImage().getScaledInstance(width,height, Image.SCALE_SMOOTH)));
timer2.stop();
}
});
width=750;height=600;
timer.stop();
}
});
timer.start();
}
}

class Listener extends FileStartListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
t++;
timer.setDelay(t * 500);
}
}


//11.
class Listener1 extends FileStartListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
t--;
if(t==0)
t=1;
timer.setDelay(t * 500);
}
}
//12.
class ColourListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
JFrame f = new JFrame();
JPanel j = new JPanel();
f.setSize(800, 600);
f.setLocation(300, 50);
f.setVisible(true);
Container c = f.getContentPane();
final JButton big = new JButton("粘贴");
j.add(big);
c.add(j, "South");
c.add(new JScrollPane(jimageLabel));
big.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ee) {
jimageLabel.setIcon(new ImageIcon(new
ImageIcon(pt).getImage().getScaledInstance(750,
600, Image.SCALE_SMOOTH)));
}
});
}
}



public String[] read(String address) throws Exception {                     // 读地址操作
int i = 0;
String[] path = new String[80];
BufferedReader reader = new BufferedReader(new FileReader(new File(
address)));
while ((path[i++] = reader.readLine()) != null)
;
reader.close();
String[] p = new String[i - 1];
for (int j = 0; j < p.length; j++) {
p[j] = path[j];
}
return p;
}
public void save(String path, String address) throws Exception { // txt.格式保存文件路径
File file = new File(address);
path = path + "\r\n";
try {
if (!file.exists())
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file, true);
fos.write(path.getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void unzip(String ars, String address) throws IOException {
@SuppressWarnings("resource")
ZipFile zf = new ZipFile(ars);
@SuppressWarnings("rawtypes")
Enumeration ent = zf.entries();
while (ent.hasMoreElements()) {
ZipEntry entry = (ZipEntry) ent.nextElement();
InputStream in = zf.getInputStream(entry);
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(address + "/" + entry.getName()));
byte[] b = new byte[8 * 1024];
int len = 0;
while ((len = in.read(b)) != -1) {
out.write(b, 0, len);
}
in.close();
out.close();
}
}
public void resizeIcon(ImageIcon originalIcon, JLabel jlb) {
ImageIcon icon = originalIcon;
icon = new ImageIcon(originalIcon.getImage().getScaledInstance(150,
120, Image.SCALE_SMOOTH));
jlb.setIcon(icon);
}
public void deleteFile(File file) {
if (file.exists()) {
if (file.isFile()) {
file.delete();
} else if (file.isDirectory()) {
File files[] = file.listFiles();
for (int i = 0; i < files.length; i++) {
this.deleteFile(files[i]);
}}
file.delete();}}
public static void main(String args[]) throws Exception {
pictureWatcher a = new pictureWatcher();}}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GodOuO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值