5. Standard-methods in the applet

So far, every applet we have seen had a method paint. We saw that the paint method does all the drawing in the applet. This method will not only run when the applet is started, but also every time the applet window is changed.

In the following applet (HowOftenDoesPaintRun) the variable inPaint is used to keep tabs of the number of times paint actually runs. Just compile the following applet and start it in Appletviewer, and then change the size of the applet window. Every time a change takes place, inPaint is increased by 1. As we saw before, the following two statements are identical:

inPaint++;                    // The value of inPaint
inPaint = inPaint + 1;   // is increased by 1.
Also minimizing and maximizing the window will increase inPaint.

You can also see that a long statement (with drawString, in the 8th and 9th lines) can be broken up into several lines. Hopefully, the program will need no further explanation.
// When does the method paint run?
import java.applet.*;
import java.awt.*;
public class HowOftenDoesPaintRun extends Applet {
    int inPaint = 0;
    public void paint (Graphics g) {
        g.drawString ("Paint has run " + inPaint
                                + " times.", 30, 100);

There are other methods which are automatically invoked in the life of an applet. They are:
  • init()       called once at the start of the applet
  • start()     called at every start or restart of the applet
  • stop()     called when the applet is stopped (also temporarily)
  • paint()    called when the applet window is (re)painted
  • destroy()called only once when the applet is finished
No matter where we place these methods, they are always invoked by the events described above. In the following program (which doesn't really do anything useful) you see these five methods. In the methods a counter will be increased by 10000, 1000, 100, 10 en 1. In that way the separate digits of the counter will show us which method has run and how often.
import java.applet.*;
import java.awt.*;
public class InitStartEtc extends Applet {
int counter=0;

public void init() {
    counter += 10000;

public void start() {
    counter += 1000;

public void stop() {
    counter += 100;

public void paint (Graphics g) {
counter += 10;
g.drawString ("" + counter, 40, 30);
g.drawString ("Position 1: init", 40, 85);
g.drawString ("Position 2: start", 40, 120);
g.drawString ("Position 3: stop", 40, 155);
g.drawString ("Position 4: paint", 40, 190);
g.drawString ("Position 5: destroy",40,225);
public void destroy() {
    counter += 1;

This applet has one problem: if any counter exceeds the value 9, the applet does not work correctly any more.

Global and local variables
If you declare a variable in the beginning of the applet (outiside the methods, also called: globally), its value will be known in every part of the applet. In this program the variable counter is declared globally, so its value can be changed by each of the methods in the applets.

If you use a variable in only one method, you should declare it in that method only (locally). In that way you make it easier to reuse the method in other programs.

An added advantage is that you avoid the risk of unintentionally changing the value of another variable, which accidentally has the same name.

Exercise 5.1
Make an applet with the methods discussed above (init(), start() etc.) When any of these methods runs, the applet should add the name of the method that has run to the end of that string, and drawString should show this string, so that we can see which methods have run. Strings can easily be extended by means of the plus-operator, for instance like this:
String firstpart = "Shake";
String lastpart = "speare";
String name = firstpart + lastpart;

By the way, do you think it will be possible to show in this way that destroy has run?

chapter 6

Main Menu Java Tutorial

To home page

(c) 2005, Thomas J.H. Luif