小程序绘画

时间:2013-03-24 00:48:29

标签: java

我试图在光泽部分中获取光标的点,然后简单地绘制一个椭圆。虽然没有运气!

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

1 个答案:

答案 0 :(得分:1)

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

有关两种解决方案,请参阅Custom Painting Approaches