import java.util.List;
import java.util.ArrayList;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import areas.*;
List<Area> areaList = new ArrayList<Area>();
boolean stateListenerOn = true;
ImagePanel imgPanel = new ImagePanel(){
public void onUpdate(boolean isPolygon){
refresh();
if(isPolygon){refreshPolyVertices();}
}
};
JSpinner rectAX = new JSpinner();
JSpinner rectAY = new JSpinner();
JSpinner rectBX = new JSpinner();
JSpinner rectBY = new JSpinner();
JSpinner circleCenterX = new JSpinner();
JSpinner circleCenterY = new JSpinner();
JSpinner circleRadius = new JSpinner();
JList polyVertices
= new JList(polyVerticesModel
);
JSpinner polyVertexX = new JSpinner();
JSpinner polyVertexY = new JSpinner();
public Main(){
super("Java HTML image mapping tool");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(1280, 960);
setExtendedState
(JFrame.
MAXIMIZED_BOTH);
SidePanel.
setPreferredSize(new Dimension(400, 960
));
l.addListSelectionListener(this);
hrefPanel.add(hrefLabel);
hrefPanel.add(hrefTextField);
hrefPanel.setSize(400, 50);
hrefTextField.getDocument().addDocumentListener(this);
editorPanel.setLayout(editorLayout);
editorPanel.
add(new JPanel(),
"");
editorPanel.add(rectEditorPanel, "rect");
editorPanel.add(circleEditorPanel, "circle");
editorPanel.add(polyEditorPanel, "poly");
rectEditorPanel.
add(new JLabel(""));
rectEditorPanel.
add(new JLabel("X"));
rectEditorPanel.
add(new JLabel("Y"));
rectEditorPanel.
add(new JLabel("Edit"));
rectEditorPanel.
add(new JLabel("Top-left:"));
rectEditorPanel.add(rectAX);
rectEditorPanel.add(rectAY);
rectEditorPanel.add(editRectA);
rectEditorPanel.
add(new JLabel("Bottom-right:"));
rectEditorPanel.add(rectBX);
rectEditorPanel.add(rectBY);
rectEditorPanel.add(editRectB);
rectAX.addChangeListener(this);
rectAY.addChangeListener(this);
rectBX.addChangeListener(this);
rectBY.addChangeListener(this);
editRectA.addActionListener(this);
editRectB.addActionListener(this);
circleEditorPanel.
setLayout(new GridLayout(2, 4
));
circleEditorPanel.
add(new JLabel("Center:"));
circleEditorPanel.add(circleCenterX);
circleEditorPanel.add(circleCenterY);
circleEditorPanel.add(editCircleCenter);
circleEditorPanel.
add(new JLabel("Radius:"));
circleEditorPanel.add(circleRadius);
circleEditorPanel.
add(new JLabel(""));
circleEditorPanel.add(editCircleRadius);
circleCenterX.addChangeListener(this);
circleCenterY.addChangeListener(this);
circleRadius.addChangeListener(this);
editCircleCenter.addActionListener(this);
editCircleRadius.addActionListener(this);
polyVertices.setFixedCellWidth(100);
gc.gridwidth = 3;
gc.gridheight = 2;
polyEditorPanel.add(polyVertices, gc);
gc.gridwidth = 1;
gc.gridheight = 1;
gc.gridx = 3;
gc.gridy = 0;
polyEditorPanel.add(polyAddVertex, gc);
gc.gridx = 4;
polyEditorPanel.add(polyRemoveVertex, gc);
gc.gridx = 3;
gc.gridy = 1;
polyEditorPanel.add(polyVertexX, gc);
gc.gridx = 4;
polyEditorPanel.add(polyVertexY, gc);
gc.gridx = 5;
polyEditorPanel.add(polyEditVertex, gc);
polyAddVertex.addActionListener(this);
polyRemoveVertex.addActionListener(this);
polyEditVertex.addActionListener(this);
polyVertexX.addChangeListener(this);
polyVertexY.addChangeListener(this);
polyVertices.addListSelectionListener(this);
buttonsPanel.
setLayout(new GridLayout(2, 3, 5, 5
));
addRect.addActionListener(this);
addCircle.addActionListener(this);
addPoly.addActionListener(this);
remove.addActionListener(this);
changeImg.addActionListener(this);
generateHTML.addActionListener(this);
buttonsPanel.add(addRect);
buttonsPanel.add(addCircle);
buttonsPanel.add(addPoly);
buttonsPanel.add(remove);
buttonsPanel.add(changeImg);
buttonsPanel.add(generateHTML);
imgPanel.areas = areaList;
scrollPanel.
setHorizontalScrollBarPolicy(JScrollPane.
HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPanel.
setVerticalScrollBarPolicy(JScrollPane.
VERTICAL_SCROLLBAR_AS_NEEDED);
editTooltip.
setFont(new Font("Arial",
Font.
PLAIN, 20
));
refresh();
}
public void loadImage(){
fc.setDialogTitle("Choose the image file");
if(fc.
showOpenDialog(this) != JFileChooser.
APPROVE_OPTION){return;}
if(!imgPanel.loadImage(fc.getSelectedFile())){
if(JOptionPane.
showOptionDialog(this,
"Error: couldn't load the image file. Do you want to try loading another one?",
"Error",
JOptionPane.
YES_NO_OPTION,
JOptionPane.
ERROR_MESSAGE,
null,
null,
null) == JOptionPane.
YES_OPTION){
loadImage();
}
return;
}
rectAX.setModel(new SpinnerNumberModel(0, 0, imgPanel.img.getWidth(null), 1));
rectAY.setModel(new SpinnerNumberModel(0, 0, imgPanel.img.getHeight(null), 1));
rectBX.setModel(new SpinnerNumberModel(0, 0, imgPanel.img.getWidth(null), 1));
rectBY.setModel(new SpinnerNumberModel(0, 0, imgPanel.img.getHeight(null), 1));
circleCenterY.setModel(new SpinnerNumberModel(0, 0, imgPanel.img.getHeight(null), 1));
circleCenterX.setModel(new SpinnerNumberModel(0, 0, imgPanel.img.getWidth(null), 1));
circleRadius.
setModel(new SpinnerNumberModel
(0, 0,
Math.
max(imgPanel.
img.
getWidth(null), imgPanel.
img.
getHeight(null)), 1
));
polyVertexX.setModel(new SpinnerNumberModel(0, 0, imgPanel.img.getWidth(null), 1));
polyVertexY.setModel(new SpinnerNumberModel(0, 0, imgPanel.img.getHeight(null), 1));
}
if(e.getSource() == l){
Area area
= areaList.
get(l.
getSelectedIndex());
editorLayout.show(editorPanel, area.getShapeName());
if(l.getSelectedIndex() != imgPanel.editIndex){
imgPanel.editAction = "";
editTooltip.setText("");
hrefTextField.setText(area.href);
}
imgPanel.editIndex = l.getSelectedIndex();
imgPanel.repaint();
refreshValues();
}else if(e.getSource() == polyVertices){
Point v
= ((PolygonArea
)areaList.
get(l.
getSelectedIndex())).
vertices.
get(polyVertices.
getSelectedIndex());
polyVertexX.setValue(v.x);
polyVertexY.setValue(v.y);
}
}
if(e.getSource() == addRect){
areaList.add(new RectangleArea());
//l.setSelectedIndex(0); //TODO
}else if(e.getSource() == addCircle){
areaList.add(new CircleArea());
}else if(e.getSource() == addPoly){
areaList.add(new PolygonArea());
}else if(e.getSource() == remove){
int i = l.getSelectedIndex();
if(i != -1){areaList.remove(i);}
}else if(e.getSource() == changeImg){
loadImage();
}else if(e.getSource() == generateHTML){
HTMLdialog d = new HTMLdialog(this, areaList);
d.show();
}else if(e.getSource() == editRectA){
imgPanel.editAction = "rectA";
editTooltip.setText("Click the image to move the top-left point");
}else if(e.getSource() == editRectB){
imgPanel.editAction = "rectB";
editTooltip.setText("Click the image to move the bottom-right point");
}else if(e.getSource() == editCircleCenter){
imgPanel.editAction = "circleCenter";
editTooltip.setText("Click the image to move the center");
}else if(e.getSource() == editCircleRadius){
imgPanel.editAction = "circleRadius";
editTooltip.setText("Click the image to set the radius");
}else if(e.getSource() == polyAddVertex){
Area area
= areaList.
get(l.
getSelectedIndex());
((PolygonArea
)area
).
vertices.
add(new Point(0, 0
));
refreshPolyVertices();
}else if(e.getSource() == polyRemoveVertex){
Area area
= areaList.
get(l.
getSelectedIndex());
int i = polyVertices.getSelectedIndex();
if(i != -1){((PolygonArea)area).vertices.remove(i);}
refreshPolyVertices();
}else if(e.getSource() == polyEditVertex){
imgPanel.editAction = "polyVertex";
imgPanel.polyVertexEditIndex = polyVertices.getSelectedIndex();
editTooltip.setText("Click the image to set the vertex position");
}
refresh();
}
if(!stateListenerOn){return;}
Area area
= areaList.
get(l.
getSelectedIndex());
switch(area.getShapeName()){
case "rect":
((RectangleArea
)area
).
a.
x = (Integer)rectAX.
getValue();
((RectangleArea
)area
).
a.
y = (Integer)rectAY.
getValue();
((RectangleArea
)area
).
b.
x = (Integer)rectBX.
getValue();
((RectangleArea
)area
).
b.
y = (Integer)rectBY.
getValue();
break;
case "circle":
((CircleArea
)area
).
center.
x = (Integer)circleCenterX.
getValue();
((CircleArea
)area
).
center.
y = (Integer)circleCenterY.
getValue();
((CircleArea
)area
).
r = (Integer)circleRadius.
getValue();
break;
case "poly":
((PolygonArea
)area
).
vertices.
set(polyVertices.
getSelectedIndex(),
new Point((Integer)polyVertexX.
getValue(),
(Integer)polyVertexY.
getValue()));
refreshPolyVertices();
break;
}
refresh();
}
public void labelEvent(){
int i = l.getSelectedIndex();
if(i != -1){
areaList.get(i).href = hrefTextField.getText();
refresh();
}
}
private void refresh(){
int i = l.getSelectedIndex();
l.removeListSelectionListener(this);
list.clear();
list.addElement(a.getFullName());
}
l.addListSelectionListener(this);
l.setSelectedIndex(i);
refreshValues();
imgPanel.repaint();
}
private void refreshValues(){
stateListenerOn = false;
if(l.getSelectedIndex() != -1){
Area area
= areaList.
get(l.
getSelectedIndex());
switch(area.getShapeName()){
case "rect":
rectAX.setValue(((RectangleArea)area).a.x);
rectAY.setValue(((RectangleArea)area).a.y);
rectBX.setValue(((RectangleArea)area).b.x);
rectBY.setValue(((RectangleArea)area).b.y);
break;
case "circle":
circleCenterX.setValue(((CircleArea)area).center.x);
circleCenterY.setValue(((CircleArea)area).center.y);
circleRadius.setValue(((CircleArea)area).r);
break;
case "poly":
refreshPolyVertices();
int vi = polyVertices.getSelectedIndex();
if(vi != -1){
Point v
= ((PolygonArea
)area
).
vertices.
get(vi
);
polyVertexX.setValue(v.x);
polyVertexY.setValue(v.y);
}
break;
}
}
stateListenerOn = true;
}
private void refreshPolyVertices(){
int i = polyVertices.getSelectedIndex();
polyVertices.removeListSelectionListener(this);
polyVerticesModel.clear();
PolygonArea area = ((PolygonArea)areaList.get(l.getSelectedIndex()));
for(Point v
: area.
vertices){
polyVerticesModel.
addElement(String.
format("Vertex (%d, %d)", v.
x, v.
y));
}
polyVertices.addListSelectionListener(this);
polyVertices.setSelectedIndex(i);
}
public static void main
(String[] args
){
public void run(){
Main m = new Main();
m.setVisible(true);
m.loadImage();
}
});
}
}