0

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 -

enter image description here

click 2 -

enter image description here

output line -

enter image description here

Any helpful solution ?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
URL87
  • 10,667
  • 35
  • 107
  • 174
  • 1
    The clicked points must have been defined w.r.t the `JFrame` not the `JPanel`, as you can see that the vertical distance between the two parallel lines is equal to the width of the titlebar. – Extreme Coders Mar 28 '13 at 18:46
  • Great ! I just set the `addMouseListener` according to the `JPanel` and it fixed ! – URL87 Mar 28 '13 at 19:09

0 Answers0