我正在尝试获取绘图部分中的光标点并简单地绘制一个椭圆形。但运气不好!
public void paint(Graphics g){
Point ComponentPoint = PaintPanel.getLocationOnScreen();
Point CursorPoint= MouseInfo.getPointerInfo().getLocation(); //gets cursor point
int ComPX = ComponentPoint.x;
int ComPY = ComponentPoint.y;
int CurPX = CursorPoint.x;
int CurPY = CursorPoint.y;
int FinalX = CurPX - ComPX;
int FinalY = CurPY - ComPY;
g.drawOval(FinalX, FinalY, 20, 20);
}
private void PaintPanelMouseDragged(java.awt.event.MouseEvent evt) {
//when mouse is moved over paintpanel
//PaintPanel.repaint();
not working
}
这是没有paint方法的图像:
最佳答案
您不能在 Paint 方法中添加这样的代码。您不会在paint 方法中引用MouseInfo 类,因为您无法控制何时调用paint() 方法。您应该使用 MouseListener 和 MouseMotionListner 来进行自定义绘画。另外,自定义绘画不应该在paint方法中完成。
参见Custom Painting Approaches两种解决方案。
关于java - 小程序绘画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15593871/