The JavaTM Tutorial
Trail: Learning the Java Language
Lesson: Object-Oriented Programming Concepts

## Run the ClickMe Applets

> > Note: The applets on this page require JDK 1.1. If you are using an older browser that does not support 1.1, you won't be able to run the applets. Instead, you need to view this page in a 1.1 browser, such as HotJava, the JDK Applet Viewer (`appletviewer`), or certain versions of Netscape Navigator and Internet Explorer. For more information about running applets, refer to About Our Examples.

> ### `ClickMe`

> > This is the original applet. Click within the applet's bounds and the applet displays a red spot at the mouse click location.

> ### `ClickMeGreen`

> > This applet is a minor modification of the original. Besides the applet name, only the code that draws the spot changed. The following table shows the spot-drawing code from both versions of the applet.

> > Old Version | New Version
> > --- | ---
> >     g.setColor(Color.red);
> >     if (spot != null) {
> >         g.fillOval(spot.x - RADIUS,
> >                    spot.y - RADIUS,
> >                    RADIUS * 2, RADIUS * 2);
> >     }
> >     g.setColor(Color.green);
> >     if (spot != null) {
> >         g.fillRect(spot.x - RADIUS,
> >                    spot.y - RADIUS,
> >                    RADIUS * 2, RADIUS * 2);
> >     }

> ### `ClickMePurple`

> > This is another relatively minor modification of the original applet. The differences between the two versions are shown in red:

> > Old Version | New Version
> > --- | ---
> >     import java.applet.Applet;
> >     import java.awt.*;
> >     import java.awt.event.*;

> >     public class ClickMe
> >            extends Applet
> >            implements MouseListener
> >     {
> >         private Spot spot = null;
> >         private static final int RADIUS = 7;

> >         public void init() {
> >             addMouseListener(this);
> >         }

> >         public void paint(Graphics g) {
> >             // draw black border and white background
> >             ...

> >             // draw the spot
> >             g.setColor(Color.red);
> >             if (spot != null) {
> >                 g.fillOval(spot.x - RADIUS,
> >                            spot.y - RADIUS,
> >                            RADIUS * 2, RADIUS * 2);
> >             }
> >         }
> >         public void mousePressed(MouseEvent event) {
> >             if (spot == null) {
> >                 spot = new Spot(RADIUS);
> >             }
> >             spot.x = event.getX();
> >             spot.y = event.getY();
> >             repaint();
> >         }
> >         ...
> >     }
> >     import java.applet.Applet;
> >     import java.awt.*;
> >     import java.awt.event.*;

> >     public class ClickMePurple
> >            extends Applet
> >            implements MouseListener
> >     {
> >         private Spot spot = null;
> >         private Color purple = new Color(0.5f, 0.0f, 0.5f);

> >         public void init() {
> >             addMouseListener(this);
> >         }

> >         public void paint(Graphics g) {
> >             // draw black border and white background
> >             ...

> >             // draw the spot
> >             g.setColor(purple);
> >             if (spot != null) {

> >                 g.drawString("Mary", spot.x, spot.y);

> >             }
> >         }
> >         public void mousePressed(MouseEvent event) {
> >             if (spot == null) {
> >                 spot = new Spot(0);
> >             }
> >             spot.x = event.getX();
> >             spot.y = event.getY();
> >             repaint();
> >         }
> >         ...
> >     }

> ### `ClickMeErase`

> > The code for this applet differs from the original in the following ways:

> > - Adds another `Spot` object that tracks the spot's previous location.
> > - Adds an `update` method, which erases the old spot by drawing a white spot at the spot's previous location.
> > - Modifies the `paint` method to draw the white background and black border.
> > - Modifies the `mousePressed` method to store the spot's previous position before setting its new position.

> ### `ClickMeDrag`

> > We used the solution to the previous exercise, `ClickMeErase`, as the basis to our solution for this one. These two programs differ in the following ways:

> > - The applet now implements two interfaces: `MouseListener` and `MouseMotionListener`.
> > - The applet registers itself twice. Once as a mouse listener and once as a mouse motion listener.
> > - The applet provides two new methods. The first, `mouseDragged`, has the same implementation as the `mousePressed` method, it moves the spot and repaints the applet. The second, `mouseMoved`, has an empty implementation because the applet doesn't care about mouse moved events.