📄 clientframe.java
字号:
package cn.dxm.frame;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.border.Border;
import cn.dxm.utils.MyFilter;
import cn.dxm.utils.SwingWorker;
public class ClientFrame extends JFrame{
private JToolBar toolBar;
private JLabel imageContainer;
private JLayeredPane layerPane;
private DrawingLayer drawingLayer;
private String imageName;
private JLabel currentShape;
private JScrollPane scrollPane;
public ClientFrame(){
super("图片自动识别器");
initComponents();
this.setVisible(true);
}
private void initComponents(){
toolBar = new JToolBar(null, JToolBar.HORIZONTAL);
toolBar.setFloatable(false);
ImageIcon newIcon = new ImageIcon(ClientFrame.class.getResource("/img/Open24.gif"), "Open");
JButton newButton = new JButton();
newButton.setToolTipText("Open Image");
newButton.setText(null);
newButton.setMargin(new Insets(0, 0, 0, 0));
newButton.setIcon(newIcon);
newButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileChooser.setFileFilter(new MyFilter());
fileChooser.setMultiSelectionEnabled(false);
fileChooser.showOpenDialog(ClientFrame.this);
File file = fileChooser.getSelectedFile();
if (file != null) {
imageName = file.getName();
System.out.println("the chose image file's root is:"+file.getPath());
loadImage(file.getPath());
// drawingLayer.deleteShapes();
}
}
});
toolBar.add(newButton);
ImageIcon selectIcon = new ImageIcon(ClientFrame.class.getResource("/img/Select24.gif"), "Select");
JToggleButton selectButton = new JToggleButton();
selectButton.setToolTipText("Selection");
selectButton.setText(null);
selectButton.setMargin(new Insets(0, 0, 0, 0));
selectButton.setIcon(selectIcon);
selectButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// drawingLayer.setCurrentShape(SELECTION);
// updateShape("Selection");
}
});
toolBar.add(selectButton);
ImageIcon rectIcon = new ImageIcon(ClientFrame.class.getResource("/img/Rect24.gif"), "Rect");
JToggleButton rectButton = new JToggleButton();
rectButton.setToolTipText("Rectangle");
rectButton.setText(null);
rectButton.setMargin(new Insets(0, 0, 0, 0));
rectButton.setIcon(rectIcon);
rectButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// drawingLayer.setCurrentShape(RECTANGLE);
// updateShape("Rectangle");
}
});
rectButton.setSelected(true);
toolBar.add(rectButton);
ImageIcon deleteIcon = new ImageIcon(ClientFrame.class.getResource("/img/Delete24.gif"), "Delete");
JButton deleteButton = new JButton();
deleteButton.setToolTipText("Delete All");
deleteButton.setText(null);
deleteButton.setMargin(new Insets(0, 0, 0, 0));
deleteButton.setIcon(deleteIcon);
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//drawingLayer.deleteShapes();
}
});
toolBar.add(deleteButton);
ButtonGroup group = new ButtonGroup();
group.add(selectButton);
group.add(rectButton);
this.getContentPane().add(toolBar, BorderLayout.NORTH);
imageContainer = new JLabel("");
imageContainer.setHorizontalAlignment(JLabel.LEFT);
imageContainer.setVerticalAlignment(JLabel.TOP);
drawingLayer = new DrawingLayer(this);
layerPane = new JLayeredPane();
layerPane.add(imageContainer, new Integer(1));
layerPane.add(drawingLayer, new Integer(2));
scrollPane = new JScrollPane(layerPane);
this.getContentPane().add(scrollPane, BorderLayout.CENTER);
JPanel shapePanel = new JPanel();
Border blackline = BorderFactory.createLineBorder(Color.black);
shapePanel.setBorder(blackline);
currentShape = new JLabel("Rectangle");
shapePanel.add(currentShape);
this.getContentPane().add(shapePanel, BorderLayout.SOUTH);
this.setSize(400, 400);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
this.setLocation(screenSize.width/2 - (frameSize.width/2), screenSize.height/2 - (frameSize.height/2));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void loadImage(final String imagePath) {
final SwingWorker worker = new SwingWorker() {
ImageIcon icon = null;
public Object construct() {
File imagefile = new File(imagePath);
if(imagefile.exists() && imagefile.isFile())
icon = new ImageIcon(imagePath);
return icon; //return value not used by this program
}
//Runs on the event-dispatching thread.
public void finished() {
if(icon != null) {
updatePhotograph(icon);
}
}
};
worker.start();
}
private void updatePhotograph(ImageIcon image) {
Dimension dim = new Dimension(image.getIconWidth(),image.getIconHeight());
imageContainer.setText(null);
imageContainer.setBounds(0, 0, image.getIconWidth(), image.getIconHeight());
drawingLayer.setSize(dim);
imageContainer.setIcon(image);
layerPane.setPreferredSize(dim);
this.pack();
}
public static void main(String args[]){
ClientFrame cf=new ClientFrame();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -