Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I tried your code. I think your problem comes from the fact that you paint on the <strong>t3_aux1</strong> using <strong>t3_aux2</strong> coordinates. I'll try something to confirm that, and I come back here ...</p> <p><strong>EDIT:</strong> OK, that's it.</p> <p>in <strong>t3_aux1</strong> constructor, if you write </p> <pre><code>System.out.println("panel1 height = " + panel1.getHeight()); System.out.println("label1 height = " + label1_x.getHeight()) </code></pre> <p>it prints </p> <pre><code>panel1 height = 42 label1 height = 20 </code></pre> <p>So your offset is 42 + 20 + 4*2 = 70</p> <p>4*2 comes from your lines borders with a thickness of 2.</p> <p>Since it's possible to calculate the exact offset, you can dynamically fix it.</p> <p><strong>EDIT 2 :</strong></p> <p>In fact, the coordinates you uses come from panel2 since the mouseListener is attached to panel2. But you draw on the JFrame Graphics, not on panel2 graphics.</p> <p>writing this should fix your coordinates problem.</p> <pre><code>inst2.paintComponent(panel2.getGraphics()); </code></pre> <p>But as Kleopatra said, you're not doing it the right way. You should never call <code>getGraphics()</code> or <code>paintComponent()</code>. I think you should consider using <code>java.awt.Canvas</code> as a super class for your "panel2" object.</p> <p>One more advice : Be aware that your drawings aren't memorized, so, if you reduce the window or hide it behind another one, everything drawn will be lost.</p>
 

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