分享
 
 
 

用java编写一个简单的电子相片查看器

王朝学院·作者佚名  2009-11-25
窄屏简体版  字體: |||超大  

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.UIManager;

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

/**

*

* @author Maoris

*/

public class PowerSee {

public static void main(String args[]) {

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

} catch (Exception e1) {

e1.printStackTrace();

}

MainFrame mainFrame = new MainFrame();

mainFrame.setIconImage(new ImageIcon("icon/powerSee.png").getImage());

mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);

mainFrame.setSize(700, 500);

mainFrame.setVisible(true);

mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

/**

*

* @author Maoris

*/

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.swing.*;

import javax.swing.event.TreeSelectionEvent;

import javax.swing.event.TreeSelectionListener;

import javax.swing.tree.*;

import java.io.*;

import javax.swing.filechooser.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.ItemListener;

import java.awt.event.ItemEvent;

import java.awt.event.MouseAdapter;

import java.util.StringTokenizer;

import java.awt.event.MouseAdapter.*;

import java.awt.event.MouseEvent;

class MainFrame extends JFrame implements TreeSelectionListener {

public JPanel viewPanel = new JPanel(new GridLayout(0, 4, 20, 20));

public JPanel includeviewPanel = new JPanel(new BorderLayout());

public JLabel label = new JLabel("选定:");

public JLabel stateLabel = new JLabel("未选定");

public JPanel statePanel = new JPanel();

FileNameExtensionFilter imageFilter = new FileNameExtensionFilter(null, "jpeg", "jpg", "png", "gif");

JFileChooser imageChooser = new JFileChooser();

public File copyFile;

public File cutFile;

public File directFile;

public File[] imagePath;

public JScrollPane scrollPane;

public static JLabel previewLabel;

public static JPanel previewPanel;

public JPanel toolPanel;

public JPanel treePanel;

public JComboBox style;

public MainFrame() {

super("PowerSee 电子像片管理程序");

viewPanel.setBackground(Color.white);

treePanel = createTreePanel();

previewLabel = new JLabel();

previewPanel = new JPanel(new BorderLayout());

previewLabel.setHorizontalAlignment(JLabel.CENTER);

previewPanel.add(previewLabel, BorderLayout.CENTER);

previewPanel.setBorder(BorderFactory.createTitledBorder("预览"));

JSplitPane vSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, treePanel, previewPanel);

Dimension screen = getToolkit().getScreenSize();

vSplitPane.setOneTouchExpandable(false);

vSplitPane.setDividerLocation((int)(screen.height * 0.52));

includeviewPanel.add(scrollPane = new JScrollPane(viewPanel), BorderLayout.CENTER);

includeviewPanel.add(statePanel, BorderLayout.SOUTH);

statePanel.add(label);

statePanel.add(stateLabel);

JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, vSplitPane, includeviewPanel);

splitPane.setDividerLocation(250);

splitPane.setOneTouchExpandable(true);

getContentPane().add(splitPane, BorderLayout.CENTER);

JMenuBar menuBar = myMenuBar();

setJMenuBar(menuBar);

JToolBar toolBar = myToolBar();

add(toolBar, BorderLayout.NORTH);

}

private JPanel createTreePanel() {

JPanel aTreePanel = new JPanel();

SoureTree tree = new SoureTree();

tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

tree.addTreeSelectionListener(this);

aTreePanel.setLayout(new BorderLayout());

aTreePanel.add(new JScrollPane(tree), BorderLayout.CENTER);

aTreePanel.setBorder(BorderFactory.createTitledBorder("资源管理器"));

return aTreePanel;

}

public void valueChanged(TreeSelectionEvent e) {

previewLabel.setIcon(new ImageIcon());

previewPanel.repaint();

previewPanel.validate();

int imageFileNumber = 0;

SoureTree tree = (SoureTree) e.getSource();

imageChooser.addChoosableFileFilter(imageFilter);

DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

if (!node.isLeaf()) {

directFile = new File(node.getUserObject().toString());

File[] filePath = directFile.listFiles();

imageFileNumber = filePath.length;

for (int i = 0; i < filePath.length; i++) {

if (!imageFilter.accept(filePath[i]) || filePath[i].isDirectory()) {

filePath[i] = null;

--imageFileNumber;

}

}

if (imageFileNumber != 0) {

imagePath = new File[imageFileNumber];

imageFileNumber = 0;

for (int i = 0; i < filePath.length; i++) {

if (filePath[i] != null) {

imagePath[imageFileNumber++] = filePath[i];

}

}

imageFileNumber--;

Runnable loadImage = new LoadImage(viewPanel, scrollPane, imagePath, stateLabel);

Thread thread = new Thread(loadImage);

thread.start();

stateLabel.setText("未选定");

} else {

viewPanel.removeAll();

viewPanel.repaint();

viewPanel.validate();

imagePath = null;

stateLabel.setText("未选定");

}

} else {

directFile = null;

imagePath = null;

}

}

public JMenuBar myMenuBar() {

JMenuBar menuBar = new JMenuBar();

JMenu file = new JMenu("文件(F)", true);

file.setMnemonic('F');

JMenu edit = new JMenu("编辑(E)", true);

edit.setMnemonic('E');

JMenu help = new JMenu("帮助(H)", true);

help.setMnemonic('H');

JMenuItem open = new JMenuItem("打开(O)", 'O');

open.setMnemonic('O');

open.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

openFile();

}

});

JMenuItem close = new JMenuItem("退出(C)", 'C');

close.setMnemonic('C');

close.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

System.exit(0);

}

});

file.add(open);

file.add(close);

JMenuItem cut = new JMenuItem("剪切(T)", 'T');

cut.setMnemonic('T');

cut.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if ((stateLabel.getText()).equals("未选定")) {

JOptionPane.showMessageDialog(null, "未选定文件");

} else {

cut(stateLabel.getText());

}

}

});

JMenuItem copy = new JMenuItem("复制(Y)", 'Y');

copy.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if ((stateLabel.getText()).equals("未选定")) {

JOptionPane.showMessageDialog(null, "未选定文件");

} else {

copy(stateLabel.getText());

}

}

});

copy.setMnemonic('Y');

JMenuItem paste = new JMenuItem("粘贴(P)", 'P');

paste.setMnemonic('P');

paste.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (copyFile == null && cutFile == null) {

JOptionPane.showMessageDialog(null, "未复制文件");

} else if (copyFile != null) {

try {

paste(copyFile, directFile);

} catch (Exception ex) {

Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);

}

} else {

try {

paste(cutFile, directFile);

cutFile.delete();

} catch (Exception ex) {

System.out.println("删除失败晕");

}

cutFile = null;

}

}

});

JMenuItem rename = new JMenuItem("重命名(R)", 'R');

rename.setMnemonic('R');

rename.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if ((stateLabel.getText()).equals("未选定")) {

JOptionPane.showMessageDialog(null, "未选定文件");

} else {

rename();

}

}

});

JMenuItem allrename = new JMenuItem("批量重命名(A)", 'A');

allrename.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

renameAll();

}

});

allrename.setMnemonic('A');

edit.add(cut);

edit.add(copy);

edit.add(paste);

edit.add(rename);

edit.add(allrename);

JMenuItem about = new JMenuItem("关于(A)", 'A');

about.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

JOptionPane.showMessageDialog(null,

"PowerSee 电子像片查看器", "关于",

JOptionPane.INFORMATION_MESSAGE);

}

});

about.setMnemonic('A');

JMenuItem update = new JMenuItem("更新(U)", 'U');

update.setMnemonic('U');

update.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

JOptionPane.showMessageDialog(null, "服务器创建中,期待···",

"在线更新", JOptionPane.INFORMATION_MESSAGE);

}

});

JMenuItem register = new JMenuItem("注册(R)", 'R');

register.setMnemonic('R');

register.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

JOptionPane.showMessageDialog(null,

"此软件暂为免费软件,欢迎使用O(∩_∩)O", "注册",

JOptionPane.INFORMATION_MESSAGE);

}

});

help.add(register);

help.add(update);

help.add(about);

menuBar.add(file);

menuBar.add(edit);

menuBar.add(help);

return menuBar;

}

public void openFile() {

imageChooser.setFileFilter(imageFilter);

int i = imageChooser.showOpenDialog(this);

if (i == JFileChooser.APPROVE_OPTION) {

File selectedFile = imageChooser.getSelectedFile();

if (imageChooser.getSelectedFile() != null) {

String imageFile = selectedFile.toString();

ImageFrame imageFrame = new ImageFrame(imageFile);

}

selectedFile = null;

}

}

public JToolBar myToolBar() {

JToolBar toolBar = new JToolBar();

JButton open = new JButton(new ImageIcon("icon/openIcon.png"));

open.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

openFile();

}

});

JButton cut = new JButton(new ImageIcon("icon/cutIcon.png"));

cut.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if ((stateLabel.getText()).equals("未选定")) {

JOptionPane.showMessageDialog(null, "未选定文件");

} else {

cut(stateLabel.getText());

}

}

});

JButton copy = new JButton(new ImageIcon("icon/copyIcon.png"));

copy.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if ((stateLabel.getText()).equals("未选定")) {

JOptionPane.showMessageDialog(null, "未选定文件");

} else {

copy(stateLabel.getText());

}

}

});

JButton paste = new JButton(new ImageIcon("icon/pasteIcon.png"));

paste.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (copyFile == null && cutFile == null) {

JOptionPane.showMessageDialog(null, "未复制文件");

} else if (copyFile != null) {

try {

paste(copyFile, directFile);

} catch (Exception ex) {

Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);

}

} else {

try {

paste(cutFile, directFile);

cutFile.delete();

} catch (Exception ex) {

System.out.println("Oh no!");

}

cutFile = null;

}

}

});

JButton del = new JButton(new ImageIcon("icon/delIcon.png"));

del.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (stateLabel.getText().equals("未选定")) {

JOptionPane.showMessageDialog(null, "未选定文件");

} else {

delete();

}

}

});

JButton rename = new JButton(new ImageIcon("icon/renameIcon.png"));

rename.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if ((stateLabel.getText()).equals("未选定")) {

JOptionPane.showMessageDialog(null, "未选定文件");

} else {

rename();

}

}

});

toolPanel = new JPanel();

toolPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

JLabel sty = new JLabel("界面风格:");

style = new JComboBox(new Object[]{"白色简洁", "黑色幽默"});

style.setForeground(Color.red);

style.setSelectedItem("经典风格");

style.addItemListener(new ItemListener() {

public void itemStateChanged(ItemEvent e) {

setStyle(style.getSelectedIndex());

}

});

toolPanel.add(sty);

toolPanel.add(style);

toolPanel.setBackground(Color.LIGHT_GRAY);

toolBar.add(open);

toolBar.add(cut);

toolBar.add(copy);

toolBar.add(paste);

toolBar.add(del);

toolBar.add(rename);

toolBar.add(toolPanel);

return toolBar;

}

public void setStyle(int i) {

if (i == 0) {

viewPanel.setBackground(Color.white);

treePanel.setBackground(Color.white);

previewPanel.setBackground(Color.white);

statePanel.setBackground(Color.white);

statePanel.repaint();

statePanel.validate();

viewPanel.repaint();

viewPanel.validate();

treePanel.repaint();

treePanel.validate();

previewPanel.repaint();

previewPanel.validate();

}

if (i == 1) {

viewPanel.setBackground(Color.BLACK);

treePanel.setBackground(Color.lightGray);

previewPanel.setBackground(Color.lightGray);

statePanel.setBackground(Color.lightGray);

statePanel.repaint();

statePanel.validate();

viewPanel.repaint();

viewPanel.validate();

treePanel.repaint();

treePanel.validate();

previewPanel.repaint();

previewPanel.validate();

}

}

public void copy(String filePath) {

copyFile = new File(filePath);

cutFile = null;

}

public void cut(String filePath) {

cutFile = new File(filePath);

copyFile = null;

}

public void paste(File sourceFile, File directFile) throws Exception {

if (directFile != null) {

String name = new String();

name = new String();

StringTokenizer tokenizer = new StringTokenizer(sourceFile.getPath(), "\\");

while (tokenizer.hasMoreTokens()) {

name = tokenizer.nextToken();

}

if ((sourceFile.getParentFile().equals(directFile))) {

JOptionPane.showMessageDialog(null, "当前文件夹已存在该文件", "提示", JOptionPane.INFORMATION_MESSAGE);

return;

}

File newFile = new File(directFile.getPath() + File.separator + name);

FileInputStream fis = new FileInputStream(sourceFile);

FileOutputStream fos = new FileOutputStream(newFile);

byte[] buf = new byte[1024 * 1024];

int i = 0;

while ((i = fis.read(buf)) != -1) {

fos.write(buf, 0, i);

}

fis.close();

fos.close();

Image img = javax.imageio.ImageIO.read(newFile);

ImageIcon imageIcon = new ImageIcon(new DrawImage(img, 200).getImage());

JButton newImageButton = new JButton(imageIcon);

newImageButton.setVerticalTextPosition(JButton.BOTTOM);

newImageButton.setHorizontalTextPosition(JButton.CENTER);

newImageButton.setHorizontalAlignment(JButton.CENTER);

int j = newFile.getPath().lastIndexOf('\\');

String newname = newFile.getPath().substring(j + 1, newFile.getPath().length());

newImageButton.setText(newname);

newImageButton.addMouseListener(new MouseAdapter() );

viewPanel.add(newImageButton);

viewPanel.repaint();

viewPanel.validate();

}

}

public void rename() {

File changeNameFile = new File(stateLabel.getText());

String newName = JOptionPane.showInputDialog(null, "请输入新文件名:", "newName");

if(newName == null) return;

int j = stateLabel.getText().lastIndexOf(".");

String sub = stateLabel.getText().substring(j, stateLabel.getText().length());

for (int i = 0; i < imagePath.length; i++) {

int p = imagePath[i].getPath().lastIndexOf("\\");

String beName = imagePath[i].getPath().substring(p + 1, imagePath[i].getPath().length());

if (beName.equals(newName + sub))

}

File afterChange = new File(changeNameFile.getParent() + File.separator + newName + sub);

changeNameFile.renameTo(afterChange);

stateLabel.setText(afterChange.getPath());

int k = stateLabel.getText().lastIndexOf("\\");

String sub2 = stateLabel.getText().substring(k + 1, stateLabel.getText().length());

LoadImage.choosedImage.setText(sub2);

viewPanel.repaint();

viewPanel.validate();

}

public void renameAll() {

if (imagePath == null) {

JOptionPane.showMessageDialog(null, "没有任何文件可重命名!");

} else {

String newName = JOptionPane.showInputDialog(null, "请输入新文件名:", "重命名");

if (newName != null)

}

}

public void delete() {

int i = JOptionPane.showConfirmDialog(null, "确定删除图片?", "删除图片", JOptionPane.YES_NO_OPTION);

if (i == JOptionPane.YES_OPTION) {

File delFile = new File(stateLabel.getText());

delFile.delete();

previewLabel.setIcon(new ImageIcon());

previewPanel.repaint();

previewPanel.validate();

int t = stateLabel.getText().lastIndexOf('\\');

String delName = stateLabel.getText().substring(t + 1, stateLabel.getText().length());

for (int j = 0; j < LoadImage.allButton.length; j++) {

if (LoadImage.allButton[j].getText().equals(delName)) {

stateLabel.setText("未选定");

copyFile = null;

cutFile = null;

viewPanel.remove(LoadImage.allButton[i]);

viewPanel.repaint();

viewPanel.validate();

}

}

} else {

return;

}

}

}

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

/**

*

* @author Maoris

*/

import javax.swing.*;

import java.io.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.event.MouseAdapter.*;

class LoadImage implements Runnable {

public Image img = null;

public File aPath;

public JPanel viewPanel;

public static File[] imagePath;

public ImageIcon[] smallPicture;

public JButton[] picButton;

public ImageFrame imageFrame;

public static JLabel stateLabel;

public JScrollPane scrollBar;

public static JButton choosedImage;

public static JButton[] allButton;

public LoadImage(JPanel viewPanel,JScrollPane scrollPane, File[] imagePath, final JLabel stateLabel) {

this.viewPanel = viewPanel;

this.scrollBar = scrollPane;

LoadImage.imagePath = imagePath;

LoadImage.stateLabel = stateLabel;

smallPicture = new ImageIcon[imagePath.length];

picButton = new JButton[imagePath.length];

allButton = picButton;

for (int i = 0; i < imagePath.length; i++) {

picButton[i] = new JButton();

picButton[i].addMouseListener(new MouseAdapter() {

@Override

public void mouseClicked(MouseEvent e) {

String picPath = "";

choosedImage = (JButton)e.getSource();

if (e.getClickCount() == 1) {

changeStateLabel((JButton) e.getSource());

MainFrame.previewLabel.setIcon(choosedImage.getIcon());

MainFrame.previewPanel.repaint();

MainFrame.previewPanel.validate();

} else if (e.getClickCount() == 2) {

MainFrame.previewLabel.setIcon(choosedImage.getIcon());

MainFrame.previewPanel.repaint();

MainFrame.previewPanel.validate();

changeStateLabel((JButton) e.getSource());

picPath = stateLabel.getText();

imageFrame = new ImageFrame(picPath);

ImageFrame.myImageFrame = imageFrame;

}

}

});

}

}

public static void changeStateLabel(JButton picButton) {

stateLabel.setText(imagePath[0].getParent() + File.separator + picButton.getText());

}

public void run() {

viewPanel.removeAll();

for (int i = 0; i < imagePath.length; i++) {

try {

img = javax.imageio.ImageIO.read(imagePath[i]);

} catch (IOException ex) {

ex.printStackTrace();

}

smallPicture[i] = new ImageIcon(new DrawImage(img, 200).getImage());

picButton[i].setIcon(smallPicture[i]);

picButton[i].setVerticalTextPosition(JLabel.BOTTOM);

picButton[i].setHorizontalTextPosition(JLabel.CENTER);

picButton[i].setHorizontalAlignment(JLabel.CENTER);

int j = imagePath[i].getPath().lastIndexOf(File.separator);

String sub = imagePath[i].getPath().substring(j + 1, imagePath[i].getPath().length());

picButton[i].setText(sub);

viewPanel.add(picButton[i]);

viewPanel.repaint();

viewPanel.validate();

scrollBar.repaint();

scrollBar.validate();

}

}

}

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

/**

*

* @author Maoris

*/

import java.awt.*;

import java.awt.image.BufferedImage;

public class DrawImage {

private BufferedImage bufferedimage;

private int Scalse;

DrawImage(Image image,int Value) { //按原图画出图像大小

int w = image.getWidth(null);

int h = image.getHeight(null);

float bi = w > h ? (float) h / (float) w : (float) w / (float) h;

if (w > h) {

w = Value;

h = (int) (Value * bi);

} else if (h > w) {

h = Value;

w = (int) (Value * bi);

} else {

w = Value;

h = Value;

}

bufferedimage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

Graphics2D g2D = bufferedimage.createGraphics();

g2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

g2D.drawImage(image, 0, 0, w, h, null);

g2D.dispose();

}

DrawImage(Image image, int Width, int Height, int S) {//画出指定大小的图

Scalse = S;

bufferedimage = new BufferedImage(Width * Scalse, Height * Scalse, BufferedImage.TYPE_INT_ARGB);

Graphics2D g2D = bufferedimage.createGraphics();

g2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

g2D.drawImage(image, 0, 0, Width * Scalse, Height * Scalse, null);

g2D.dispose();

}

public Image getImage() {

return this.bufferedimage;

}

}

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

/**

*

* @author Maoris

*/

import java.awt.*;

import java.awt.event.*;

import java.io.File;

import java.io.IOException;

import javax.swing.*;

import javax.swing.filechooser.FileNameExtensionFilter;

import javax.swing.ImageIcon.*;

