📄 imageframe.java
字号:
package cn.dxm.frame;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import cn.dxm.util.ChangeIMG;
import cn.dxm.util.FileNameDeal;
import cn.dxm.util.ImageUtil;
public class ImageFrame implements ActionListener {
public static JFrame frame = new JFrame("自动标签器");
private JPanel btnContainer;
private static JPanel imgContainer;
private JButton narrow_img_btn;
private JButton enlage_img_btn;
private JButton show_jtree_btn;
private JButton cut_img_btn;
private JMenuBar menuBar;
private JMenu file_menu;
private JMenu about_menu;
private JMenuItem openFile_item;
private JMenuItem exit_item;
private JMenuItem about_item;
private JMenuItem help_item;
private Dimension screenSize;
private static Container container;
private FileDialog open_dialog;
private static BufferedImage bimage;
private static BufferedImage srcImage;
private File openFile = null;
private static JLabel image = new JLabel();
/**
* @author dxm frame: the main frame use for container the image that you
* want to deal with; initial the main frame
*
*/
public ImageFrame() {
screenSize = Toolkit.getDefaultToolkit().getScreenSize();// get the
// dimension
// of the
// screen
menuBar = new JMenuBar();
file_menu = new JMenu("文件(F)");
file_menu.setToolTipText("文件");
file_menu.setMnemonic('F');
about_menu = new JMenu("帮助(H)");
about_menu.setToolTipText("帮助");
about_menu.setMnemonic('H');
openFile_item = new JMenuItem("打开(O)");
openFile_item.setToolTipText("打开");
openFile_item.setMnemonic('O');
exit_item = new JMenuItem("退出(E)");
exit_item.setToolTipText("退出");
exit_item.setMnemonic('E');
about_item = new JMenuItem("关于(A)");
about_item.setToolTipText("关于");
about_item.setMnemonic('A');
help_item = new JMenuItem("帮助(H)");
help_item.setToolTipText("帮助");
help_item.setMnemonic('H');
file_menu.add(openFile_item);
file_menu.add(exit_item);
about_menu.add(about_item);
about_menu.add(help_item);
menuBar.add(file_menu);
menuBar.add(about_menu);
frame.setJMenuBar(menuBar);
frame.setLayout(null);
frame.setSize(screenSize);
narrow_img_btn = new JButton("放大(E)");
narrow_img_btn.setToolTipText("放大图片");
narrow_img_btn.setMnemonic('E');
enlage_img_btn = new JButton("缩小(N)");
enlage_img_btn.setToolTipText("缩小图片");
enlage_img_btn.setMnemonic('N');
cut_img_btn = new JButton("采样(C)");
cut_img_btn.setToolTipText("截取样本图片");
cut_img_btn.setMnemonic('C');
if (bimage == null)
cut_img_btn.setEnabled(false);
show_jtree_btn = new JButton("显示树(S)");
show_jtree_btn.setToolTipText("显示管理树");
show_jtree_btn.setMnemonic('S');
btnContainer = new JPanel();
btnContainer.setSize(frame.getWidth(), 35);
btnContainer.updateUI();
btnContainer.setVisible(true);
btnContainer.setBackground(Color.gray);
btnContainer.add(narrow_img_btn);
btnContainer.add(enlage_img_btn);
btnContainer.add(cut_img_btn);
btnContainer.add(show_jtree_btn);
ButtonGroup group = new ButtonGroup();
group.add(narrow_img_btn);
group.add(enlage_img_btn);
group.add(cut_img_btn);
group.add(show_jtree_btn);
group.add(show_jtree_btn);
narrow_img_btn.addActionListener(this);
enlage_img_btn.addActionListener(this);
show_jtree_btn.addActionListener(this);
cut_img_btn.addActionListener(this);
file_menu.addActionListener(this);
about_menu.addActionListener(this);
openFile_item.addActionListener(this);
exit_item.addActionListener(this);
about_item.addActionListener(this);
help_item.addActionListener(this);
imgContainer = new JPanel();
imgContainer.setSize(frame.getWidth(), frame.getHeight() - 45);
imgContainer.setBackground(Color.darkGray);
imgContainer.setVisible(true);
container = frame.getContentPane();
container.add(btnContainer).setLocation(0, 10);
container.add(imgContainer).setLocation(0, 45);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setBackground(Color.blue);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// add the close method here
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
public static void main(String args[]) {
ImageFrame frame = new ImageFrame();
}
public void actionPerformed(ActionEvent e) {
String name = null;
name = e.getActionCommand();
if (name.equals("打开(O)")) {
String imageURL;
String imageDir;
String imageName;
// 打开新的图片时,要移除上一张图片.
image.removeAll();
image.updateUI();
open_dialog = new FileDialog(frame, "选择打开的图片文件", FileDialog.LOAD);
open_dialog.setVisible(true);
imageDir = open_dialog.getDirectory();
imageName = open_dialog.getFile();
imageURL = imageDir + imageName;
openFile = new File(imageURL);
if (imageName != null) {// 叛断文件名是否为空
/**
* 当打开的文件不是图片时,提示打开文件错误.
*
*/
FileNameDeal fileNameDeal = new FileNameDeal(imageName);
String ends = fileNameDeal.getFileNameEnds();
if (fileNameDeal.wetherIsIMG(ends)) {// 当开打的文件是图片,则进行操作,否则提示文件不能打开.
// 如果文件为空,则打提示打开失败,否则将图片展示到frame上
if (openFile.length() == 0) {
javax.swing.JOptionPane.showMessageDialog(null,
"文件不存在或\n其它原因打开文件失败,\n请重试", "提醒",
javax.swing.JOptionPane.INFORMATION_MESSAGE);
return;
} else {
// 将图片变成适合大小
ChangeIMG cimg = new ChangeIMG();
bimage = cimg.ChangeIMGToProbleSize(imageURL, frame);
// 保存当前图片.
srcImage = ImageUtil.loadImage(imageURL);
ImageIcon iicon2 = new ImageIcon(bimage);
image.setIcon(iicon2);
image.removeAll();
image.updateUI();
imgContainer.add(image);
// container.add(imgContainer).setLocation(10, 45);
imgContainer.updateUI();
cut_img_btn.setEnabled(true);
}
} else {// 文件不为图片,不能打开.
javax.swing.JOptionPane.showMessageDialog(null,
"非图片文件,不能打开!", "提醒",
javax.swing.JOptionPane.INFORMATION_MESSAGE);
return;
}
} else {// 文件名为空,返回
return;
}
} else if (name.equals("退出(E)")) {
int configType = javax.swing.JOptionPane.showConfirmDialog(
ImageFrame.frame, "是否真的退出?", "提示",
JOptionPane.YES_NO_CANCEL_OPTION);
if (configType == JOptionPane.CANCEL_OPTION) {
return;
} else {
System.exit(0);
}
} else if (name.equals("关于(A)")) {
javax.swing.JOptionPane
.showMessageDialog(
null,
"功能:截取样本图片,并取其特征值,\n将其保存到数据库,供以后自\n动标注用.暂时只能支持.jpg .gif .png\n三种格式的图片",
"功能及简介",
javax.swing.JOptionPane.INFORMATION_MESSAGE);
} else if (name.equals("帮助(H)")) {
javax.swing.JOptionPane.showMessageDialog(null,
"作者:dxm\n联系方式:QQ:41835212", "帮助",
javax.swing.JOptionPane.INFORMATION_MESSAGE);
} else if (name.equals("放大(E)")) {
if (null != bimage && null != srcImage) {
ChangeIMG ci = new ChangeIMG();
// 重新设置当前图片.
bimage = ci.enlargeIMG(bimage, srcImage);
ImageIcon iicon2 = new ImageIcon(bimage);
// 打开新的图片时,要移除上一张图片.
image.removeAll();
image.setIcon(iicon2);
imgContainer.add(image);
imgContainer.updateUI();
}
} else if (name.equals("缩小(N)")) {
if (null != bimage && null != srcImage) {
ChangeIMG ci = new ChangeIMG();
// 重新设置当前图片.
bimage = ci.narrowIMG(bimage, srcImage);
ImageIcon iicon2 = new ImageIcon(bimage);
// 打开新的图片时,要移除上一张图片.
image.removeAll();
image.setIcon(iicon2);
imgContainer.add(image);
imgContainer.updateUI();
}
} else if (name.equals("采样(C)")) {
CatchScreenImageFrame csif=new CatchScreenImageFrame();
if(f==null)
f = new JTreeFrame();
else f.setVisible(true);
} else if (name.equals("显示树(S)")) {
if(f==null)
f = new JTreeFrame();
else f.setVisible(true);
}
}
private static JTreeFrame f=null;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -