Previous | Next | Trail Map | Getting Started | The "Hello World" Applet


Implementing Applet Methods

The bold lines of the following listing implement the paint method.
import java.applet.Applet;
import java.awt.Graphics;

public class HelloWorld extends Applet {
    public void paint(Graphics g) {
        g.drawString("Hello world!", 50, 25);
    }
}

Every applet must implement one or more of the init, start, and paint methods. You'll learn about these methods in the Overview of Applets(in the Writing Applets trail) lesson.

Besides the init, start, and paint methods, applets can implement two more methods that the browser calls when a major event occurs (such as leaving the applet's page): stop and destroy. Applets can implement any number of other methods, as well.

Returning to the above code snippet, the Graphics object passed into the paint method represents the applet's onscreen drawing context. The first argument to the Graphics drawString method is the string to draw onscreen. The second and third arguments are the (x,y) position of the lower left corner of the text onscreen. This applet draws the string "Hello world!" starting at location (50,25). The applet's coordinate system starts at (0,0), which is at the upper left corner of the applet's display area. You'll learn all about drawing to the screen in the Creating a User Interface(in the Creating a User Interface trail) trail.


Previous | Next | Trail Map | Getting Started | The "Hello World" Applet