/**
*
*/
package de.tutorials;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.SwingUtilities;
/**
* @author Thomas
*
*/
public class FacePlanExample extends JFrame {
LayoutPanel layoutPanel;
final static String LAYOUT_DATA_EXTENSION = ".layout";
public FacePlanExample() {
super("FacePlanExample");
setDefaultCloseOperation(EXIT_ON_CLOSE);
createJMenuBar();
layoutPanel = new LayoutPanel();
JScrollPane scrollPane = new JScrollPane(layoutPanel);
scrollPane.setPreferredSize(new Dimension(400, 300));
add(scrollPane);
pack();
setVisible(true);
}
/**
*
*/
private void createJMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem loadMenuItem = new JMenuItem("load");
loadMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
loadLayout();
}
});
JMenuItem saveMenuItem = new JMenuItem("save");
JMenuItem quitMenuItem = new JMenuItem("quit");
fileMenu.add(loadMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.add(new JSeparator());
fileMenu.add(quitMenuItem);
menuBar.add(fileMenu);
setJMenuBar(menuBar);
}
protected void loadLayoutImage(File file) {
try {
BufferedImage image = ImageIO.read(file);
layoutPanel.setLayoutImage(image);
} catch (IOException e) {
e.printStackTrace();
}
}
protected void loadLayoutDataFile(File file) {
File layoutDataFile = new File(file.getParentFile(), file.getName()
.concat(LAYOUT_DATA_EXTENSION));
System.out.println(layoutDataFile);
if (layoutDataFile.exists()) {
}
}
protected void loadLayout() {
System.out.println("load item");
JFileChooser fileChooser = new JFileChooser("c:/");
int optionValue = fileChooser.showOpenDialog(this);
if (optionValue == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
loadLayoutImage(file);
loadLayoutDataFile(file);
layoutPanel.updateUI();
}
}
/**
* @param args
*/
public static void main(String[] args) {
new FacePlanExample();
}
static class LayoutPanel extends JPanel {
BufferedImage layoutImage;
File layoutDataFile;
List<LayoutData> layoutDataList = new ArrayList<LayoutData>();
Polygon currentPolygon;
Composite composite = AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, 0.3F);
public LayoutPanel() {
setDoubleBuffered(true);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
System.out.println("foo");
if (SwingUtilities.isRightMouseButton(e)) {
currentPolygon = null;
}
if (SwingUtilities.isLeftMouseButton(e)) {
if (currentPolygon == null) {
currentPolygon = new Polygon();
}
Point p = e.getPoint();
currentPolygon.addPoint(p.x, p.y);
if (currentPolygon.npoints == 4) {
String id = JOptionPane
.showInputDialog("Please enter an ID for the current area.");
LayoutData layoutData = new LayoutData(
currentPolygon, id);
layoutDataList.add(layoutData);
currentPolygon = null;
repaint();
}
}
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
Point p = e.getPoint();
if (currentPolygon != null) {
GeneralPath path = new GeneralPath();
path.moveTo(currentPolygon.xpoints[0],
currentPolygon.ypoints[0]);
for (int i = 0; i < currentPolygon.npoints; i++) {
path.lineTo(currentPolygon.xpoints[i],
currentPolygon.ypoints[i]);
path.moveTo(currentPolygon.xpoints[i],
currentPolygon.ypoints[i]);
}
Graphics2D g = (Graphics2D) getGraphics();
g.draw(path);
g
.drawLine(
currentPolygon.xpoints[currentPolygon.npoints - 1],
currentPolygon.ypoints[currentPolygon.npoints - 1],
p.x, p.y);
repaint();
}
LayoutData layoutData = isMouseOverLayoutElement(p);
if (layoutData != null) {
setToolTipText(layoutData.getId());
} else {
setToolTipText("");
}
}
});
}
private LayoutData isMouseOverLayoutElement(Point p) {
for (LayoutData layoutData : layoutDataList) {
if (layoutData.getPolygon().contains(p)) {
return layoutData;
}
}
return null;
}
public BufferedImage getLayoutImage() {
return layoutImage;
}
public void setLayoutImage(BufferedImage layoutImage) {
this.layoutImage = layoutImage;
Dimension dimension = new Dimension(layoutImage.getWidth(),
layoutImage.getHeight());
setPreferredSize(dimension);
}
public File getLayoutDataFile() {
return layoutDataFile;
}
public void setLayoutDataFile(File layoutDataFile) {
this.layoutDataFile = layoutDataFile;
}
protected void paintComponent(Graphics gra) {
Graphics2D g = (Graphics2D) gra;
super.paintComponent(g);
g.drawImage(layoutImage, 0, 0, null);
g.setColor(Color.RED);
g.setComposite(composite);
for (LayoutData layoutData : layoutDataList) {
g.fillPolygon(layoutData.getPolygon());
}
}
}
static class LayoutData {
Polygon polygon;
String id;
public LayoutData(Polygon polygon, String id) {
super();
// TODO Auto-generated constructor stub
this.polygon = polygon;
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Polygon getPolygon() {
return polygon;
}
public void setPolygon(Polygon polygon) {
this.polygon = polygon;
}
}
}