Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *Java Version: 1.8.0_25
- *Author: Peadar Ó Duinnín
- *Class: COM3-A
- *Student Number: R00095488
- */
- package Assignment1;
- import java.awt.BorderLayout;
- import java.awt.CardLayout;
- import java.awt.Choice;
- import java.awt.Font;
- import java.awt.GridLayout;
- import java.awt.Image;
- import java.awt.Insets;
- import java.awt.event.ActionEvent;
- import java.awt.event.ItemEvent;
- import java.util.Random;
- import javax.swing.JApplet;
- import javax.swing.JButton;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- public class AUIApplet extends JApplet {
- private final int APPLET_WIDTH = 600;
- private final int APPLET_HEIGHT = 400;
- //private enum ThreadStatus {NOT_RUNNING, RUNNING, WAITING};
- private int[] highScores = {0, 0, 0, 0};
- private int[] currentNumbers;
- private String numsDisplayed;
- private int currentScore = 0;
- //private ThreadStatus threadStatus;
- final Font normalFont = new Font("Cambria", Font.PLAIN, 18);
- final Font headingFont = new Font("Cambria", Font.BOLD, 24);
- JPanel contPan, startPan, setupPan, gamePan;
- JPanel scorePan, startInfoPan, rulePan, setupInfoPan, setupMidPan, gameOptPan, gameInfoPan, userInputPan;
- JButton setupGameBut, cancelBut, endGameBut, startGameBut, checkAnswerBut;
- JLabel highScoreLab, currentScoreLab, headingLab, difficultyLab, difficultyScoreLab, currentNumberLab;
- JTextField answerField;
- Choice difficulty;
- CardLayout cl = new CardLayout();
- Image waitingPic, correctPic, incorrectPic, highScorePic;
- @Override
- public void init() {
- //set applet size
- setSize(APPLET_WIDTH, APPLET_HEIGHT);
- //set thread status to not running
- //threadStatus = ThreadStatus.NOT_RUNNING;
- //start screen
- startPan = new JPanel();
- startPan.setLayout(new BorderLayout());
- //setup screen
- setupPan = new JPanel();
- setupPan.setLayout(new BorderLayout());
- setupGameBut = new JButton("New Game");
- cancelBut = new JButton("Cancel");
- //game screen
- gamePan = new JPanel();
- gamePan.setLayout(new BorderLayout());
- endGameBut = new JButton("Quit Game");
- //heading label
- headingLab = new JLabel("Number Memory");
- headingLab.setFont(headingFont);
- //rule panel
- rulePan = new JPanel();
- rulePan.setLayout(new GridLayout(8,1));
- rulePan.add(new JLabel("Rules:"));
- rulePan.add(new JLabel("1. You will be shown a series of numbers, one at a time."));
- rulePan.add(new JLabel("2. You must recite the series of numbers after the last number has been displayed."));
- rulePan.add(new JLabel("3. After each correct recitation of the sequence, another sequence will play with one extra number."));
- rulePan.add(new JLabel("Note: You can decrease/increase the time each number displays for by changing the difficulty."));
- //difficulty selection
- difficulty = new Choice();
- difficulty.add("Easy");
- difficulty.add("Normal");
- difficulty.add("Hard");
- difficulty.add("Extra Hard");
- difficulty.select(1);
- difficultyScoreLab = new JLabel("" + highScores[difficulty.getSelectedIndex()] + "");
- //game option panel
- gameOptPan = new JPanel();
- gameOptPan.setLayout(new GridLayout(1,2));
- startGameBut = new JButton("Start Game");
- gameOptPan.add(startGameBut);
- gameOptPan.add(difficulty);
- //start info panel
- startInfoPan = new JPanel();
- startInfoPan.setLayout(new BorderLayout());
- startInfoPan.add(rulePan, BorderLayout.CENTER);
- //set up high score display panel
- setupMidPan = new JPanel();
- setupMidPan.setLayout(new GridLayout(1,8));
- difficultyLab = new JLabel();
- difficultyLab.setText("High Score for " + difficulty.getSelectedItem() + " Difficulty:");
- setupMidPan.add(difficultyLab);
- setupMidPan.add(difficultyScoreLab);
- //setup info panel
- setupInfoPan = new JPanel();
- setupInfoPan.setLayout(new BorderLayout());
- setupInfoPan.add(gameOptPan, BorderLayout.SOUTH);
- setupInfoPan.add(setupMidPan, BorderLayout.CENTER);
- //user input panel
- userInputPan = new JPanel();
- userInputPan.setLayout(new GridLayout(2,1));
- answerField = new JTextField(30);
- userInputPan.add(answerField);
- checkAnswerBut = new JButton("Check Answer");
- userInputPan.add(checkAnswerBut);
- //game info panel
- gameInfoPan = new JPanel();
- gameInfoPan.setLayout(new GridLayout(5,1));
- currentNumberLab = new JLabel("");
- gameInfoPan.add(currentNumberLab);
- gameInfoPan.add(userInputPan);
- gameInfoPan.add(new JLabel("Enter each number followed by a space."));
- //score panel
- scorePan = new JPanel();
- scorePan.setLayout(new GridLayout(10,1));
- highScoreLab = new JLabel("High Score: " + highScores[difficulty.getSelectedIndex()] + " ");
- currentScoreLab = new JLabel("Current Score: " + currentScore + " ");
- scorePan.add(highScoreLab);
- scorePan.add(currentScoreLab);
- //adding to start panel
- startPan.add(setupGameBut, BorderLayout.SOUTH);
- startPan.add(headingLab, BorderLayout.NORTH);
- startPan.add(startInfoPan, BorderLayout.CENTER);
- //adding to setup panel
- setupPan.add(cancelBut, BorderLayout.SOUTH);
- setupPan.add(headingLab, BorderLayout.NORTH);
- setupPan.add(setupInfoPan, BorderLayout.CENTER);
- //adding to game panel
- gamePan.add(endGameBut, BorderLayout.SOUTH);
- gamePan.add(headingLab, BorderLayout.NORTH);
- gamePan.add(gameInfoPan, BorderLayout.CENTER);
- gamePan.add(scorePan, BorderLayout.EAST);
- //setting up container panel and adding each screen to it
- contPan = new JPanel();
- contPan.setLayout(cl);
- contPan.add(startPan, "Start Applet Screen");
- contPan.add(setupPan, "Setup Game Screen");
- contPan.add(gamePan, "New Game Screen");
- //action listeners
- setupGameBut.addActionListener((ActionEvent e) -> {
- newGame();
- });
- startGameBut.addActionListener((ActionEvent e) -> {
- //startGame(0);
- });
- cancelBut.addActionListener((ActionEvent e) -> {
- quitGame();
- });
- endGameBut.addActionListener((ActionEvent e) -> {
- quitGame();
- });
- difficulty.addItemListener((ItemEvent e) -> {
- switch(difficulty.getSelectedIndex()) {
- case 0:
- difficultyLab.setText("High Score for " + difficulty.getSelectedItem() + " Difficulty:");
- difficultyScoreLab.setText(Integer.toString(highScores[difficulty.getSelectedIndex()]));
- break;
- case 1:
- difficultyLab.setText("High Score for " + difficulty.getSelectedItem() + " Difficulty:");
- difficultyScoreLab.setText(Integer.toString(highScores[difficulty.getSelectedIndex()]));
- break;
- case 2:
- difficultyLab.setText("High Score for " + difficulty.getSelectedItem() + " Difficulty:");
- difficultyScoreLab.setText(Integer.toString(highScores[difficulty.getSelectedIndex()]));
- break;
- case 3:
- difficultyLab.setText("High Score for " + difficulty.getSelectedItem() + " Difficulty:");
- difficultyScoreLab.setText(Integer.toString(highScores[difficulty.getSelectedIndex()]));
- break;
- }
- });
- //add container panel
- this.add(contPan);
- }
- private void newGame() {
- cl.show(contPan, "Setup Game Screen");
- }
- /* not completed
- private void startGame(int currentScore) {
- cl.show(contPan, "New Game Screen");
- changeThreadStatus(ThreadStatus.RUNNING);
- currentNumbers = getRandomNumbers(currentScore);
- printNumbers(currentNumbers);
- }
- */
- /* not completed
- private int[] getRandomNumbers(int currentScore) {
- int[] randomNums = new int[BASE_AMOUNT + currentScore];
- for (int i = 0; i < randomNums.length; i++) {
- randomNums[i] = getRandomNumber();
- }
- return randomNums;
- }
- */
- private int getRandomNumber() {
- Random rand = new Random();
- int max = 99;
- int min = 0;
- int randomNum = rand.nextInt((max-min) + 1) + min;
- return randomNum;
- }
- /*
- private void printNumbers(int[] randomNumbers) {
- int diffChoiceIndex = difficulty.getSelectedIndex();
- int currentAmount = BASE_AMOUNT + currentScore;
- new NumberThread(diffChoiceIndex, currentAmount);
- for
- changeThreadStatus(ThreadStatus.WAITING);
- }
- */
- private void quitGame() {
- cl.show(contPan, "Start Applet Screen");
- if (currentScore > highScores[difficulty.getSelectedIndex()]) {
- highScores[difficulty.getSelectedIndex()] = currentScore;
- }
- currentScore = 0;
- }
- //get back to this
- private void checkAnswer(String answer) {
- String numbersAsString = "";
- for (int number : currentNumbers) {
- numbersAsString += number + " ";
- }
- }
- private void answerCorrect() {
- //unimplemented
- }
- private void answerIncorrect() {
- //unimplemented
- }
- @Override
- public Insets getInsets() {
- return new Insets(10, 10, 10, 10);
- }
- }
- /*
- *This class is a mess. I'm not entirely sure what I'm doing. Don't mind it.
- */
- package Assignment1;
- public class NumberThread implements Runnable {
- private final int BASE_AMOUNT = 4;
- private final int[] DIFF_TIMES = {2000,1800,1600,1400};
- private final int CURRENT_SCORE;
- private final int NUM_OF_RUNS;
- int speed;
- public NumberThread(int difficulty, int currentScore) {
- this.speed = DIFF_TIMES[difficulty];
- this.CURRENT_SCORE = currentScore;
- this.NUM_OF_RUNS = BASE_AMOUNT + CURRENT_SCORE;
- }
- @Override
- public void run() {
- try {
- for (int i = 0; i < NUM_OF_RUNS; i++) {
- currentNumberLab.setText(Integer.toString(randomNumbers[i]));
- Thread.sleep(speed);
- }
- }
- catch (InterruptedException e) {
- }
- currentNumberLab.setText(Integer.toString(randomNumbers[i]));
- Thread.sleep(speed);
- currentNumberLab.setText("");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement