在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 -
点击2 -
输出行 -
任何有用的解决方案?