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(in the Learning the Java Language trail).

ClickMe(in a .java source file)

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

ClickMeGreen(in a .java source file)

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(in a .java source file)

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();
    }
    ...
}

Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.