根据mouseClicked Point在drawline上的偏差位置

时间:2013-03-28 18:34:14

标签: java swing jpanel java-2d mouselistener

在JFrame上有JPanel后,JPanel如下 -

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);
    }

}

我的问题是drawline绘制线条与原始组件上的实际点击点位置略有偏差。

我附上一些屏幕截图,清除了更多 -

上线边缘是我点击它们的2个点,下面的线条是从drawline接收到的线条,正如您所看到的那样,这两条线条不重叠。

点击1 -

enter image description here

点击2 -

enter image description here

输出行 -

enter image description here

任何有用的解决方案?

0 个答案:

没有答案