FileDocCategorySizeDatePackage
simpleEvent.javaAPI DocExample2148Thu Nov 23 15:25:12 GMT 2000None

simpleEvent

public class simpleEvent extends Applet implements ActionListener

Fields Summary
private TextField
myText
private Button
but1
private int
number
private boolean
hit
Constructors Summary
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent ae)

		String tempString;
		
		if (ae.getSource()==myText)			//was it the text event?
		{
			tempString = myText.getText(); //get the string out of the text field
			number = Integer.parseInt(tempString);	//convert it to an integer
		}
		else
		{
			hit=true;											//turn our switch on to say the button was pressed
		}
		repaint();											//call paint to update the display
	
public voidinit()

	
	   //step 4
	
		//step 5
		myText = new TextField(10);			//create a new textfield object
		add(myText);											//add the new object to the window display
		myText.addActionListener(this);	//add the event handling mechanism
		Button but1 = new Button("HitMe!"); //make a new button with "HitMe!" displayed in it
		add(but1);											//add it to the window
		but1.addActionListener(this);		//add the event handling mechanism

	
public voidpaint(java.awt.Graphics g)

		int x,y;
		if (hit == true)							//is the switch on (i.e. was the button pressed)?
		{
			hit=false; //reset flag (work out why, by seeing what would happen if this line were not here)
			for(int i=0; i<number;i++)	//print Ouch out the number of times myText in the text box
			{
				x = (int) (Math.random() * 100)+1;	//calculate a random x position
				y = (int) (Math.random() * 100)+1;	//calculate a random y position
				g.drawString("Ouch!!!",20+x+i,20+y+i);	//print at the random position
			}
		}
		if (number != 0)	//if a number was typed in display it.
		{
			g.drawString("You typed in " + number,100,125);
		}