public class ImageFrame extends JFrame implements ActionListener{

public JPanel imagePanel;

public JPanel buttonPanel;

public JPanel statePanel;

public JLabel imageLabel;

public JLabel stateLabel;

public JButton before;

public JButton play;

public JButton stop;

public JButton next;

public JButton bigger;

public JButton smaller;

public File picFile;

public Image img;

public ImageIcon imageIcon;

public String fileParent;

FileNameExtensionFilter imageFilter = new FileNameExtensionFilter(null, "jpeg", "jpg", "png", "gif");

JFileChooser imageChooser = new JFileChooser();

public File filePath[];

public File imagePath[];

public int imageFileNumber = 0;

public int locationImage = 0;

public static ImageFrame myImageFrame;

private PlayTimer playTimer;

public Image[] imageOffer;

public int scale = 8;

public JScrollPane imageScrollPane;

public ImageFrame(String picPath) {

picFile = new File(picPath);

fileParent = picFile.getParent();

filePath = (new File(fileParent)).listFiles();

try {

img = javax.imageio.ImageIO.read(picFile);

} catch (IOException ex) {

ex.printStackTrace();

}

imageIcon = new ImageIcon(img);

imageLabel = new JLabel(imageIcon);

imagePanel = new JPanel(new BorderLayout());

imagePanel.setBackground(Color.black);

stateLabel = new JLabel();

setLayout(new BorderLayout());

setTitle("PowerSee 图片查看器");

setIconImage(new ImageIcon("icon/powerSee.png").getImage());

setSize(300, 250);

setVisible(true);

setExtendedState(JFrame.MAXIMIZED_BOTH);

buttonPanel = makeButtonPanel();

statePanel = new JPanel();

stateLabel.setText("像素大小: " + imageIcon.getIconWidth() + "*" + imageIcon.getIconHeight() + " 文件位置: " +

picFile.toString() + " 文件大小: " + picFile.length() / 1024 + "KB");

imagePanel.add(imageLabel, BorderLayout.CENTER);

imageScrollPane = new JScrollPane(imagePanel);

add(imageScrollPane, BorderLayout.CENTER);

add(buttonPanel, BorderLayout.NORTH);

statePanel.add(stateLabel);

add(statePanel, BorderLayout.SOUTH);

imagePanel.repaint();

imagePanel.validate();

load();

}

public JPanel makeButtonPanel() {

JPanel aButtonPanel = new JPanel();

before = new JButton(new ImageIcon("icon" + File.separator + "beforeIcon.png"));

next = new JButton(new ImageIcon("icon/nextIcon.png"));

play = new JButton(new ImageIcon("icon/playIcon.png"));

stop = new JButton(new ImageIcon("icon/stopIcon.png"));

bigger = new JButton(new ImageIcon("icon/biggerIcon.png"));

smaller = new JButton(new ImageIcon("icon/smallerIcon.png"));

stop.setEnabled(false);

before.addActionListener(this);

next.addActionListener(this);

play.addActionListener(this);

stop.addActionListener(this);

bigger.addActionListener(this);

smaller.addActionListener(this);

aButtonPanel.add(before);

aButtonPanel.add(play);

aButtonPanel.add(stop);

aButtonPanel.add(next);

aButtonPanel.add(smaller);

aButtonPanel.add(bigger);

return aButtonPanel;

}

public void load() {

imageChooser.setFileFilter(imageFilter);

for (int i = 0; i < filePath.length; i++) {

if (!filePath[i].isDirectory() && imageFilter.accept(filePath[i])) {

imageFileNumber++;

} else {

filePath[i] = null;

}

}

imagePath = new File[imageFileNumber];

imageFileNumber = 0;

for (int i = 0; i < filePath.length; i++) {

if (filePath[i] != null) {

imagePath[imageFileNumber++] = filePath[i];

}

}

imageFileNumber--;

for (int i = 0; i < imagePath.length; i++) {

if (imagePath[i] == picFile) {

locationImage = i;

}

}

}

public void before() {

scale = 8;

if (--locationImage < 0) {

locationImage = imageFileNumber;

}

try {

img = javax.imageio.ImageIO.read(imagePath[locationImage]);

} catch (IOException ex) {

ex.printStackTrace();

}

imageIcon.setImage(img);

imageLabel.setIcon(imageIcon);

picFile = imagePath[locationImage];

stateLabel.setText("像素大小: " + imageIcon.getIconWidth() + "*" + imageIcon.getIconHeight() + " 文件位置: " +

picFile.toString() + " 文件大小: " + picFile.length() / 1024 + "KB");

imagePanel.repaint();

imagePanel.validate();

imageScrollPane.repaint();

imageScrollPane.validate();

}

public void next() {

scale = 8;

if (++locationImage > imageFileNumber) {

locationImage = 0;

}

try {

img = javax.imageio.ImageIO.read(imagePath[locationImage]);

} catch (IOException ex) {

ex.printStackTrace();

}

imageIcon.setImage(img);

imageLabel.setIcon(imageIcon);

picFile = imagePath[locationImage];

stateLabel.setText("像素大小: " + imageIcon.getIconWidth() + "*" + imageIcon.getIconHeight() + " 文件位置: " +

picFile.toString() + " 文件大小: " + picFile.length() / 1024 + "KB");

imagePanel.repaint();

imagePanel.validate();

imageScrollPane.repaint();

imageScrollPane.validate();

}

public void bigger(){

ImageIcon icon = imageIcon;

if (scale <= 17) {

ImageIcon tmpicon = new ImageIcon(new DrawImage(icon.getImage(), icon.getIconWidth() / 8, icon.getIconHeight() / 8, ++scale).getImage());

imageLabel.setIcon(tmpicon);

} else {

return;

}

}

public void smaller(){

ImageIcon icon = imageIcon;

if (scale > 1) {

ImageIcon tmpicon = new ImageIcon(new DrawImage(icon.getImage(), icon.getIconWidth() / 8, icon.getIconHeight() / 8, --scale).getImage());

imageLabel.setIcon(tmpicon);

} else {

return;

}

}

public void actionPerformed(ActionEvent e) {

if (e.getSource().equals(before)) {

before();

}

if (e.getSource().equals(next)) {

next();

}

if (e.getSource().equals(play)) {

stop.setEnabled(true);

before.setEnabled(false);

next.setEnabled(false);

bigger.setEnabled(false);

smaller.setEnabled(false);

play.setEnabled(false);

playTimer = new PlayTimer(this);

playTimer.start();

}

if (e.getSource().equals(stop)) {

stop.setEnabled(false);

before.setEnabled(true);

next.setEnabled(true);

bigger.setEnabled(true);

smaller.setEnabled(true);

play.setEnabled(true);

playTimer.cancel();

}

if(e.getSource().equals(bigger)){

bigger();

}

if(e.getSource().equals(smaller)){

smaller();

}

}

}

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

/**

*

* @author Maoris

*/

import java.awt.*;

import java.awt.datatransfer.*;

import java.awt.event.*;

import java.awt.image.BufferedImage;

import java.io.*;

import java.util.Iterator;

import java.util.List;

import javax.swing.*;

import javax.swing.event.TreeExpansionEvent;

import javax.swing.event.TreeExpansionListener;

import javax.swing.filechooser.FileSystemView;

import javax.swing.tree.*;

class SoureTree extends JTree {

BufferedImage ghostImage;

private TreePath lastPath;

private Timer hoverTimer;

FileNode sourceNode;

public SoureTree() {

setModel(createTreeModel());

addTreeExpansionListener(new TreeExpansionListener() {

public void treeCollapsed(TreeExpansionEvent e) {

}

public void treeExpanded(TreeExpansionEvent e) {

TreePath path = e.getPath();

if (path != null) {

FileNode node = (FileNode) path.getLastPathComponent();

if (!node.isExplored()) {

DefaultTreeModel model = (DefaultTreeModel) getModel();

node.explore();

model.nodeStructureChanged(node);

}

}

}

});

this.setCellRenderer(new DefaultTreeCellRenderer() {

@Override

public Component getTreeCellRendererComponent(JTree tree,

Object value, boolean selected, boolean expanded,

boolean leaf, int row, boolean hasFocus) {

TreePath tp = tree.getPathForRow(row);

if (tp != null) {

FileNode node = (FileNode) tp.getLastPathComponent();

File f = node.getFile();

try {

Icon icon = FileSystemView.getFileSystemView().getSystemIcon(f);

this.setIcon(icon);

this.setLeafIcon(icon);

this.setOpenIcon(icon);

this.setClosedIcon(icon);

this.setDisabledIcon(icon);

} catch (Exception e) {

e.printStackTrace();

}

}

return super.getTreeCellRendererComponent(tree, value,

selected, expanded, leaf, row, hasFocus);

}

});

super.setScrollsOnExpand(true);

hoverTimer = new Timer(1000, new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (lastPath == null) {

return;

}

if (getRowForPath(lastPath) == 0) {

return;

}

if (isExpanded(lastPath)) {

collapsePath(lastPath);

} else {

expandPath(lastPath);

}

}

});

hoverTimer.setRepeats(false);

this.addKeyListener(new KeyAdapter() {

@Override

public void keyPressed(KeyEvent e) {

int code = e.getKeyCode();

int modifiers = e.getModifiers();

if (code == 'v' || code == 'V') {

System.out.println("find v");

System.out.println("modifiers:" + modifiers + "\t" + ((modifiers & KeyEvent.CTRL_MASK) != 0));

}

if ((modifiers & KeyEvent.CTRL_MASK) != 0 && (code == 'v' || code == 'V')) {

Transferable tr = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);

TreePath path = getSelectionPath();

if (path == null) {

return;

}

FileNode node = (FileNode) path.getLastPathComponent();

if (node.isDirectory()) {

System.out.println("file cp");

try {

List list = (List) (tr.getTransferData(DataFlavor.javaFileListFlavor));

Iterator iterator = list.iterator();

File parent = node.getFile();

while (iterator.hasNext()) {

File f = (File) iterator.next();

}

node.reexplore();

} catch (Exception ioe) {

ioe.printStackTrace();

}

updateUI();

}

}

}

});

}

public String getFilename() {

TreePath path = getLeadSelectionPath();

FileNode node = (FileNode) path.getLastPathComponent();

return ((File) node.getUserObject()).getAbsolutePath();

}

private DefaultTreeModel createTreeModel() {

File root = FileSystemView.getFileSystemView().getRoots()[0];

FileNode rootNode = new FileNode(root);

rootNode.explore();

return new DefaultTreeModel(rootNode);

}

class FileNode extends DefaultMutableTreeNode {

private boolean explored = false;

public FileNode(File file) {

setUserObject(file);

}

@Override

public boolean getAllowsChildren() {

return isDirectory();

}

@Override

public boolean isLeaf() {

return !isDirectory();

}

public File getFile() {

return (File) getUserObject();

}

public boolean isExplored() {

return explored;

}

public boolean isDirectory() {

File file = getFile();

return file.isDirectory();

}

@Override

public String toString() {

File file = (File) getUserObject();

String filename = file.toString();

int index = filename.lastIndexOf(File.separator);

return (index != -1 && index != filename.length() - 1) ? filename.substring(index + 1) : filename;

}

public void explore() {

if (!isDirectory()) {

return;

}

if (!isExplored()) {

File file = getFile();

File[] childrenNode = file.listFiles();

for (int i = 0; i < childrenNode.length; ++i) {

if (childrenNode[i].isDirectory()) {

add(new FileNode(childrenNode[i]));

}

}

explored = true;

}

}

public void reexplore() {

this.removeAllChildren();

explored = false;

explore();

}

}

}

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

/**

*

* @author Maoris

*/

import java.util.Timer;

import java.util.TimerTask;

class PlayTimer {

private int second;

ImageFrame imageFrame;

private final Timer timer = new Timer();

public PlayTimer(ImageFrame imageFrame){

this.imageFrame = imageFrame;

second = 2;

}

public void start(){

timer.schedule(new TimerTask() {

public void run() {

imageFrame.next();

}

},second * 1000 ,second * 1000);

}

public void cancel(){

timer.cancel();

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有