Note that there are some explanatory texts on larger screens.

plurals
  1. PORemembering where a mouse clicked? ArrayLists? HashCodes?
    primarykey
    data
    text
    <p>Sorry guys, I deleted my APPLES and CATS example :) Here's the updated version of my question!</p> <p>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.</p> <p>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.</p> <p><img src="https://i.stack.imgur.com/7oNzg.jpg" alt="enter image description here"></p> <p>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.</p> <p><img src="https://i.stack.imgur.com/9iVWK.jpg" alt="enter image description here"></p> <p>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.</p> <p><img src="https://i.stack.imgur.com/WDcTU.jpg" alt="enter image description here"></p> <p>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.</p> <p>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.</p> <p>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!</p> <p>How I'm setting the text on JLabel and updating the JList:</p> <pre><code>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); } </code></pre> <p>My mouseListener upon click:</p> <pre><code>@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) &amp;&amp; !input.isEmpty()){ tagModel.addElement(input); } } </code></pre> <p>My mouseMotionListener upon hovering:</p> <pre><code>@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 &gt; x &amp;&amp; xpos &lt; x + 100 &amp;&amp; ypos &gt; y &amp;&amp; ypos &lt; y + 100) isRectPresent = false; if(e.getSource() == hoverLabel){ isRectPresent = true; repaint(); } repaint(); } </code></pre> <p>How I'm painting:</p> <pre><code> public void paintComponent(Graphics g){ Graphics2D g2 = (Graphics2D) g; g2.drawImage(image, 0, 0, null); if(image != null &amp;&amp; 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 &gt; x &amp;&amp; xpos &lt; x + 100 &amp;&amp; ypos &gt; y &amp;&amp; ypos &lt; 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); } } } </code></pre> <p>If you want me to add some more snippets, just tell me! :)</p>
    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.
 

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