Your First Applet

It's time to write a complete, real applet. This lesson introduces PaintApplet, the running example this entire track builds on - later lessons add color, mouse-driven drawing, images, and more to this exact same class. For now it does two simple things: it sets a plain white background once, and it draws a welcome message and a border rectangle every time it's asked to redraw itself.

The Complete Code

import java.applet.Applet;
import java.awt.Graphics;

public class PaintApplet extends Applet {

    public void init() {
        setBackground(java.awt.Color.WHITE);
    }

    public void paint(Graphics g) {
        g.drawString("Welcome to PaintApplet!", 20, 30);
        g.drawRect(10, 10, getWidth() - 20, getHeight() - 20);
    }
}
As with every code sample in this track, treat this as a preview to read and study, not something you can execute on this page - codingwithsonu.com is a PHP site, not a Java runtime. Save the code as PaintApplet.java and run it locally with JDK 8's javac and appletviewer as covered in Environment Setup.

Line by Line

public class PaintApplet extends Applet - this single line is what makes PaintApplet an applet rather than an ordinary class. By extending java.applet.Applet, the class inherits an entire lifecycle contract (the init/start/paint/stop/destroy methods from the previous lesson) plus everything Applet itself inherits from Panel, Container, and ultimately Component - things like setBackground(), getWidth(), and getHeight() that PaintApplet uses below without defining them itself.

init() is overridden to call setBackground(java.awt.Color.WHITE). This runs exactly once, right after the applet is loaded, which is exactly right for a background color - there is no reason to set it again every time the applet redraws. This is the textbook use of init(): one-time setup that shouldn't be repeated.

paint(Graphics g) is where the actual drawing happens. Notice that PaintApplet never constructs a Graphics object itself - the g parameter is handed in by the AWT painting system every time it decides the applet needs to be redrawn (on first display, after being resized, after being uncovered by another window, and so on, as covered in The Applet Lifecycle). Your code never calls paint() directly; you only ever receive a call to it.

Inside paint(), g.drawString("Welcome to PaintApplet!", 20, 30) draws text with its baseline at the point (20, 30) - x increasing rightward, y increasing downward from the applet's top-left corner. g.drawRect(10, 10, getWidth() - 20, getHeight() - 20) then draws an unfilled rectangle starting 10 pixels in from the top-left corner, sized to stay 10 pixels clear of every edge of the applet regardless of how large the applet is. getWidth() and getHeight() are not defined on Applet or even Graphics - they come from java.awt.Component, one of Applet's ancestor classes, and simply report the current pixel size of the applet itself.

Using getWidth()/getHeight() instead of hardcoded numbers for the rectangle's size means the border correctly hugs the applet's edges even if the width/ height attributes in the HTML embedding change - you'll see exactly how those attributes work in the next lesson.

Where This Goes Next

PaintApplet is deliberately minimal right now - a background color and two drawing calls. The next lesson embeds this exact class into an HTML page using the classic (and, as you'll see, entirely non-functional in any modern browser) <applet> tag. Later lessons in this track then grow PaintApplet itself: custom colors and fonts, mouse-driven freehand drawing, keyboard shortcuts, and a background image, one capability per lesson, all built on the same init()/paint() structure you just wrote.

Recap: PaintApplet extends Applet, overrides init() for one-time background setup and paint(Graphics g) for drawing, and relies on inherited Component methods (setBackground, getWidth, getHeight) it never had to define itself.
Next steps: See how a browser (or, in practice, appletviewer) would have loaded PaintApplet from a real web page in Embedding in HTML.