我有一个简单的绘图应用程序,我试图理解绘制方法是如何工作的,该方法有一个类型为Graphics的参数,
public void paint( Graphics g ) {
g.fillOval(x, y,20, 20);
}
我的问题是这个Graphic对象来自哪里?
这是完整的代码:
public class Painter extends JFrame {
private int x = -10, y = -10;
public Painter()
{
super( "Simple Painter" );setSize( 500, 500 );setVisible( true );
addMouseMotionListener(new MyMouseWatcher());
}
@Override
public void paint( Graphics g ) {
g.fillOval(x, y,20, 20);
}
private class MyMouseWatcher extends MouseAdapter{
public void mouseDragged( MouseEvent event ){
x = event.getX();
y = event.getY();
repaint();
}
}
public static void main( String args[] )
{
Painter painter = new Painter();
painter.addWindowListener( new WindowAdapter(){
public void windowClosing( WindowEvent event )
{System.exit( 0 );}
}/* end inner class*/ );
}}