Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6890789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:19:36+00:00 2026-05-27T06:19:36+00:00

Sorry guys, I deleted my APPLES and CATS example :) Here’s the updated version

  • 0

Sorry guys, I deleted my APPLES and CATS example 🙂 Here’s the updated version of my question!

I’m losing my sanity here. I need someone who can enlighten me. I’ve tried a couple of times explaining my problem here. Hopefully, this time, my question will be easier to understand.

Basically I have this frame, and there’s an image displayed. There is a JList on the right, and there is another panel for JLabels at the bottom. Here’s a screencap of my frame.

enter image description here

When I click on the image, a JOptionPane pops out, like so. And I enter my input. My JList is an ArrayList, so everything I input is added to the JList and the JPanel at the bottom.

enter image description here

Now, when I hover on the the part where I clicked, you noticed that the square disappeared). It only appears when I click the image, and when I hover the label at the bottom. My labels, as of now are LOLZ NOSE and INPUT HERE.

enter image description here

What I want to do is when I hover on the label, for example INPUT HERE, it shows the square again, featuring the part where I clicked. My problem now is when I click on NOSE, which is supposed to be showing a square on the nose part and a the name NOSE with black bg, IT IS NOT SHOWING. Also, only the last label’s square is shown, disregarding the other labels’ position clicked.

How do I get a label to remember the position of the click I make? People said I should use ArrayLists or HashCodes however I have no idea how to implement them. Thank you to anyone who can help.

Edit: I’ve already done the rectangle, btw. It’s showing only for the last label inputted. Here are some of the code snippets requested!

How I’m setting the text on JLabel and updating the JList:

public void updateLabel(){

        StringBuilder text = new StringBuilder(); //creates empty builder, capacity 16

        for(Object s: tagModel.toArray()) //returns an array containing the elements of the tagModel
            text.append(" " + s);

        repaint();      
        hoverLabel.setText(text.toString()); //returns a String
        hoverLabel.addMouseMotionListener(this);
        hoverPanel.add(hoverLabel);

    }

My mouseListener upon click:

@Override
    public void mouseClicked(MouseEvent event) {
        // TODO Auto-generated method stub

        x = event.getX();
        y = event.getY();

        isRectPresent = true;
        repaint();

        input = JOptionPane.showInputDialog("Enter tag name:");

        if((input != null) && !input.isEmpty()){
            tagModel.addElement(input);
        }
    }

My mouseMotionListener upon hovering:

@Override
    public void mouseMoved(MouseEvent e) {
        // TODO Auto-generated method stub

        xpos = e.getX(); //gets where the mouse moved
        ypos = e.getY(); 

        //checks if the mouse is inside the bounds of the rectangle
        if (xpos > x && xpos < x + 100 && ypos > y && ypos < y + 100)
            isRectPresent = false;

        if(e.getSource() == hoverLabel){
            isRectPresent = true;
            repaint();
        }

        repaint();
    }

How I’m painting:

    public void paintComponent(Graphics g){ 
            Graphics2D g2 = (Graphics2D) g;

            g2.drawImage(image, 0, 0, null);

            if(image != null && isRectPresent){                 
                            Stroke stroke = g2.getStroke();
                g2.setStroke(new BasicStroke(4));
                g2.setColor(Color.WHITE);
                g2.drawRect(x-50, y-50, 100, 100);
                g2.setStroke(stroke);
            }else{
                if(xpos > x && xpos < x + 100 && ypos > y && ypos < y + 100){
                    g.setColor(Color.BLACK);
                    g.fillRect(x-50, y-50, 100, 25);
                    g.setColor(Color.WHITE);
                    g.setFont(new Font("Tahoma", Font.BOLD, 12));
                    g.drawString(input, x-30, y-30);
                }
            }
        }

If you want me to add some more snippets, just tell me! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:19:37+00:00Added an answer on May 27, 2026 at 6:19 am

    You should create a HashMap, say something like:

    Map linkSet = new HashMap();

    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 get(...) method.

    edit:
    Corrected as per alicedimarco’s comment. Again, thanks!

    edit 2
    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:

    public void setDisplayPoint(Point p) {
      this.displayPoint  = p;
      repaint();
    }
    

    and assuming that the object of interest is centered at that point, use the displayPoint in the paintComponent method:

    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);
      }
    }
    

    edit 3:
    To get mouse clicks, it’s quite easy, simply add a MouseListener to the component that holds the image:

         // !! added
         imgRect.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
               imgMousePressed(e);
            }
         }); 
    

    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:

    // !! 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);
      }
    }
    

    That’s it.

    Then putting it all together:

    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<String, Point> stringPointMap = new HashMap<String, Point>();
    
       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 = "https://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);
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

sorry guys, asked a bit wrongly, here is the EDIT got a simple question,
First of all sorry guys for posting the question here. The support forum seems
I could be not more sorry guys - the question was a very wrong
Hello guys sorry if the question looks stupid to you. i have 3 tables
Sorry guys, it's another CSS height 100% (sortof) question... I have a layout like
Sorry guys, it seems like I didn't explain my question clearly. Please allow me
Sorry guys i couldn't provide feedback because of my other username and password here,
Sorry guys if this is a lame question, but the examples i found dont
Sorry guys, if this is a stupid question. I have a set of integers
Sorry guys, I posted this earlier today where my add as linked images were

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.