go to previous page   go to home page   go to next page


No. The Web browser is the main application. An applet is an object that the web browser uses.

Example JApplet

Not all browsers can run applets. If you see this, yours can not. You should be able to continue reading these lessons, however.

An applet is an object that is used by another program, typically a Web browser. The Web browser is the application program and (at least conceptually) holds the main() method. The applet part of a Web page provides services (methods) to the browser when the browser asks for them.

An applet object has many instance variables and methods. Most of these are defined in the JApplet class.

To access these definitions, your program should import javax.applet.JApplet and java.awt.*. Here is the code for a small applet. The name of the class is JustOneCircle and the name of the source file is JustOneCircle.java.

import javax.swing.JApplet;
import java.awt.*;

// assume that the drawing area is 150 by 150
public class JustOneCircle extends JApplet
  final int radius = 25;

  public void paint ( Graphics gr )
    gr.setColor( Color.white );
    gr.fillRect( 0, 0, 150, 150 );
    gr.setColor( Color.black );

    gr.drawOval( (150/2 - radius), (150/2 - radius), radius*2, radius*2 );

The applet is compiled just like an application using javac JustOneCircle.java. This produces a bytecode file called JustOneCircle.class that can be used in a Web page. The applet is responsible for the white rectangle containing a circle. How it does this will be explained later.

The size of the rectangle is 150 pixels wide by 150 pixels high. This size is encoded both in the source code for the applet (above) and in the HTML that describes the entire Web page. Details later.

Note: If you don't see the white rectangle you may be using a browser that has applets disabled. Sometimes this is done for security reasons. Also, some firewalls block applets.


Look at the code for the class. How many methods does it define?