JApplet and Swing Components

Everything so far has used java.applet.Applet, part of the older AWT (Abstract Window Toolkit) UI library. When Swing arrived, it brought its own applet base class, javax.swing.JApplet, which extends Applet and adds a Swing content pane plus support for Swing components like JButton, JLabel, and a JMenuBar. This lesson introduces a second, standalone example - SwingPaintApplet - to show the idiomatic way to do custom drawing under JApplet, which is different enough from plain Applet painting to deserve its own careful example. PaintApplet itself is not changed in this lesson.

Why Not Just Override paint() Again?

On a plain Applet, overriding paint(Graphics g) directly, as PaintApplet does, is perfectly correct because AWT has a single, simple painting pipeline. Swing components, including JApplet, sit on top of a more elaborate painting system that also has to draw borders, layered panes, and child components correctly - overriding paint() directly on a JApplet the same way bypasses parts of that pipeline and commonly causes rendering bugs (components not repainting correctly, artifacts left behind after resizing, and so on).

The idiomatic fix is to keep the custom drawing out of the applet class entirely. Instead, you create a JPanel subclass that overrides paintComponent(Graphics g) - always calling super.paintComponent(g) first so Swing can do its own background/border painting before your drawing code runs - and then add that panel to the applet's content pane with getContentPane().add(panel).

Don't mix the two styles: on a JApplet, do your custom drawing in a JPanel's paintComponent(), added via the content pane - not by overriding paint() on the JApplet itself the way PaintApplet does on plain Applet.

SwingPaintApplet

Here's a small, separate example applying that pattern - note this is a new class, SwingPaintApplet, and does not replace or modify PaintApplet from earlier lessons:

import javax.swing.JApplet;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Color;

public class SwingPaintApplet extends JApplet {

    public void init() {
        JPanel canvas = new JPanel() {
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.BLUE);
                g.drawString("Welcome to SwingPaintApplet!", 20, 30);
            }
        };
        getContentPane().add(canvas);
    }
}
As with every other Java sample in this track, this is a preview meant for a local JDK 8 setup with appletviewer - this website's server runs PHP, not a Java runtime, so nothing here executes on the page itself.

Walking through it: init() builds an anonymous JPanel subclass ( canvas) whose paintComponent(Graphics g) is overridden to call super.paintComponent(g) first - letting JPanel paint its own background as it normally would - before setting the drawing color to blue and drawing the welcome string. getContentPane().add(canvas) then places that panel into the applet's Swing content pane, which is what JApplet adds on top of plain Applet: a dedicated container object that manages the applet's Swing children, rather than the applet drawing directly onto itself.

Applet + AWT vs. JApplet + Swing

AspectApplet (AWT)JApplet (Swing)
Component library AWT (Button, Label, TextField, ...) Swing (JButton, JLabel, JTextField, ...), plus JMenuBar support
Custom drawing method Override paint(Graphics g) directly on the applet, as PaintApplet does Override paintComponent(Graphics g) on a JPanel, calling super.paintComponent(g) first, then add it via the content pane
Container structure Applet draws directly; no separate content pane concept Adds a dedicated content pane (getContentPane()) that manages child components
Typical use, then Simpler graphics-only or lightly interactive applets Applets wanting a richer, more polished Swing widget set
Recap: JApplet extends Applet and adds a content pane plus Swing component and menu-bar support. For custom drawing in a JApplet, subclass JPanel, override paintComponent(Graphics g) (calling super.paintComponent(g) first), and add the panel with getContentPane().add(panel) - do not override paint() directly on the JApplet itself.
Next steps: Both PaintApplet and SwingPaintApplet have relied on a handful of Graphics method calls so far. The Graphics Class covers what that object can actually do in much more depth.