Advertisement
Guest User

Untitled

a guest
Nov 1st, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.32 KB | None | 0 0
  1. /*
  2. *Java Version:      1.8.0_25
  3. *Author:            Peadar Ó Duinnín
  4. *Class:             COM3-A
  5. *Student Number:    R00095488
  6. */
  7.  
  8. package Assignment1;
  9.  
  10. import java.awt.BorderLayout;
  11. import java.awt.CardLayout;
  12. import java.awt.Choice;
  13. import java.awt.Font;
  14. import java.awt.GridLayout;
  15. import java.awt.Image;
  16. import java.awt.Insets;
  17. import java.awt.event.ActionEvent;
  18. import java.awt.event.ItemEvent;
  19. import java.util.Random;
  20. import java.util.concurrent.Executors;
  21. import java.util.concurrent.ScheduledExecutorService;
  22. import java.util.concurrent.ScheduledFuture;
  23. import static java.util.concurrent.TimeUnit.MILLISECONDS;
  24. import javax.swing.JApplet;
  25. import javax.swing.JButton;
  26. import javax.swing.JLabel;
  27. import javax.swing.JPanel;
  28. import javax.swing.JTextField;
  29.  
  30.  
  31. public class AUIApplet extends JApplet {
  32.     private final int APPLET_WIDTH = 600;
  33.     private final int APPLET_HEIGHT = 400;
  34.     private final int BASE_AMOUNT = 4;
  35.     private final int[] DIFF_TIMES = {4000,3000,2000,1000};
  36.     private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
  37.    
  38.     private int[] highScores = {0, 0, 0, 0};
  39.     private int[] currentNumbers;
  40.     private String numsDisplayed;
  41.     private int currentScore = 0;
  42.    
  43.     final Font normalFont = new Font("Cambria", Font.PLAIN, 18);
  44.     final Font headingFont = new Font("Cambria", Font.BOLD, 24);
  45.     JPanel contPan, startPan, setupPan, gamePan;
  46.     JPanel scorePan, startInfoPan, rulePan, setupInfoPan, setupMidPan, gameOptPan, gameInfoPan, userInputPan;
  47.     JButton setupGameBut, cancelBut, endGameBut, startGameBut, checkAnswerBut;
  48.     JLabel highScoreLab, currentScoreLab, headingLab, difficultyLab, difficultyScoreLab, currentNumberLab, currentDiffLab;
  49.     JTextField answerField;
  50.     Choice difficulty;
  51.     CardLayout cl = new CardLayout();
  52.     Image waitingPic, correctPic, incorrectPic, highScorePic;
  53.    
  54.     @Override
  55.     public void init() {
  56.         //set applet size
  57.         setSize(APPLET_WIDTH, APPLET_HEIGHT);
  58.        
  59.         //start screen
  60.         startPan = new JPanel();
  61.         startPan.setLayout(new BorderLayout(5,5));
  62.        
  63.         //setup screen
  64.         setupPan = new JPanel();
  65.         setupPan.setLayout(new BorderLayout(5, 5));
  66.        
  67.         setupGameBut = new JButton("New Game");
  68.         cancelBut = new JButton("Cancel");
  69.        
  70.         //game screen
  71.         gamePan = new JPanel();
  72.         gamePan.setLayout(new BorderLayout(5, 5));
  73.        
  74.         endGameBut = new JButton("Quit Game");
  75.        
  76.         //heading label
  77.         headingLab = new JLabel("Number Memory");
  78.         headingLab.setFont(headingFont);
  79.        
  80.         //rule panel
  81.         rulePan = new JPanel();
  82.         rulePan.setLayout(new GridLayout(8, 1, 5, 5));
  83.         rulePan.add(new JLabel("Rules:"));
  84.         rulePan.add(new JLabel("1. You will be shown a series of numbers, one at a time."));
  85.         rulePan.add(new JLabel("2. You must recite the series of numbers after the last number has been displayed."));
  86.         rulePan.add(new JLabel("3. After each correct recitation of the sequence, another sequence will play with one extra number."));
  87.         rulePan.add(new JLabel("Note: You can decrease/increase the time each number displays for by changing the difficulty."));
  88.        
  89.         //difficulty selection
  90.         difficulty = new Choice();
  91.         difficulty.add("Easy");
  92.         difficulty.add("Normal");
  93.         difficulty.add("Hard");
  94.         difficulty.add("Extra Hard");
  95.         difficulty.select(1);
  96.        
  97.         difficultyScoreLab = new JLabel("" + highScores[difficulty.getSelectedIndex()] + "");
  98.        
  99.         //game option panel
  100.         gameOptPan = new JPanel();
  101.         gameOptPan.setLayout(new GridLayout(1, 2, 5, 5));
  102.         startGameBut = new JButton("Start Game");
  103.         gameOptPan.add(startGameBut);
  104.         gameOptPan.add(difficulty);
  105.        
  106.         //start info panel
  107.         startInfoPan = new JPanel();
  108.         startInfoPan.setLayout(new BorderLayout());
  109.         startInfoPan.add(rulePan, BorderLayout.CENTER);
  110.        
  111.         //set up high score display panel
  112.         setupMidPan = new JPanel();
  113.         setupMidPan.setLayout(new GridLayout(1, 8, 5, 5));
  114.         difficultyLab = new JLabel();
  115.         difficultyLab.setText("High Score for " + difficulty.getSelectedItem() + " Difficulty:");
  116.         setupMidPan.add(difficultyLab);
  117.         setupMidPan.add(difficultyScoreLab);
  118.        
  119.         //setup info panel
  120.         setupInfoPan = new JPanel();
  121.         setupInfoPan.setLayout(new BorderLayout());
  122.         setupInfoPan.add(gameOptPan, BorderLayout.SOUTH);
  123.         setupInfoPan.add(setupMidPan, BorderLayout.CENTER);
  124.        
  125.         //user input panel
  126.         userInputPan = new JPanel();
  127.         userInputPan.setLayout(new GridLayout(2, 1, 5, 5));
  128.         answerField = new JTextField(30);
  129.         userInputPan.add(answerField);
  130.         checkAnswerBut = new JButton("Check Answer");
  131.         userInputPan.add(checkAnswerBut);
  132.        
  133.         //game info panel
  134.         gameInfoPan = new JPanel();
  135.         gameInfoPan.setLayout(new GridLayout(5, 1, 5, 5));
  136.         currentNumberLab = new JLabel("");
  137.         gameInfoPan.add(currentNumberLab);
  138.         gameInfoPan.add(userInputPan);
  139.         gameInfoPan.add(new JLabel("Enter each number followed by a space."));
  140.        
  141.         //score panel
  142.         scorePan = new JPanel();
  143.         scorePan.setLayout(new GridLayout(10, 1, 5, 5));
  144.         currentDiffLab = new JLabel("");
  145.         highScoreLab = new JLabel("High Score: " + highScores[difficulty.getSelectedIndex()] + "  ");
  146.         currentScoreLab = new JLabel("Current Score: " + currentScore + "  ");
  147.         scorePan.add(currentDiffLab);
  148.         scorePan.add(highScoreLab);
  149.         scorePan.add(currentScoreLab);
  150.        
  151.         //adding to start panel
  152.         startPan.add(headingLab, BorderLayout.NORTH);
  153.         startPan.add(setupGameBut, BorderLayout.SOUTH);
  154.         startPan.add(startInfoPan, BorderLayout.CENTER);
  155.        
  156.         //adding to setup panel
  157.         setupPan.add(headingLab, BorderLayout.NORTH);
  158.         setupPan.add(cancelBut, BorderLayout.SOUTH);
  159.         setupPan.add(setupInfoPan, BorderLayout.CENTER);
  160.        
  161.         //adding to game panel
  162.         gamePan.add(headingLab, BorderLayout.NORTH);
  163.         gamePan.add(endGameBut, BorderLayout.SOUTH);
  164.         gamePan.add(gameInfoPan, BorderLayout.CENTER);
  165.         gamePan.add(scorePan, BorderLayout.EAST);
  166.        
  167.         //setting up container panel and adding each screen to it
  168.         contPan = new JPanel();
  169.         contPan.setLayout(cl);
  170.         contPan.add(startPan, "Start Applet Screen");
  171.         contPan.add(setupPan, "Setup Game Screen");
  172.         contPan.add(gamePan, "New Game Screen");
  173.        
  174.         //action listeners
  175.         setupGameBut.addActionListener((ActionEvent e) -> {
  176.             newGame();
  177.         });
  178.        
  179.         startGameBut.addActionListener((ActionEvent e) -> {
  180.             startGame(0);
  181.         });
  182.        
  183.         cancelBut.addActionListener((ActionEvent e) -> {
  184.             quitGame();
  185.         });
  186.        
  187.         endGameBut.addActionListener((ActionEvent e) -> {
  188.             quitGame();
  189.         });
  190.        
  191.         difficulty.addItemListener((ItemEvent e) -> {
  192.             switch(difficulty.getSelectedIndex()) {
  193.                 case 0:
  194.                     difficultyLab.setText("High Score for " + difficulty.getSelectedItem() + " Difficulty:");
  195.                     difficultyScoreLab.setText(Integer.toString(highScores[difficulty.getSelectedIndex()]));
  196.                     break;
  197.                 case 1:
  198.                     difficultyLab.setText("High Score for " + difficulty.getSelectedItem() + " Difficulty:");
  199.                     difficultyScoreLab.setText(Integer.toString(highScores[difficulty.getSelectedIndex()]));
  200.                     break;
  201.                 case 2:
  202.                     difficultyLab.setText("High Score for " + difficulty.getSelectedItem() + " Difficulty:");
  203.                     difficultyScoreLab.setText(Integer.toString(highScores[difficulty.getSelectedIndex()]));
  204.                     break;
  205.                 case 3:
  206.                     difficultyLab.setText("High Score for " + difficulty.getSelectedItem() + " Difficulty:");
  207.                     difficultyScoreLab.setText(Integer.toString(highScores[difficulty.getSelectedIndex()]));
  208.                     break;
  209.             }
  210.         });
  211.        
  212.        
  213.        
  214.         //answerField.addActionListener
  215.        
  216.         //add container panel
  217.         this.add(contPan);
  218.     }
  219.    
  220.     private void newGame() {
  221.         cl.show(contPan, "Setup Game Screen");
  222.     }
  223.    
  224.     private void startGame(int currentScore) {
  225.         cl.show(contPan, "New Game Screen");
  226.         currentNumbers = getRandomNumbers(currentScore);
  227.         updateScorePan(currentScore);
  228.         printNumbers(currentNumbers);
  229.     }
  230.    
  231.     private int[] getRandomNumbers(int currentScore) {
  232.         int[] randomNums = new int[BASE_AMOUNT + currentScore];
  233.        
  234.         for (int i = 0; i < randomNums.length; i++) {
  235.             randomNums[i] = getRandomNumber();
  236.         }
  237.         return randomNums;
  238.     }
  239.    
  240.     private int getRandomNumber() {
  241.         Random rand = new Random();
  242.         int max = 99;
  243.         int min = 0;
  244.         int randomNum = rand.nextInt((max-min) + 1) + min;
  245.         return randomNum;
  246.     }
  247.    
  248.     private void printNumbers(int[] randomNumbers) {
  249.         int speed = DIFF_TIMES[difficulty.getSelectedIndex()];
  250.         int amount = BASE_AMOUNT + currentScore;
  251.         answerField.setEditable(false);
  252.         scheduleNumbers(randomNumbers, speed, amount);
  253.         currentNumberLab.setText("");
  254.         answerField.setEditable(true);
  255.     }
  256.    
  257.     public void scheduleNumbers(int[] randomNumbers, int speed, int amount) {
  258.         int curNumber = 0;
  259.         long initialDelay = 1000;
  260.         final Runnable setNumber = () -> {
  261.             currentNumberLab.setText(Integer.toString(randomNumbers[curNumber]));
  262.             System.out.println("Set to " + randomNumbers[curNumber]);
  263.         };
  264.         final ScheduledFuture<?> setNumberHandle = scheduler.scheduleAtFixedRate(setNumber, initialDelay, speed, MILLISECONDS);
  265.         scheduler.schedule(() -> {
  266.             setNumberHandle.cancel(true);
  267.         }, (speed*amount)+initialDelay, MILLISECONDS);
  268.     }
  269.    
  270.     private void updateScorePan(int curScore) {
  271.         currentDiffLab.setText("");
  272.         highScoreLab.setText("High Score: " + highScores[difficulty.getSelectedIndex()] + "  ");
  273.         currentScoreLab.setText("Current Score: " + curScore + "  ");
  274.     }
  275.    
  276.     private void quitGame() {
  277.         cl.show(contPan, "Start Applet Screen");
  278.         if (currentScore > highScores[difficulty.getSelectedIndex()]) {
  279.             highScores[difficulty.getSelectedIndex()] = currentScore;
  280.         }
  281.         currentScore = 0;
  282.     }
  283.    
  284.     /*get back to this
  285.     private void checkAnswer(String answer) {
  286.         String numbersAsString = "";
  287.         for (int number : currentNumbers) {
  288.             numbersAsString += number + " ";
  289.         }
  290.     }
  291.    
  292.     private void answerCorrect() {
  293.         //unimplemented
  294.     }
  295.    
  296.     private void answerIncorrect() {
  297.         //unimplemented
  298.     }
  299.     */
  300.    
  301.     @Override
  302.     public Insets getInsets() {
  303.         return new Insets(10, 10, 10, 10);
  304.     }
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement