Having a JPanel
on a JFrame , the JPanel
is as the follow -
public class MyPanel extends JPanel implements MouseListener,
MouseMotionListener {
...
@Override
public void mouseClicked(MouseEvent arg) {
...
Point p = arg.getPoint();
// save this point ...
...
repaint();
}
@Override
protected void paintComponent(Graphics g) {
...
// point1 & point2 are the last two clicked point ..
super.paintComponent(g);
int x1 = point1.x;
int y1 = point1.y;
int x2 = point2.x;
int y2 = point2.y;
g.drawLine(x1, y1, x2, y2);
}
}
My problem is that the drawline
paint the line with a slightly deviation from the real clicked point location on the originator component .
I attach some screenshots which clear it some more -
The upper line edges are the 2 points I clicked on them , the lower line is the line received from the drawline
, and as you can see these 2 line doesn't overlap .
click 1 -
click 2 -
output line -
Any helpful solution ?