Activity 10.2 - Programming an applet

Topic

This optional exercise allows you to modify or extend the applet for the game "Spot the Robot", discussed in the course text.

Materials

In addition to the Solution document, the project for this exercise contains one Java class:

Task

You are to make a number of changes to the applet and run it using either the appletviewer or a browser, to see how your modified game works in each case. We suggest using theappletviewer.

Instructions

  1. Experiment with using a different image file by changing the filename parameter in the getImage method invocation (look in the images folder to see what is available). Or try changing the size of the applet window by changing the relevant constants in the code. Run the file by right-clicking on it in the Projects window and choosing Run File. This should display the applet using the appletviewer.

    Close down the applet window to finish.

  2. Modify the applet class, SpotTheRobot, to act as a mouse listener, instead of using the inner class MouseTrap. See the course text for some initial ideas about how to do this. As before, run your modified applet and check that it still works as expected.

  3. Design a more complex game, using a number of robot images and possibly other images, and a screen display that responds to mouse clicks. Modify the applet class, SpotTheRobot, to implement your game. For example, you may wish to have robots chasing each other around the screen or being "zapped" by the mouse - it is up to you! Run your modified applet and check that it works as expected.

  4. If you have access to a website, you may wish to try installing one or more of your applets together with its associated web page. You can then try downloading and running the web page linked to the applet. Make sure that any image files or audio files required by the applet are downloaded from the computer that hosts the website. Recall that applet security restrictions mean that a standard applet cannot access the local file system (i.e. on the computer that is running the applet).

  5. Note

    [IMPORTANT]Unless you are very sure of what you are doing, it is not a good idea at this stage to make your applet publicly available. If you want to do this, get some expert advice.

    To set the size of the applet in a browser, after building, copy the HTML file from the build folder to the src/ex10_2 folder and edit the size of the applet in the HTML file by setting the width and height attributes in the APPLET tag.