Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
JHtmlImageMapper - Main.java

Main.java

Caricato da: GN
Scarica il programma completo

  1. import java.util.List;
  2. import java.util.ArrayList;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6. import javax.swing.event.*;
  7. import areas.*;
  8.  
  9.  
  10.         List<Area> areaList = new ArrayList<Area>();
  11.         boolean stateListenerOn = true;
  12.  
  13.         DefaultListModel list = new DefaultListModel();
  14.         JList l = new JList(list);
  15.         JPanel SidePanel = new JPanel();
  16.         ImagePanel imgPanel = new ImagePanel(){
  17.                 public void onUpdate(boolean isPolygon){
  18.                         refresh();
  19.                         if(isPolygon){refreshPolyVertices();}
  20.                 }
  21.         };
  22.         JPanel hrefPanel = new JPanel();
  23.         JLabel hrefLabel = new JLabel("Link (href attribute):");
  24.         JTextField hrefTextField = new JTextField(20);
  25.         JPanel editorPanel = new JPanel();
  26.         JLabel editTooltip = new JLabel("");
  27.         CardLayout editorLayout = new CardLayout();
  28.  
  29.         JPanel rectEditorPanel = new JPanel();
  30.         JSpinner rectAX = new JSpinner();
  31.         JSpinner rectAY = new JSpinner();
  32.         JSpinner rectBX = new JSpinner();
  33.         JSpinner rectBY = new JSpinner();
  34.         JButton editRectA = new JButton("Edit");
  35.         JButton editRectB = new JButton("Edit");
  36.  
  37.         JPanel circleEditorPanel = new JPanel();
  38.         JSpinner circleCenterX = new JSpinner();
  39.         JSpinner circleCenterY = new JSpinner();
  40.         JButton editCircleCenter = new JButton("Edit");
  41.         JSpinner circleRadius = new JSpinner();
  42.         JButton editCircleRadius = new JButton("Edit");
  43.  
  44.         JPanel polyEditorPanel = new JPanel();
  45.         DefaultListModel polyVerticesModel = new DefaultListModel();
  46.         JList polyVertices = new JList(polyVerticesModel);
  47.         JButton polyAddVertex = new JButton("Add");
  48.         JButton polyRemoveVertex = new JButton("Remove");
  49.         JSpinner polyVertexX = new JSpinner();
  50.         JSpinner polyVertexY = new JSpinner();
  51.         JButton polyEditVertex = new JButton("Edit");
  52.  
  53.         JButton addRect = new JButton("Add rect");
  54.         JButton addCircle = new JButton("Add circle");
  55.         JButton addPoly = new JButton("Add poly");
  56.         JButton remove = new JButton("Remove");
  57.         JButton changeImg = new JButton("Change image");
  58.         JButton generateHTML = new JButton("Generate HTML");
  59.  
  60.         public Main(){
  61.                 super("Java HTML image mapping tool");
  62.                 setDefaultCloseOperation(EXIT_ON_CLOSE);
  63.                 setLayout(new BorderLayout());
  64.                 setSize(1280, 960);
  65.                 setExtendedState(JFrame.MAXIMIZED_BOTH);
  66.  
  67.                 SidePanel.setPreferredSize(new Dimension(400, 960));
  68.                 SidePanel.setLayout(new BorderLayout(2, 2));
  69.                 JLabel listLabel = new JLabel("Image clickable areas:");
  70.                 SidePanel.add(listLabel, BorderLayout.NORTH);
  71.  
  72.                 l.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  73.                 l.addListSelectionListener(this);
  74.  
  75.                 hrefPanel.add(hrefLabel);
  76.                 hrefPanel.add(hrefTextField);
  77.                 hrefPanel.setSize(400, 50);
  78.                 hrefTextField.getDocument().addDocumentListener(this);
  79.  
  80.                 JPanel CenterSidePanel = new JPanel();
  81.                 CenterSidePanel.setLayout(new BorderLayout());
  82.                 CenterSidePanel.add(l, BorderLayout.CENTER);
  83.                 CenterSidePanel.add(hrefPanel, BorderLayout.NORTH);
  84.                 CenterSidePanel.add(editorPanel, BorderLayout.SOUTH);
  85.                 editorPanel.setLayout(editorLayout);
  86.  
  87.                 SidePanel.add(CenterSidePanel, BorderLayout.CENTER);
  88.                 editorPanel.add(new JPanel(), "");
  89.                 editorPanel.add(rectEditorPanel, "rect");
  90.                 editorPanel.add(circleEditorPanel, "circle");
  91.                 editorPanel.add(polyEditorPanel, "poly");
  92.  
  93.                 rectEditorPanel.setLayout(new GridLayout(3, 4));
  94.                 rectEditorPanel.add(new JLabel(""));
  95.                 rectEditorPanel.add(new JLabel("X"));
  96.                 rectEditorPanel.add(new JLabel("Y"));
  97.                 rectEditorPanel.add(new JLabel("Edit"));
  98.                 rectEditorPanel.add(new JLabel("Top-left:"));
  99.                 rectEditorPanel.add(rectAX);
  100.                 rectEditorPanel.add(rectAY);
  101.                 rectEditorPanel.add(editRectA);
  102.                 rectEditorPanel.add(new JLabel("Bottom-right:"));
  103.                 rectEditorPanel.add(rectBX);
  104.                 rectEditorPanel.add(rectBY);
  105.                 rectEditorPanel.add(editRectB);
  106.                 rectAX.addChangeListener(this);
  107.                 rectAY.addChangeListener(this);
  108.                 rectBX.addChangeListener(this);
  109.                 rectBY.addChangeListener(this);
  110.                 editRectA.addActionListener(this);
  111.                 editRectB.addActionListener(this);
  112.  
  113.                 circleEditorPanel.setLayout(new GridLayout(2, 4));
  114.                 circleEditorPanel.add(new JLabel("Center:"));
  115.                 circleEditorPanel.add(circleCenterX);
  116.                 circleEditorPanel.add(circleCenterY);
  117.                 circleEditorPanel.add(editCircleCenter);
  118.                 circleEditorPanel.add(new JLabel("Radius:"));
  119.                 circleEditorPanel.add(circleRadius);
  120.                 circleEditorPanel.add(new JLabel(""));
  121.                 circleEditorPanel.add(editCircleRadius);
  122.                 circleCenterX.addChangeListener(this);
  123.                 circleCenterY.addChangeListener(this);
  124.                 circleRadius.addChangeListener(this);
  125.                 editCircleCenter.addActionListener(this);
  126.                 editCircleRadius.addActionListener(this);
  127.  
  128.                 polyVertices.setFixedCellWidth(100);
  129.                 polyEditorPanel.setLayout(new GridBagLayout());
  130.                 GridBagConstraints gc = new GridBagConstraints();
  131.                 gc.fill = GridBagConstraints.BOTH;
  132.                 gc.gridwidth = 3;
  133.                 gc.gridheight = 2;
  134.                 polyEditorPanel.add(polyVertices, gc);
  135.                 gc.gridwidth = 1;
  136.                 gc.gridheight = 1;
  137.                 gc.gridx = 3;
  138.                 gc.gridy = 0;
  139.                 polyEditorPanel.add(polyAddVertex, gc);
  140.                 gc.gridx = 4;
  141.                 polyEditorPanel.add(polyRemoveVertex, gc);     
  142.                 gc.gridx = 3;
  143.                 gc.gridy = 1;
  144.                 polyEditorPanel.add(polyVertexX, gc);  
  145.                 gc.gridx = 4;
  146.                 polyEditorPanel.add(polyVertexY, gc);  
  147.                 gc.gridx = 5;
  148.                 polyEditorPanel.add(polyEditVertex, gc);
  149.                 polyAddVertex.addActionListener(this);
  150.                 polyRemoveVertex.addActionListener(this);
  151.                 polyEditVertex.addActionListener(this);
  152.                 polyVertexX.addChangeListener(this);
  153.                 polyVertexY.addChangeListener(this);
  154.                 polyVertices.addListSelectionListener(this);
  155.                 polyVertices.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  156.  
  157.  
  158.                 JPanel buttonsPanel = new JPanel();
  159.                 buttonsPanel.setLayout(new GridLayout(2, 3, 5, 5));
  160.                 addRect.addActionListener(this);
  161.                 addCircle.addActionListener(this);
  162.                 addPoly.addActionListener(this);
  163.                 remove.addActionListener(this);
  164.                 changeImg.addActionListener(this);
  165.                 generateHTML.addActionListener(this);
  166.                 buttonsPanel.add(addRect);
  167.                 buttonsPanel.add(addCircle);
  168.                 buttonsPanel.add(addPoly);
  169.                 buttonsPanel.add(remove);
  170.                 buttonsPanel.add(changeImg);
  171.                 buttonsPanel.add(generateHTML);
  172.                 SidePanel.add(buttonsPanel, BorderLayout.SOUTH);
  173.  
  174.                 add(SidePanel, BorderLayout.WEST);
  175.                 imgPanel.areas = areaList;
  176.                 JScrollPane scrollPanel = new JScrollPane(imgPanel);
  177.                 scrollPanel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  178.                 scrollPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
  179.                 JPanel mainPanel = new JPanel();
  180.                 mainPanel.setLayout(new BorderLayout());
  181.                 mainPanel.add(scrollPanel, BorderLayout.CENTER);
  182.                 editTooltip.setFont(new Font("Arial", Font.PLAIN, 20));
  183.                 mainPanel.add(editTooltip, BorderLayout.NORTH);
  184.                 add(mainPanel, BorderLayout.CENTER);
  185.                 refresh();
  186.         }
  187.  
  188.         public void loadImage(){
  189.                 JFileChooser fc = new JFileChooser();
  190.                 fc.setDialogTitle("Choose the image file");
  191.                 if(fc.showOpenDialog(this) != JFileChooser.APPROVE_OPTION){return;}
  192.                 if(!imgPanel.loadImage(fc.getSelectedFile())){
  193.                         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){
  194.                                 loadImage();
  195.                         }
  196.                         return;
  197.                 }
  198.                 rectAX.setModel(new SpinnerNumberModel(0, 0, imgPanel.img.getWidth(null), 1));
  199.                 rectAY.setModel(new SpinnerNumberModel(0, 0, imgPanel.img.getHeight(null), 1));
  200.                 rectBX.setModel(new SpinnerNumberModel(0, 0, imgPanel.img.getWidth(null), 1));
  201.                 rectBY.setModel(new SpinnerNumberModel(0, 0, imgPanel.img.getHeight(null), 1));
  202.                 circleCenterY.setModel(new SpinnerNumberModel(0, 0, imgPanel.img.getHeight(null), 1));
  203.                 circleCenterX.setModel(new SpinnerNumberModel(0, 0, imgPanel.img.getWidth(null), 1));
  204.                 circleRadius.setModel(new SpinnerNumberModel(0, 0, Math.max(imgPanel.img.getWidth(null), imgPanel.img.getHeight(null)), 1));
  205.                 polyVertexX.setModel(new SpinnerNumberModel(0, 0, imgPanel.img.getWidth(null), 1));
  206.                 polyVertexY.setModel(new SpinnerNumberModel(0, 0, imgPanel.img.getHeight(null), 1));
  207.         }
  208.  
  209.         public void valueChanged(ListSelectionEvent e){
  210.                 if(e.getSource() == l){
  211.                         Area area = areaList.get(l.getSelectedIndex());
  212.                         editorLayout.show(editorPanel, area.getShapeName());
  213.                         if(l.getSelectedIndex() != imgPanel.editIndex){
  214.                                 imgPanel.editAction = "";
  215.                                 editTooltip.setText("");
  216.                                 hrefTextField.setText(area.href);
  217.                         }
  218.                         imgPanel.editIndex = l.getSelectedIndex();
  219.                         imgPanel.repaint();
  220.                         refreshValues();
  221.                 }else if(e.getSource() == polyVertices){
  222.                         Point v = ((PolygonArea)areaList.get(l.getSelectedIndex())).vertices.get(polyVertices.getSelectedIndex());
  223.                         polyVertexX.setValue(v.x);
  224.                         polyVertexY.setValue(v.y);
  225.                 }
  226.         }
  227.  
  228.         public void actionPerformed(ActionEvent e){
  229.                 if(e.getSource() == addRect){
  230.                         areaList.add(new RectangleArea());
  231.                         //l.setSelectedIndex(0); //TODO
  232.                 }else if(e.getSource() == addCircle){
  233.                         areaList.add(new CircleArea());
  234.                 }else if(e.getSource() == addPoly){
  235.                         areaList.add(new PolygonArea());
  236.                 }else if(e.getSource() == remove){
  237.                         int i = l.getSelectedIndex();
  238.                         if(i != -1){areaList.remove(i);}
  239.                 }else if(e.getSource() == changeImg){
  240.                         loadImage();
  241.                 }else if(e.getSource() == generateHTML){
  242.                         HTMLdialog d = new HTMLdialog(this, areaList);
  243.                         d.show();
  244.                 }else if(e.getSource() == editRectA){
  245.                         imgPanel.editAction = "rectA";
  246.                         editTooltip.setText("Click the image to move the top-left point");
  247.                 }else if(e.getSource() == editRectB){
  248.                         imgPanel.editAction = "rectB";
  249.                         editTooltip.setText("Click the image to move the bottom-right point");
  250.                 }else if(e.getSource() == editCircleCenter){
  251.                         imgPanel.editAction = "circleCenter";
  252.                         editTooltip.setText("Click the image to move the center");
  253.                 }else if(e.getSource() == editCircleRadius){
  254.                         imgPanel.editAction = "circleRadius";
  255.                         editTooltip.setText("Click the image to set the radius");
  256.                 }else if(e.getSource() == polyAddVertex){
  257.                         Area area = areaList.get(l.getSelectedIndex());
  258.                         ((PolygonArea)area).vertices.add(new Point(0, 0));
  259.                         refreshPolyVertices();
  260.                 }else if(e.getSource() == polyRemoveVertex){
  261.                         Area area = areaList.get(l.getSelectedIndex());
  262.                         int i = polyVertices.getSelectedIndex();
  263.                         if(i != -1){((PolygonArea)area).vertices.remove(i);}
  264.                         refreshPolyVertices();
  265.                 }else if(e.getSource() == polyEditVertex){
  266.                         imgPanel.editAction = "polyVertex";
  267.                         imgPanel.polyVertexEditIndex = polyVertices.getSelectedIndex();
  268.                         editTooltip.setText("Click the image to set the vertex position");
  269.                 }
  270.                 refresh();
  271.         }
  272.  
  273.         public void stateChanged(ChangeEvent e){
  274.                 if(!stateListenerOn){return;}
  275.                 Area area = areaList.get(l.getSelectedIndex());
  276.                 switch(area.getShapeName()){
  277.                         case "rect":
  278.                                 ((RectangleArea)area).a.x = (Integer)rectAX.getValue();
  279.                                 ((RectangleArea)area).a.y = (Integer)rectAY.getValue();
  280.                                 ((RectangleArea)area).b.x = (Integer)rectBX.getValue();
  281.                                 ((RectangleArea)area).b.y = (Integer)rectBY.getValue();
  282.                         break;
  283.                         case "circle":
  284.                                 ((CircleArea)area).center.x = (Integer)circleCenterX.getValue();
  285.                                 ((CircleArea)area).center.y = (Integer)circleCenterY.getValue();
  286.                                 ((CircleArea)area).r = (Integer)circleRadius.getValue();
  287.                         break;
  288.                         case "poly":
  289.                                 ((PolygonArea)area).vertices.set(polyVertices.getSelectedIndex(), new Point((Integer)polyVertexX.getValue(), (Integer)polyVertexY.getValue()));
  290.                                 refreshPolyVertices();
  291.                         break;
  292.                 }
  293.                 refresh();
  294.         }
  295.  
  296.         public void changedUpdate(DocumentEvent e){labelEvent();}
  297.         public void removeUpdate(DocumentEvent e){labelEvent();}
  298.         public void insertUpdate(DocumentEvent e){labelEvent();}
  299.  
  300.         public void labelEvent(){
  301.                 int i = l.getSelectedIndex();
  302.                 if(i != -1){
  303.                         areaList.get(i).href = hrefTextField.getText();
  304.                         refresh();
  305.                 }
  306.         }
  307.  
  308.         private void refresh(){
  309.                 int i = l.getSelectedIndex();
  310.                 l.removeListSelectionListener(this);
  311.                 list.clear();
  312.                 for(Area a: areaList){
  313.                         list.addElement(a.getFullName());
  314.                 }
  315.                 l.addListSelectionListener(this);
  316.                 l.setSelectedIndex(i);
  317.                 refreshValues();
  318.                 imgPanel.repaint();
  319.         }
  320.        
  321.         private void refreshValues(){
  322.                 stateListenerOn = false;
  323.                 if(l.getSelectedIndex() != -1){
  324.                         Area area = areaList.get(l.getSelectedIndex());
  325.                         switch(area.getShapeName()){
  326.                                 case "rect":
  327.                                         rectAX.setValue(((RectangleArea)area).a.x);
  328.                                         rectAY.setValue(((RectangleArea)area).a.y);
  329.                                         rectBX.setValue(((RectangleArea)area).b.x);
  330.                                         rectBY.setValue(((RectangleArea)area).b.y);
  331.                                 break;
  332.                                 case "circle":
  333.                                         circleCenterX.setValue(((CircleArea)area).center.x);
  334.                                         circleCenterY.setValue(((CircleArea)area).center.y);
  335.                                         circleRadius.setValue(((CircleArea)area).r);
  336.                                 break;
  337.                                 case "poly":
  338.                                         refreshPolyVertices();
  339.                                         int vi = polyVertices.getSelectedIndex();
  340.                                         if(vi != -1){
  341.                                                 Point v = ((PolygonArea)area).vertices.get(vi);
  342.                                                 polyVertexX.setValue(v.x);
  343.                                                 polyVertexY.setValue(v.y);
  344.                                         }
  345.                                 break;
  346.                         }
  347.                 }
  348.                 stateListenerOn = true;
  349.         }
  350.  
  351.         private void refreshPolyVertices(){
  352.                 int i = polyVertices.getSelectedIndex();
  353.                 polyVertices.removeListSelectionListener(this);
  354.                 polyVerticesModel.clear();
  355.                 PolygonArea area = ((PolygonArea)areaList.get(l.getSelectedIndex()));
  356.                 for(Point v: area.vertices){
  357.                         polyVerticesModel.addElement(String.format("Vertex (%d, %d)", v.x, v.y));
  358.                 }
  359.                 polyVertices.addListSelectionListener(this);
  360.                 polyVertices.setSelectedIndex(i);
  361.         }
  362.  
  363.         public static void main(String[] args){
  364.                 EventQueue.invokeLater(new Runnable() {
  365.                         public void run(){
  366.                                 Main m = new Main();
  367.                                 m.setVisible(true);
  368.                                 m.loadImage();
  369.                         }
  370.                 });
  371.         }
  372. }