Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
6.4 kB
2
Indexable
Never
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 *
 * @author 
 */
public class Main  implements Runnable, ActionListener{

  // Class Variables
  JPanel mainPanel;
  JLabel oddsorevensLabel;
  JButton oddButton;
  JButton evenButton;
  JLabel oddsorevensOutput;
  JLabel fingerscountLabel;
  JTextField fingerscountInput;
  JButton countdownButton;
  JLabel youpickedOutput;
  JLabel computerpickedOutput;
  JLabel oddorevenWinsOutput;
  JButton resetButton;

  // make a random number
  int number;
  int highest = 5;
  int lowest = 0;
  
  // Method to assemble our GUI
  public void run(){
    // Creates a JFrame that is 800 pixels by 600 pixels, and closes when you click on the X
    JFrame frame = new JFrame("Odds and Evens");
    // Makes the X button close the program
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // makes the windows 800 pixel wide by 600 pixels tall
    frame.setSize(800,600);
    // shows the window
    frame.setVisible(true);

    // create main panel
    mainPanel = new JPanel();

    // set up a manual layout
    mainPanel.setLayout(null);

    // make the oddsorevens area
    oddsorevensLabel = new JLabel("Odds or Evens?");
    oddsorevensLabel.setBounds(160, 10, 175, 30);

    // create the odd button
    oddButton = new JButton("ODD");
    oddButton.setBounds(130, 50, 70, 30);
    // set the button disabled
    oddButton.setEnabled(true);
    oddButton.addActionListener(this);
    oddButton.setActionCommand("oddButton");


    // create the even button
    evenButton = new JButton("EVEN");
    evenButton.setBounds(235, 50, 70, 30);
    // set the button disabled
    evenButton.setEnabled(true);
    evenButton.addActionListener(this);
    evenButton.setActionCommand("evenButton");
    
    // create the output area
    oddsorevensOutput = new JLabel();
    oddsorevensOutput.setBounds(145, 90, 200, 30);

    // make the fingerscount area
    fingerscountLabel = new JLabel("Number of Fingers");
    fingerscountLabel.setBounds(10, 125, 160, 30);
    fingerscountInput = new JTextField();
    fingerscountInput.setBounds(165, 125, 50, 30);

    // create the countdown button
    countdownButton = new JButton("1, 2, 3, Shoot!");
    countdownButton.setBounds(135, 175, 150, 30);
    // make sure the button listens to when it is clicked
    countdownButton.addActionListener(this);
    countdownButton.setActionCommand("shoot");

    // create the output area
    youpickedOutput = new JLabel();
    youpickedOutput.setBounds(10, 215, 260, 30);

    // create the output area
    computerpickedOutput = new JLabel();
    computerpickedOutput.setBounds(10, 245, 260, 30);

    // create the output area
    oddorevenWinsOutput = new JLabel();
    oddorevenWinsOutput.setBounds(10, 290, 320, 30);

    // create the reset button
    resetButton = new JButton("Reset back to the beginning");
    resetButton.setBounds(115, 340, 260, 30);
    // make sure the button listens to when it is clicked
    resetButton.addActionListener(this);
    resetButton.setActionCommand("reset");

    // add everything onto the panel
    mainPanel.add(oddsorevensLabel);
    mainPanel.add(oddButton);
    mainPanel.add(evenButton);
    mainPanel.add(oddsorevensOutput);
    mainPanel.add(fingerscountLabel);
    mainPanel.add(fingerscountInput);
    mainPanel.add(countdownButton);
    mainPanel.add(youpickedOutput);
    mainPanel.add(computerpickedOutput);
    mainPanel.add(oddorevenWinsOutput);
    mainPanel.add(resetButton);

    // add the main panel onto the frame
    frame.add(mainPanel);
    
  }

  // method called when a button is pressed
  public void actionPerformed(ActionEvent e){
    // get the command from the action
    String command = e.getActionCommand();

    //check for the oddButton command
    if(command.equals("oddButton")){
      // disable the button after it's clicked
      oddButton.setEnabled(false);
      // disable for the other button 
      evenButton.setEnabled(false);
      // put the display word below oddButton
      oddsorevensOutput.setText("You are on team ODD");
    }
    //check for the evenButton command
    if(command.equals("evenButton")){
      // disable the button after it's clicked
      evenButton.setEnabled(false);
      // disable for the other button
      oddButton.setEnabled(false);
      // put the display word below evenButton
      oddsorevensOutput.setText("You are on team EVEN");
    }
    
    // determine if a countdown has been made
    if(command.equals("shoot")){
      String shootString = fingerscountInput.getText();
      // set shootString as integer
      int shootValue = (int)Integer.parseInt(shootString);
      // print out youpicked
      youpickedOutput.setText("You picked " + shootString + " fingers");
      // generate a rando number
      number = (int)(Math.random()*(highest-lowest + 1) + lowest);
      // print out computerpicked
      computerpickedOutput.setText("Computer picked " + number + " fingers");
      // determine the winner between odd and even
      if((shootValue + number) % 2 == 0){
        // add between youpicked and computerpicked
        number = shootValue + number;
        // print out one of the winner
        oddorevenWinsOutput.setText("That is " + number + " fingers." + " Even wins!");
      }
      // print out the next winner
      else{
        oddorevenWinsOutput.setText("That is " + (number + shootValue) + " fingers." + " Odd wins!");
      }
  
      if(command.equals("reset")){
      // reset everything back to the beginning
      oddButton.setEnabled(true);
      evenButton.setEnabled(true);
      fingerscountInput.setEnabled(false);
      fingerscountLabel.setEnabled(false);
      countdownButton.setEnabled(false);
      fingerscountLabel.setText("");
      resetButton.setVisible(false);
      oddorevenWinsOutput.setVisible(false);
      shootValue = 0;
      number = 0;
      youpickedOutput.setText("You picked " + shootString + " fingers");
      computerpickedOutput.setText("Computer picked " + number + " fingers");
      oddorevenWinsOutput.setText("");
      youpickedOutput.setText("");
      computerpickedOutput.setText("");
      }
    }
  }


  // Main method to start our program
  public static void main(String[] args){
    // Creates an instance of our program
    Main  gui = new Main();
    // Lets the computer know to start it in the event thread
    SwingUtilities.invokeLater(gui);
  }
}