2013-03-24 79 views
0

我试图在绘画部分获得光标的点,并只画一个椭圆。虽然没有运气!小程序绘画

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

+0

考虑创建并发布一个小的*几乎*工作的例子,你最好的尝试解决方案,换句话说,[sscce](http://sscce.org)。 – 2013-03-24 00:51:20

+0

随着我发布它没有显示任何东西:/我尝试了别的东西,它摆脱了一切,但画了椭圆形。 – bob 2013-03-24 00:57:21

+0

很难知道如何在没有上下文的情况下发布一小段代码。这就是为什么我要求你通过创建一个非常小但可编译和可运行的程序来提供一些上下文。 – 2013-03-24 00:59:11

回答

1

在这样的paint方法不能添加代码。 YOu不会在paint方法中引用MouseInfo类,因为您无法控制何时调用paint()方法。您应该使用MouseListener和MouseMotionListner进行自定义绘画。另外,自定义绘画不应该在绘画方法中完成。

请参阅Custom Painting Approaches两种解决方案。

+0

和[如何编写鼠标监听器](http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html) – MadProgrammer 2013-03-24 01:31:40