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 java.util.concurrent.Executors;
- import java.util.concurrent.ScheduledExecutorService;
- import java.util.concurrent.ScheduledFuture;
- import static java.util.concurrent.TimeUnit.MILLISECONDS;
- 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 final int BASE_AMOUNT = 4;
- private final int[] DIFF_TIMES = {4000,3000,2000,1000};
- private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
- private int[] highScores = {0, 0, 0, 0};
- private int[] currentNumbers;
- private String numsDisplayed;
- private int currentScore = 0;
- 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, currentDiffLab;
- 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);
- //start screen
- startPan = new JPanel();
- startPan.setLayout(new BorderLayout(5,5));
- //setup screen
- setupPan = new JPanel();
- setupPan.setLayout(new BorderLayout(5, 5));
- setupGameBut = new JButton("New Game");
- cancelBut = new JButton("Cancel");
- //game screen
- gamePan = new JPanel();
- gamePan.setLayout(new BorderLayout(5, 5));
- 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, 5, 5));
- 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, 5, 5));
- 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, 5, 5));
- 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, 5, 5));
- 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, 5, 5));
- 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, 5, 5));
- currentDiffLab = new JLabel("");
- highScoreLab = new JLabel("High Score: " + highScores[difficulty.getSelectedIndex()] + " ");
- currentScoreLab = new JLabel("Current Score: " + currentScore + " ");
- scorePan.add(currentDiffLab);
- scorePan.add(highScoreLab);
- scorePan.add(currentScoreLab);
- //adding to start panel
- startPan.add(headingLab, BorderLayout.NORTH);
- startPan.add(setupGameBut, BorderLayout.SOUTH);
- startPan.add(startInfoPan, BorderLayout.CENTER);
- //adding to setup panel
- setupPan.add(headingLab, BorderLayout.NORTH);
- setupPan.add(cancelBut, BorderLayout.SOUTH);
- setupPan.add(setupInfoPan, BorderLayout.CENTER);
- //adding to game panel
- gamePan.add(headingLab, BorderLayout.NORTH);
- gamePan.add(endGameBut, BorderLayout.SOUTH);
- 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;
- }
- });
- //answerField.addActionListener
- //add container panel
- this.add(contPan);
- }
- private void newGame() {
- cl.show(contPan, "Setup Game Screen");
- }
- private void startGame(int currentScore) {
- cl.show(contPan, "New Game Screen");
- currentNumbers = getRandomNumbers(currentScore);
- updateScorePan(currentScore);
- printNumbers(currentNumbers);
- }
- 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 speed = DIFF_TIMES[difficulty.getSelectedIndex()];
- int amount = BASE_AMOUNT + currentScore;
- answerField.setEditable(false);
- scheduleNumbers(randomNumbers, speed, amount);
- currentNumberLab.setText("");
- answerField.setEditable(true);
- }
- public void scheduleNumbers(int[] randomNumbers, int speed, int amount) {
- int curNumber = 0;
- long initialDelay = 1000;
- final Runnable setNumber = () -> {
- currentNumberLab.setText(Integer.toString(randomNumbers[curNumber]));
- System.out.println("Set to " + randomNumbers[curNumber]);
- };
- final ScheduledFuture<?> setNumberHandle = scheduler.scheduleAtFixedRate(setNumber, initialDelay, speed, MILLISECONDS);
- scheduler.schedule(() -> {
- setNumberHandle.cancel(true);
- }, (speed*amount)+initialDelay, MILLISECONDS);
- }
- private void updateScorePan(int curScore) {
- currentDiffLab.setText("");
- highScoreLab.setText("High Score: " + highScores[difficulty.getSelectedIndex()] + " ");
- currentScoreLab.setText("Current Score: " + curScore + " ");
- }
- 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);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement