If this applet does not run at all, or behaves oddly, see the discussion for Activity 10.1 which has some suggestions for basic things that might be wrong. The following comments apply to the corresponding numbered instructions in the Activity 10.2 document.
You may get some odd effects if you made the applet window very large or very small. Otherwise this should be fairly straightforward. If the robot image does not appear, but the game otherwise works, check that you have specified the images folder and the file name correctly in the second argument of the getImage method.
If you changed the size of the applet window in the previous task, things may or may not work as you expect when viewing using a browser. This is because the maximum size of the applet window is set by the APPLET tag in the web page.
To make the class act as a MouseListener, you need to modify the class header as follows:
public class SpotTheRobot extends Applet implements MouseListener
You then need to delete the inner class MouseTrap and implement the five methods of the MouseListener interface as methods of class SpotTheRobot:
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e)
{
mouseXPosition = e.getX();
mouseYPosition = e.getY();
repaint(); // invokes the applet paint method
}
As you can see, the four unused methods have empty method bodies and the mouseClicked method is defined in exactly the same way as before.
Finally, you need to change the code in the init method so that the applet itself is the listener, rather than the MouseTrap object as before. To do this, you delete the following line:
this.addMouseListener(new MouseTrap());
And replace it with the following code:
this.addMouseListener(this);
Making the applet listen for mouse clicks may seem a little odd, but this technique is sometimes used and it is useful to be familiar with this idea. As far as the Java system is concerned, the listener can be almost anything as long as it implements the necessary methods for the MouseListener interface.