Lab 10
Java Applets

Java is most well known for its use with network browsers such as Netscape or Internet Explorer. These Java programs are called applets. Applets are initiated from a tag in the web page (the html file).

Java applets are quite similar to Java applications but do have some important distinctions.

For this lab, you will complete an applet that implements a simple game that counts how many times the user clicks on a target. The target will be a picture of your lab insructor!

Work to Complete

The code that is provided displays a picture of a famous person amd 'moves' the picture inside a panel.

When a mouse is clicked anywhere inside the panel, a count is updated and displayed.

You will replace the picture with a picture of your lab instructor, cause the picture to appear at random positions within the panel, and determine if a player clicks on the picture.

Your code should then update the count if the player clicked the picture (and only then).

You can shorten the amount of time that the picture stays in one place to challenge the player.

Goals for Lab 10

Files for this lab:

The following files will be used for this lab: Compile the .java files.

Getting Applets Started

Applets are initiated from within a web page (or by an appletviewer). So, an html file must be provided. Below is the html file for an applet.

The APPLET tag provides the name of the Java class file containing the applet code.

< HTML >

< HEAD >
< TITLE >  Applet </TITLE>
< /HEAD >

< BODY >
< APPLET width=500 height=500 code="CatchTheTA.class"  >
< /BODY >
< /HTML >

To run this applet, we could use the Java appletviewer (assuming the html is stored in a file named Animation.html):

 appletviewer Animation.html
Try running the applet.

Applets vs. Applications

We have just mentioned one distinction between applets and applications. Applets are initiated by a browser or an appletviewer. Applications are run using java.

In terms of coding, there are other important differences. Two of these are


For our applet, the paintComponent() method in causes the image to be displayed. Like paint(), it is never called directly. Instead, it is invoked when we repaint().


The applet for this lab contains a panel with an image (a .gif file ) displayed at regular intervals. The image is displayed at different locations to give a sense of movement. This provides an animation effect.

To create the necessary pause between displays, the Timer object is used to trigger an event at regular intervals. The length of an interval is specified as an integer quantity of milliseconds (1000 milliseconds per second).

Below is code from today's lab with some explanations:

...from private final int DELAY =100; private Timer timer; timer = new Timer (DELAY, null); Create a timer object using .1 second interval AnimationPanel (timer,totalHits) Give the timer object to the panel timer.start();Start the timer timer.stop(); Stop the timer
...from timer.addActionListener(this); Add the panel object as a listener for timer public void actionPerformed (ActionEvent event) { Invoked each time timer interval expires ... }

GUI Intermission -- Mouse events

The GUI for this lab is provided for you. The listens for mouse clicks. When a mouse click is detected, it updates a counter in a text field.

We have shown you how a button can respond to an event with an action list ener. But now we want the JPanel to respond to a mouse click -- anywhere inside the panel, not just on a button. Here is how to make that happen:

  • Declare the class with a MouseListener
    public class ClassName extends JPanel implements MouseListener
  • The constructor needs this line of code to initiate listening:
  • You'll use a mouseClicked() method like this to receive control when the mouse button is clicked.
    public void mouseClicked(MouseEvent e) 

    You can tell where the mouse was clicked by looking at e.getX() and e.getY().

  • Other methods for mouse events must be listed. But you may leave them with empty bodies if you don't care about the events.
    public void mouseReleased(MouseEvent e )
    public void mouseEntered(MouseEvent e)
    public void mouseExited(MouseEvent e)
    public void mousePressed(MouseEvent e)


Submit and The assignment name is Lab_10.

Copyright © 2003 by Carol Miller and Tony Sprinkle. All rights reserved