I have written the following micro-paintbrush program in Java:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.a
I tried your code. I think your problem comes from the fact that you paint on the t3_aux1 using t3_aux2 coordinates. I'll try something to confirm that, and I come back here ...
EDIT: OK, that's it.
in t3_aux1 constructor, if you write
System.out.println("panel1 height = " + panel1.getHeight());
System.out.println("label1 height = " + label1_x.getHeight())
it prints
panel1 height = 42
label1 height = 20
So your offset is 42 + 20 + 4*2 = 70
4*2 comes from your lines borders with a thickness of 2.
Since it's possible to calculate the exact offset, you can dynamically fix it.
EDIT 2 :
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.
writing this should fix your coordinates problem.
inst2.paintComponent(panel2.getGraphics());
But as Kleopatra said, you're not doing it the right way. You should never call getGraphics()
or paintComponent()
. I think you should consider using java.awt.Canvas
as a super class for your "panel2" object.
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.