Lab 8

Applets! Read chapter 6 and review lectures 13-15. Read the instructions for compiling and executing an Applet.

There is nothing to turn in. Your TA will check the applet in your mason account. Therefore the html file to invoke the applet must be named rsp.html (lower case). If it is not named this, you will not get credit. Be sure to test it carefully.

For this lab and the next, we are going to take the "Rock, Scissors, Paper" game your wrote in the last lab and turn it into an Applet. The objective for this lab is to get the menu of choices, button, and TextField to display on the screen. In the next lab we will create a working game.

Begin by making sure these import statements are at the top of your program:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
And you will need to extend the Applet class and implement ActionListener (for Buttons) and ItemListener (for the menu choices):
public class rspapplet extends Applet implements ActionListener, ItemListener
Then declare as class variables, a TextField, Button, and Choice menu as follows:
TextField resultField;   //Output field
Button playButton;      //Control button
Choice rspMenu;		//Menu of choices
Then declare a class variable to store the user's choice (1 for Rock, 2 for Scissors, 3 for Paper): int choice=0; //add an appropriate comment We then need an init() method to lay out the components on the screen:
public void init()
{ 
	Panel p=new Panel();
	p.setLayout(new BorderLayout());

        p.add("North", new Label("Welcome to the 'Rock, Scissors, Paper' Game!"));                

        //make a menu
        rspMenu=new Choice();
	rspMenu.addItem("Rock");
	rspMenu.addItem("Scissors");
	rspMenu.addItem("Paper");
        rspMenu.addItemListener(this); //activate the menu
	p.add("Center",rspMenu);

        //make a button and activate it
        playButton=new Button("Play");
        playButton.addActionListener(this);
        p.add("East",playButton);

	//make a field to report the results of the game
        resultField=new TextField(" ",30);
        resultField.setEditable(false);
        p.add("South",resultField);
      
	add(p);
}
Then we need a method to detect when a menu item has been selected. For now, we will just print to the screen which item number has been chosen to debug our Applet.
//check which item was selected from the menu
public void itemStateChanged(ItemEvent e)
{
    repaint(); //clear the textField

//If we chose a menu item, figure out which one it was.
//getSelectedIndex returns 0-(number of items-1)
    if(e.getItemSelectable()==rspMenu)
	choice=rspMenu.getSelectedIndex()+1;
}
actionPerformed is called when we click the Play button. In the next Lab, we will add the code here to evaluate the game. For now, we will just test to be sure the method was called:
//e is the "event" that was "fired" when we clicked the button
public void actionPerformed(ActionEvent e)
{
//when user presses button, evaluate choice
//print answer
 if(e.getActionCommand().equals("Play"))
 {
                 resultField.setText("Playing Game!!");
 }
}
The paint method redraws the TextField.
	public void paint (Graphics g)
	{
		resultField.setText("User chose: "+choice);//clear textField
	}  

Type this code in and make sure it compiles. Create an html file to test the applet. Be sure both the .class and .html file are readable, but the .java file is not! (If the .java file is readable your score for this lab is 0!). Ask your TA or undergrad assistant if you get stuck. Have fun!