Credits

Tuesday, March 16, 2010

Another Java Graphics Program

As indicated in the previous post, we can write our own paint() inside an Applet's program. When we do this we create an object of Graphics class which we can use for our graphics operations. 


The paint() is actually called whenever we run our applet, resize, minimize and restore the applet page.  So, basically, this method works in close hand with other applet's methods.   However, paint() is not invoked directly. It is actually called whenever we use repaint() which calls for update() and in turn, calls for paint(). 


To see the application of this idea, check the sample program below.

[+/-] show/hide



/* sample program on graphics */

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class GraphicsDemoPaint extends Applet implements ActionListener{
Button pressButton = new Button ("Press");
public void init(){
add(pressButton);
pressButton.addActionListener(this);
}

public void paint(Graphics g){
System.out.println("in paint method");
}

public void actionPerformed(ActionEvent e)
{
repaint();
}
}

AddThis