Chapter 16

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 16-1 and 16-2 show you a very simple Java applet. The applet displays the words Java For Dummies inside a rectangular box. (See Figure 16-1.)

Listing 16-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 16-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 ...

Get Java For Dummies, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.