import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class RPSButtonApplet extends Applet implements ActionListener
{
Label MyName = new Label(" John Sklar Presents... ");
Label HeadLine = new Label(" - Rock-Paper-Scissors Game - ");
String result = new String("");
String comp_play = new String("");
String yourplay = new String("");
int wins=0,Mywins=0,Ties=0;
Font bigFont = new Font("Arial", Font.PLAIN, 24);
Font buttonFont = new Font("Times", Font.BOLD, 24);
Font smallFont = new Font("Arial", Font.PLAIN, 14);
Button Rock = new Button(" Rock ");
Button Paper = new Button(" Paper ");
Button Scissors = new Button("Scissors");
Button reset = new Button("Reset");
public void start()
{
wins=0;
Mywins=0;
Ties=0;
result="";
comp_play="";
yourplay="";
}
public void init()
{
MyName.setFont(smallFont);
add(MyName);
HeadLine.setFont(bigFont);
HeadLine.setForeground(Color.magenta);
add(HeadLine);
Rock.setFont(buttonFont);
add(Rock);
Rock.addActionListener(this);
Paper.setFont(buttonFont);
add(Paper);
Paper.addActionListener(this);
Scissors.setFont(buttonFont);
add(Scissors);
Scissors.addActionListener(this);
}
public void paint(Graphics gr)
{
gr.setFont(bigFont);
gr.setColor(Color.blue);
gr.drawString("I clicked on: ",15,140);
gr.drawString(yourplay,210,140);
gr.setColor(Color.red);
gr.drawString("Computer Got: ",15,180);
gr.drawString(comp_play,210, 180);
gr.setColor(Color.black);
gr.drawString(result,15,220);
gr.setFont(smallFont);
gr.setColor(Color.blue);
gr.drawString("Your wins: ",87,250);
gr.drawString(Integer.toString(wins),185,250);
gr.setColor(Color.red);
gr.drawString("Computer wins: ",55,270);
gr.drawString(Integer.toString(Mywins),185,270);
gr.setColor(Color.black);
gr.drawString("Ties: ",121,290);
gr.drawString(Integer.toString(Ties),185,290);
}
public void actionPerformed(ActionEvent e)
{
add(reset);
reset.addActionListener(this);
String[] Choice = { "rock", "paper", "scissors"};
Object source = e.getSource();
if (source == Rock)
yourplay= "rock";
else
if(source == Paper)
yourplay="paper";
else
if(source==Scissors)
yourplay="scissors";
comp_play = Choice[(int) (Math.random()*3)];
if(yourplay.equals(comp_play)){// a tie
result ="Tie -- do it again";
Ties++;
}
else { // play the game.
if(yourplay.equals("rock"))// you got a rock
if(comp_play.equals("paper")){
result= "Paper covers rock, You lose!";
Mywins++;
}
else {
result="Rock breaks scissors, You win!";
wins++;
}
if(yourplay.equals("paper"))// you got a rock
if(comp_play.equals("scissors")){
result= "Scissors cuts paper, You lose!" ;
Mywins++;
}
else{
result= "Paper covers rock You win!" ;
wins++;
}
if(yourplay.equals("scissors"))// you got a rock
if(comp_play.equals("rock")){
result= "Rock breaks scissors, You lose!";
Mywins++;
}
else{
result= "Scissors cuts paper, You win!" ;
wins++;
}
}
if(source==reset)
{
start();
init();
}
invalidate();
validate();
repaint();
}
}