Using nested loops and objects in an applet

In this lab you will write an applet version of the Treasure Island game we have written previously.

Setting up

Download and save, and use it as a starting point for writing your applet. Also save Island.class into the same folder. This version of Island.class is only slightly different than the one we used in the previous treasure island exercise (see Javadocs). Also remember to make an html file for your applet. Setup the dimensions in the html file to be width="400" and height="450". You should be able to run the applet and see something like this:

Coding it up

Note that the paint method has nested loops. Every time through the outer loop executes, y counts from 0 up through 10 in the inner loop. Thus, the if statement inside the inner loop gets checked a total of 10*10, or 100 times. The variables are setup in such a way that it checks once for every location on the island. Fill in some code to color each square according to its terrain type (you may need to reference the Island Javadocs). Don't get carried away on graphics just yet. Your applet should look something like this at this point:

Now add some more code to your paint method so that it displays the terrain type where the player is currently located. This can be displayed near the bottom of the applet. To do this you use the drawString method of the Graphics class.

Next, fill in keyPressed so that the user can move by using the arrow keys. Don't forget to repaint so they get an update on what kind of terrain they are on. The user should be able to move around now (after clicking the applet to gain focus).

You might notice that you can move past the water and cause a runtime error. In fact, we shouldn't allow the user to move at all once they hit the water, pirate, or treasure since these cause the game to be over. To deal with this, we will take advantage of the ability to make an early exit from a method. Make the following code the very first thing that happens in keyPressed.

if (island.currentLocation() == Island.WATER) {

If a key is pressed down and the location is currently water, then the return is executed and the method exits immediately, and the user won't get to move anywhere, because the other code in that method isn't reached. The only difference between this and return statements we have seen so far is that, since this is a void method, the return doesn't actually return any data. It just ends execution of the method.

Modify keyPressed so that the user can't move once they hit the treasure or pirate, as well.

Next, in keyPressed, have it execute the code island = new Island(10); if the user presses R on the keyboard. This will make it reset the game, by replacing the island object with a whole new one.

Now you should have a working game. Pretty up the graphics a bit. It doesn't have to be like mine, but it shouldn't just be all boring squares.

Finishing it up

Once it's done and checked over, hand in your .java source code and .html file in folder lab17.