E.S
E.S

Reputation: 101

need explaination for how swing method paint() works?

I have a simple paint application, I'm trying to understand how paint method works, the method has an arguments of type Graphics,

public void paint( Graphics g ) { 
    g.fillOval(x, y,20, 20);
}

my question is from where is this Graphic object g coming from?

this is the full code:

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*/ );
}}    

Upvotes: 0

Views: 160

Answers (1)

camickr
camickr

Reputation: 324118

Read the Swing tutorial on Custom Painting for an explanation on how painting works.

Hint, you should NOT be overriding paint() and you should NOT be doing custom painting on a JFrame.

Upvotes: 2

Related Questions