我试图在绘制部分中获取光标的位置,然后简单地绘制一个椭圆形。不过没有运气!
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
}
这是没有绘画方法的图像:
http://i.stack.imgur.com/VOyhr.png
最佳答案
您不能像这样在paint方法中添加代码。因为您无法控制何时调用paint()方法,所以您不会在paint方法中引用MouseInfo类。您应该使用MouseListener和MouseMotionListner进行自定义绘制。另外,自定义绘画不应使用paint方法进行。
有关两种解决方案,请参见Custom Painting Approaches。
关于java - 小程序绘画,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15593871/