Download the JDK
Search the Tutorials
Hide the TOC
Trail: Deployment
Lesson: Applets
Section: Getting Started with Applets
Applets
Getting Started with Applets
Importing Classes and Packages for Applets
Defining an Applet Subclass
The Life Cycle of an Applet
Methods for Milestones
Methods for Drawing and Event Handling
Methods for Adding UI Components
What Applets Can and Can't Do
Taking Advantage of the Applet API
Finding and Loading Data Files
Displaying Short Status Strings
Displaying Documents in the Browser
Sending Messages to Other Applets
Playing Sounds
Defining and Using Applet Parameters
Deciding Which Parameters to Support
Writing the Code to Support Parameters
Giving Information about Parameters
Using the APPLET Tag
Practical Considerations When Writing Applets
Security Restrictions
Creating a User Interface
Creating a GUI
Displaying Diagnostics to the Standard Output and Error Streams
Getting System Properties
Threads in Applets
Threads in Applets: Examples
Working with a Server-Side Application
A Simple Network Client Applet
Using a Server to Work Around Security Restrictions
Finishing an Applet
Before You Ship that Applet
The Perfectly Finished Applet
Deploying Applets
General Deployment Considerations
Using the applet Tag
Using the object Tag
Using the embed Tag
Deploying Applets in a Mixed-Browser Environment
Solving Common Applet Problems
Questions and Exercises
Home Page > Deployment > Applets
Methods for Drawing and Event Handling

### Using the Paint Method

> To draw the applet's representation within a browser page, you use the `paint` method.

> For example, the `Simple` applet defines its onscreen appearance by overriding the `paint` method:

> >     public void paint(Graphics g) {
> >     	//Draw a Rectangle around the applet's display area.
> >             g.drawRect(0, 0,
> >     		   getWidth() - 1,
> >     		   getHeight() - 1);

> >     	//Draw the current string inside the rectangle.
> >             g.drawString(buffer.toString(), 5, 15);
> >         }

> Applets inherit the `paint` method from the Abstract Window Toolkit (AWT) `Container` class.

### Handling Events

> Applets inherit a group of event-handling methods from the `Container` class.

> The `Container` class defines several methods, such as `processKeyEvent` and `processMouseEvent`, for handling particular types of events, and then one catch-all method called `processEvent`.

> To react to an event, an applet must override the appropriate event-specific method. For example, the following program, `SimpleClick`, implements a `MouseListener` and overrides the `mouseClicked` method.

> >     /*
> >      * Java(TM) SE 6 version.
> >      */

> >     import java.awt.event.MouseListener;
> >     import java.awt.event.MouseEvent;
> >     import java.applet.Applet;
> >     import java.awt.Graphics;

> >     //No need to extend JApplet, since we don't add any components;
> >     //we just paint.
> >     public class SimpleClick extends Applet
> >     			 implements MouseListener {

> >         StringBuffer buffer;

> >         public void init() {
> >     	addMouseListener(this);
> >     	buffer = new StringBuffer();
> >             addItem("initializing... ");
> >         }

> >         public void start() {
> >             addItem("starting... ");
> >         }

> >         public void stop() {
> >             addItem("stopping... ");
> >         }

> >         public void destroy() {
> >             addItem("preparing for unloading...");
> >         }

> >         void addItem(String newWord) {
> >             System.out.println(newWord);
> >             buffer.append(newWord);
> >             repaint();
> >         }

> >         public void paint(Graphics g) {
> >     	//Draw a Rectangle around the applet's display area.
> >             g.drawRect(0, 0,
> >     		   getWidth() - 1,
> >     		   getHeight() - 1);

> >     	//Draw the current string inside the rectangle.
> >             g.drawString(buffer.toString(), 5, 15);
> >         }

> >         //The following empty methods could be removed
> >         //by implementing a MouseAdapter (usually done
> >         //using an inner class).
> >         public void mouseEntered(MouseEvent event) {
> >         }
> >         public void mouseExited(MouseEvent event) {
> >         }
> >         public void mousePressed(MouseEvent event) {
> >         }
> >         public void mouseReleased(MouseEvent event) {
> >         }

> >         public void mouseClicked(MouseEvent event) {
> >     	addItem("click!... ");
> >         }
> >     }

> Below is the resulting applet. When you click within its rectangle, it displays the word "click!...".

> > Note: If you don't see the applet running above, you need to install Java Plug-in, which happens automatically when you install the Java(TM) SE JRE or JDK. This applet requires JDK 1.4 or later. You can find more information on the Java Plug-in home page.

Previous page: Methods for Milestones
Next page: Methods for Adding UI Components