Chapter 15

Writing Java Applets

In This Chapter

arrow Creating a simple applet

arrow Building applet animation

arrow Putting buttons (and other such things) on an applet

With Java’s first big burst onto the scene in 1995, the thing that made the language so popular was the notion of an applet. An applet is a Java program that sits inside a web browser window. The applet has its own rectangular area on a web page. The applet can display a drawing, show an image, make a figure move, respond to information from the user, and do all kinds of interesting things. When you put a real, live computer program on a web page, you open up a world of possibilities.

Applets 101

Listings 15-1 and 15-2 show you a very simple Java applet. The applet displays the words Java For Dummies inside a rectangular box. (See Figure 15-1.)

Listing 15-1: An Applet

import javax.swing.JApplet;

public class SimpleApplet extends JApplet {

private static final long serialVersionUID = 1L;

public void init() {

setContentPane(new DummiesPanel());

}

}

Listing 15-2: Some Helper Code for the Applet

import javax.swing.JPanel;

import java.awt.Font;

import java.awt.Graphics;

class DummiesPanel extends JPanel {

private static final long ...

Get Java For Dummies, 5th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.