Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should create a HashMap, say something like: </p> <p>Map linkSet = new HashMap();</p> <p>And whenever you click on the drawing and create a label, add the JLabel and the point on the image to the set using the put method with the JLabel as the key and the Point as the value. Then in the JLabel's MouseMotionListener, use your label as a key and obtain the corresponding point from the set using the map's <code>get(...)</code> method.</p> <p><em><strong>edit:</em></strong><br> Corrected as per alicedimarco's comment. Again, thanks!</p> <p><em><strong>edit 2</em></strong><br> I think you want again to use a Map. If you have a Map, you can have it retrieve the Point of interest from the JLabel's or the JList's String, and then pass this Point to the class that's drawing the image and let it use the Point to draw a rectangle. For instance you could give the image drawing class a Point field called displayPoint, and a method called setDisplayPoint(Point p). It can be as simple as this:</p> <pre><code>public void setDisplayPoint(Point p) { this.displayPoint = p; repaint(); } </code></pre> <p>and assuming that the object of interest is centered at that point, use the displayPoint in the paintComponent method:</p> <pre><code>protected void paintComponent(Graphics g) { super.paintComponent(g); // draw image if (img != null) { g.drawImage(img, X_SHIFT, Y_SHIFT, null); } // if displayPoint not null, draw the surrounding rectangle if (displayPoint != null) { g.setColor(RECT_COLOR); int x = displayPoint.x - RECT_WIDTH / 2; int y = displayPoint.y - RECT_WIDTH / 2; int width = RECT_WIDTH; int height = RECT_WIDTH; g.drawRect(x, y, width, height); } } </code></pre> <p><em><strong>edit 3:</em></strong><br> To get mouse clicks, it's quite easy, simply add a MouseListener to the component that holds the image:</p> <pre><code> // !! added imgRect.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { imgMousePressed(e); } }); </code></pre> <p>And in your code that is called from this mouse listener, use a JOptionPane to get the user's choice of tag name, and add the resulting String to both the listDataModel so that it is seen in the JList and also in the stringPointMap together with the Point obtained from the MouseEvent so that you can map the String to the Point and be able to retrieve it:</p> <pre><code>// !! added private void imgMousePressed(MouseEvent e) { String result = JOptionPane.showInputDialog(this, "Please enter name for this point on image:"); if (result != null) { stringPointMap.put(result, e.getPoint()); listDataModel.addElement(result); } } </code></pre> <p>That's it.</p> <p>Then putting it all together: </p> <pre><code>import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; import javax.swing.*; @SuppressWarnings("serial") public class ImageRectMain extends JPanel { private ImageRect imgRect; private DefaultListModel listDataModel = new DefaultListModel(); private JList list = new JList(listDataModel); private Map&lt;String, Point&gt; stringPointMap = new HashMap&lt;String, Point&gt;(); public ImageRectMain() { String nose = "Nose"; String ear = "Ear"; String rightEye = "Right Eye"; String leftEye = "Left Eye"; listDataModel.addElement(ear); listDataModel.addElement(nose); listDataModel.addElement(rightEye); listDataModel.addElement(leftEye); stringPointMap.put(nose, new Point(480, 500)); stringPointMap.put(ear, new Point(270, 230)); stringPointMap.put(rightEye, new Point(380, 390)); stringPointMap.put(leftEye, new Point(662, 440)); MouseAdapter listMouseAdapter = new MouseAdapter() { @Override public void mouseMoved(MouseEvent e) { listMouseMoved(e); } @Override public void mouseExited(MouseEvent e) { listMouseExited(e); } }; list.addMouseMotionListener(listMouseAdapter); list.addMouseListener(listMouseAdapter); try { imgRect = new ImageRect(); // !! added imgRect.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { imgMousePressed(e); } }); JPanel eastPanel = new JPanel(); eastPanel.setLayout(new BoxLayout(eastPanel, BoxLayout.PAGE_AXIS)); eastPanel.add(new JLabel("You have tagged the following:")); eastPanel.add(new JScrollPane(list)); eastPanel.add(Box.createVerticalGlue()); eastPanel.add(Box.createVerticalGlue()); eastPanel.add(Box.createVerticalGlue()); eastPanel.add(Box.createVerticalGlue()); setLayout(new BorderLayout()); add(imgRect, BorderLayout.CENTER); add(eastPanel, BorderLayout.EAST); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } // !! added private void imgMousePressed(MouseEvent e) { String result = JOptionPane.showInputDialog(this, "Please enter name for this point on image:"); if (result != null) { stringPointMap.put(result, e.getPoint()); listDataModel.addElement(result); } } private void listMouseExited(MouseEvent e) { imgRect.setDisplayPoint(null); } private void listMouseMoved(MouseEvent e) { int index = list.locationToIndex(e.getPoint()); Object value = listDataModel.get(index); if (value != null) { Point point = stringPointMap.get(value.toString()); if (point != null) { imgRect.setDisplayPoint(point); } } } private static void createAndShowGui() { JFrame frame = new JFrame("ImageRectMain"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new ImageRectMain()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } } @SuppressWarnings("serial") class ImageRect extends JPanel { public static final String IMAGE_PATH = "http://i.stack.imgur.com/7oNzg.jpg"; private static final int DEFAULT_W = 687; private static final int DEFAULT_H = 636; private static final int X_SHIFT = -6; private static final int Y_SHIFT = -26; private static final Color RECT_COLOR = Color.pink; private static final int RECT_WIDTH = 40; private BufferedImage img; private Point displayPoint = null; public ImageRect() throws MalformedURLException, IOException { img = ImageIO.read(new URL(IMAGE_PATH)); } public void setDisplayPoint(Point p) { this.displayPoint = p; repaint(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); if (img != null) { g.drawImage(img, X_SHIFT, Y_SHIFT, null); } if (displayPoint != null) { g.setColor(RECT_COLOR); int x = displayPoint.x - RECT_WIDTH / 2; int y = displayPoint.y - RECT_WIDTH / 2; int width = RECT_WIDTH; int height = RECT_WIDTH; g.drawRect(x, y, width, height); } } @Override public Dimension getPreferredSize() { return new Dimension(DEFAULT_W, DEFAULT_H); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload