📄 jimagemapper.java
字号:
/**
* JImageMapper.java - main class
* Mark Bryan Yu
* Todd S. Davenport
* jimagemapper.sourceforge.net
*
* This file was modified by
* Heiko Schroeder
* wikiimagemapper.sourceforge.net
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package cn.dxm.client;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
public class JImageMapper extends JFrame {
/**
*
*/
private static final long serialVersionUID = -7174624436533324512L;
public static final int RECTANGLE = 1;
public static final int SELECTION = 4;
public static final String imagepath=System.getProperty("user.dir")+"\\client\\images";
public static final String imagepath1=System.getProperty("user.dir")+"\\client\\image";
public static final String imagefeature[] =new String[10000]; //存取获得的对象语义
public static final String features="";
public static final int imagenum=0;
private JToolBar toolBar;
private JLabel imageContainer;
private JLayeredPane layerPane;
private DrawingLayer drawingLayer;
private String imageName;
private JLabel currentShape;
private JScrollPane scrollPane;
private Dimension currentImageSize;
public JImageMapper() {
super("图片解析器");
initComponents();
//this.setAlwaysOnTop(true);
this.setVisible(true);
}
public JImageMapper(String newImage) {
this();
imageName = newImage;
loadImage(imageName);
drawingLayer.deleteShapes();
}
private void initComponents() {
toolBar = new JToolBar(null, JToolBar.HORIZONTAL);
toolBar.setFloatable(false);
ImageIcon newIcon = new ImageIcon(JImageMapper.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(JImageMapper.this);
File file = fileChooser.getSelectedFile();
MatchImage.delete(imagepath);
if (file != null) {
imageName = file.getName();
loadImage(file.getPath());
MatchImage.copyFile(file.getPath(),imagepath1+"\\All.jpg");
drawingLayer.deleteShapes();
}
}
});
toolBar.add(newButton);
ImageIcon exportIcon = new ImageIcon(JImageMapper.class
.getResource("/img/Export24.gif"), "Export");
JButton exportButton = new JButton();
exportButton.setToolTipText("Copy to Clipboard");
exportButton.setText(null);
exportButton.setMargin(new Insets(0, 0, 0, 0));
exportButton.setIcon(exportIcon);
exportButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
java.util.List shapes = drawingLayer.getShapes();
MapGenerator.generateImageMap(imageName, shapes);
}
});
toolBar.add(exportButton);
/* IMPORT ICON */
ImageIcon importIcon = new ImageIcon(JImageMapper.class
.getResource("/img/Import24.gif"), "Import");
JButton importButton = new JButton();
importButton.setToolTipText("Get from Clipboard");
importButton.setText(null);
importButton.setMargin(new Insets(0, 0, 0, 0));
importButton.setIcon(importIcon);
importButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
drawingLayer.addShapes(ImportImageMap.processImageMapSyntax());
}
});
toolBar.add(importButton);
toolBar.add(new JToolBar.Separator());
ImageIcon selectIcon = new ImageIcon(JImageMapper.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(JImageMapper.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);
toolBar.add(new JToolBar.Separator());
ImageIcon deleteIcon = new ImageIcon(JImageMapper.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);
toolBar.add(new JToolBar.Separator());
ImageIcon helpIcon = new ImageIcon(JImageMapper.class
.getResource("/img/HelpCenter23.gif"), "Help");
JButton helpButton = new JButton();
helpButton.setToolTipText("Help");
helpButton.setText(null);
helpButton.setMargin(new Insets(0, 0, 0, 0));
helpButton.setIcon(helpIcon);
helpButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
javax.swing.JOptionPane
.showMessageDialog(
JImageMapper.this,
"after select the choice img\n you can get the information \n of the image object.\n author:dxm",
"Help",
javax.swing.JOptionPane.INFORMATION_MESSAGE);
}
});
toolBar.add(helpButton);
toolBar.add(new JToolBar.Separator());
ImageIcon exitIcon = new ImageIcon(JImageMapper.class
.getResource("/img/Exit.gif"), "Exit");
JButton exitButton = new JButton();
exitButton.setToolTipText("Exit");
exitButton.setText(null);
exitButton.setMargin(new Insets(0, 0, 0, 0));
exitButton.setIcon(exitIcon);
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int result = javax.swing.JOptionPane.showConfirmDialog(null,
"are you sure to exit the system?", "WARING",
javax.swing.JOptionPane.CANCEL_OPTION);
if(result==JOptionPane.CANCEL_OPTION){
return ;
}else{
System.exit(0);
}
}
});
toolBar.add(exitButton);
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);
JButton save = new JButton("对象语义");
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MatchImage img =new MatchImage();
MatchImage.delete(imagepath);
drawingLayer.saveShapes();
imagefeature[imagenum]=img.getMaxFeature();
updateShape(img.getMaxFeature());
}
});
currentShape = new JLabel("Rectangle");
shapePanel.add(save);
shapePanel.add(currentShape);
this.getContentPane().add(shapePanel, BorderLayout.SOUTH);
this.setSize(400, 400);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setMaximumSize(new Dimension(screenSize.width - 600,
screenSize.height - 500));
Dimension frameSize = this.getSize();
this.setLocation(screenSize.width / 2 - (frameSize.width / 2),
screenSize.height / 2 - (frameSize.height / 2));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// Must be invoked from the event-dispatching thread.
private void updatePhotograph(ImageIcon image) {
currentImageSize = new Dimension(image.getIconWidth(), image
.getIconHeight());
imageContainer.setText(null);
imageContainer.setBounds(0, 0, image.getIconWidth(), image
.getIconHeight());
drawingLayer.setSize(currentImageSize);
imageContainer.setIcon(image);
layerPane.setPreferredSize(currentImageSize);
this.pack();
}
// Load an image in a separate thread.
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 updateShape(String text) {
currentShape.setText(text);
}
class MyFilter extends FileFilter {
public boolean accept(File file) {
if (file.isDirectory())
return true;
String filename = file.getName();
return filename.endsWith(".png") || filename.endsWith(".gif")
|| filename.endsWith(".jpg") || filename.endsWith(".PNG")
|| filename.endsWith(".GIF") || filename.endsWith(".JPG")
|| filename.endsWith(".JPEG") || filename.endsWith(".jpeg");
}
public String getDescription() {
return "Graphics files";
}
}
public static void main(String[] args) {
if (args.length > 0) {
new JImageMapper(args[0]);
} else {
new JImageMapper();
}
//System.out.println(System.getProperty("user.dir"));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -