Advertisement
Guest User

Game.java

a guest
Oct 22nd, 2018
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.55 KB | None | 0 0
  1. package com.mathfacts.views;
  2.  
  3.  
  4. import java.awt.Dimension;
  5. import java.awt.EventQueue;
  6. import javax.swing.JFrame;
  7. import javax.swing.JPanel;
  8. import javax.swing.UIManager;
  9. import javax.swing.border.EmptyBorder;
  10.  
  11. import com.mathfacts.common.Lives;
  12. import com.mathfacts.common.LoopArray;
  13.  
  14. import javax.swing.JLabel;
  15. import javax.swing.GroupLayout;
  16. import javax.swing.GroupLayout.Alignment;
  17. import javax.swing.SwingConstants;
  18. import javax.swing.SwingWorker;
  19.  
  20. import java.awt.Font;
  21. import java.awt.event.ActionEvent;
  22. import java.awt.event.ActionListener;
  23.  
  24. import javax.swing.LayoutStyle.ComponentPlacement;
  25. import javax.swing.JButton;
  26.  
  27. public class Game extends JFrame {
  28.  
  29.     private JPanel contentPane;
  30.     JLabel lblProblem;
  31.     JButton btnAnswer1 = new JButton("100");
  32.     JButton btnAnswer2 = new JButton("100");
  33.     JButton btnAnswer3 = new JButton("100");
  34.     JButton btnAnswer4 = new JButton("100");
  35.     JButton btnAnswer5 = new JButton("100");
  36.     JButton btnAnswer6 = new JButton("100");
  37.     private JLabel lblLivesHint = new JLabel("Lives");
  38.     private static JLabel lblLives;
  39.     private static Game game = new Game();
  40.    
  41.     //String[] values = lblProblem.getText().split(" \\* ");
  42.     //String answerAsString = lblProblem.getText();
  43.     //int answerAsInt = Integer.valueOf(values[0]) * Integer.valueOf(values[1]);
  44.    
  45.     /**
  46.      * Launch the application.
  47.      */
  48.     public static void main(String[] args) {
  49.         try {
  50.             UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  51.         } catch (Throwable e) {
  52.             e.printStackTrace();
  53.         }
  54.         EventQueue.invokeLater(new Runnable() {
  55.             public void run() {
  56.                 try {
  57.                     //Game1 frame = new Game1();
  58.                     //frame.setVisible(true);
  59.                     game.setVisible(true);
  60.                 } catch (Exception e) {
  61.                     e.printStackTrace();
  62.                 }
  63.             }
  64.         });
  65.     }
  66.  
  67.     /**
  68.      * Create the frame.
  69.      */
  70.     public Game() {
  71.        
  72.         LoopArray.addProblems();
  73.        
  74.         initComponents();
  75.         createEvents();
  76.         new LivesSwingBackgroupWorker().execute();
  77.         new GameOverSwingBackgroupWorker().execute();
  78.        
  79.     }
  80.    
  81.     ///////////////////////////////////////////////////////////
  82.     // This method contains all of the code for creating and //
  83.     // initializing components                               //
  84.     ///////////////////////////////////////////////////////////
  85.    
  86.     private void initComponents() {
  87.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  88.         contentPane = new JPanel();
  89.         contentPane.setBounds(100, 100, 585, 700);
  90.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  91.         setContentPane(contentPane);
  92.         contentPane.setPreferredSize(new Dimension(585, 700));
  93.        
  94.         lblProblem = new JLabel("0");
  95.         lblProblem.setFont(new Font("Tahoma", Font.PLAIN, 30));
  96.         lblProblem.setHorizontalAlignment(SwingConstants.LEFT);
  97.         lblProblem.setText(LoopArray.sendRandomProblem());
  98.        
  99.         lblLives = new JLabel(Lives.getStringLives());
  100.        
  101.         lblLivesHint.setFont(new Font("Tahoma", Font.PLAIN, 30));
  102.        
  103.         JLabel lblProblemHint = new JLabel("Problem:");
  104.         lblProblemHint.setFont(new Font("Tahoma", Font.PLAIN, 30));
  105.        
  106.        
  107.         btnAnswer1.setFont(new Font("Tahoma", Font.PLAIN, 30));
  108.         btnAnswer1.setText(Integer.toString( (int) Math.floor(Math.random()*145) ) );
  109.        
  110.         btnAnswer2.setFont(new Font("Tahoma", Font.PLAIN, 30));
  111.         btnAnswer2.setText(Integer.toString( (int) Math.floor(Math.random()*145) ) );
  112.        
  113.         btnAnswer3.setFont(new Font("Tahoma", Font.PLAIN, 30));
  114.         btnAnswer3.setText(Integer.toString( (int) Math.floor(Math.random()*145) ) );
  115.        
  116.         btnAnswer4.setFont(new Font("Tahoma", Font.PLAIN, 30));
  117.         btnAnswer4.setText(Integer.toString( (int) Math.floor(Math.random()*145) ) );
  118.        
  119.         btnAnswer5.setFont(new Font("Tahoma", Font.PLAIN, 30));
  120.         btnAnswer5.setText(Integer.toString( (int) Math.floor(Math.random()*145) ) );
  121.        
  122.         btnAnswer6.setFont(new Font("Tahoma", Font.PLAIN, 30));
  123.         btnAnswer6.setText(Integer.toString( (int) Math.floor(Math.random()*144) ) );
  124.        
  125.        
  126.         GroupLayout gl_contentPane = new GroupLayout(contentPane);
  127.         gl_contentPane.setHorizontalGroup(
  128.             gl_contentPane.createParallelGroup(Alignment.LEADING)
  129.                 .addGroup(gl_contentPane.createSequentialGroup()
  130.                     .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
  131.                         .addGroup(gl_contentPane.createSequentialGroup()
  132.                             .addComponent(lblProblemHint)
  133.                             .addPreferredGap(ComponentPlacement.RELATED)
  134.                             .addComponent(lblProblem, GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
  135.                             .addPreferredGap(ComponentPlacement.RELATED)
  136.                             .addComponent(lblLivesHint)
  137.                             .addPreferredGap(ComponentPlacement.RELATED)
  138.                             .addComponent(lblLives)
  139.                             .addGap(13))
  140.                         .addGroup(gl_contentPane.createSequentialGroup()
  141.                             .addGap(18)
  142.                             .addComponent(btnAnswer1)
  143.                             .addPreferredGap(ComponentPlacement.UNRELATED)
  144.                             .addComponent(btnAnswer2)
  145.                             .addPreferredGap(ComponentPlacement.UNRELATED)
  146.                             .addComponent(btnAnswer3)
  147.                             .addPreferredGap(ComponentPlacement.UNRELATED)
  148.                             .addComponent(btnAnswer4)
  149.                             .addPreferredGap(ComponentPlacement.UNRELATED)
  150.                             .addComponent(btnAnswer5)
  151.                             .addPreferredGap(ComponentPlacement.UNRELATED)
  152.                             .addComponent(btnAnswer6)))
  153.                     .addContainerGap())
  154.         );
  155.         gl_contentPane.setVerticalGroup(
  156.             gl_contentPane.createParallelGroup(Alignment.LEADING)
  157.                 .addGroup(gl_contentPane.createSequentialGroup()
  158.                     .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
  159.                         .addGroup(gl_contentPane.createSequentialGroup()
  160.                             .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
  161.                                 .addComponent(lblProblem)
  162.                                 .addComponent(lblProblemHint)
  163.                                 .addComponent(lblLivesHint))
  164.                             .addPreferredGap(ComponentPlacement.RELATED)
  165.                             .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
  166.                                 .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
  167.                                     .addComponent(btnAnswer1)
  168.                                     .addComponent(btnAnswer2)
  169.                                     .addComponent(btnAnswer3))
  170.                                 .addComponent(btnAnswer4)
  171.                                 .addComponent(btnAnswer5)
  172.                                 .addComponent(btnAnswer6)))
  173.                         .addGroup(gl_contentPane.createSequentialGroup()
  174.                             .addContainerGap()
  175.                             .addComponent(lblLives)))
  176.                     .addContainerGap(602, Short.MAX_VALUE))
  177.         );
  178.         contentPane.setLayout(gl_contentPane);
  179.        
  180.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  181.         pack();
  182.     }
  183.    
  184.     //////////////////////////////////////////////////////////////
  185.     // This method contains all of the code for creating events //  
  186.     //////////////////////////////////////////////////////////////
  187.    
  188.     private void createEvents() {              
  189.         String input = lblProblem.getText();
  190.         String parts[] = input.split("\\*");
  191.         int result = 1;
  192.         for(String part : parts)
  193.             result *= Integer.parseInt(part.trim());
  194.         System.out.println(result);
  195.        
  196.         int chooserThing = (int) Math.floor(Math.random() * 6);
  197.        
  198.         if (chooserThing == 0) {
  199.             btnAnswer1.setText(Integer.toString(result));
  200.         } else if (chooserThing == 1) {
  201.             btnAnswer2.setText(Integer.toString(result));
  202.         } else if (chooserThing == 2) {
  203.             btnAnswer3.setText(Integer.toString(result));
  204.         } else if (chooserThing == 3) {
  205.             btnAnswer4.setText(Integer.toString(result));
  206.         } else if (chooserThing == 4) {
  207.             btnAnswer5.setText(Integer.toString(result));
  208.         } else if (chooserThing == 5) {
  209.             btnAnswer6.setText(Integer.toString(result));
  210.         } else {
  211.             System.out.println("OH NOOOOOOOOOOOOOO HELP ME THE WORLD IS GONNA END THE ANSWER IS NOT THERE WHAT DO I DO THIS SHOULD HAVE NEVER HAPPENED OMG OMG OMG IT IS THE END OF THE WORLD HEEEEEEEEEEEEEEEEEEEEEELLLLLLLLLLLLLLLLLLLLLPPPPPPPPPPPPPPP THE WORLD IS GONNA EXPLODE OMG OMG OMG OMG OMG OMG OMG OMG OMG OMG OMG OMG OMG OMG OMG OMG OMG OMG OMG OMG OMG");
  212.         }
  213.        
  214.         if (chooserThing == 0) {
  215.             btnAnswer1.addActionListener(new ActionListener() {
  216.                 public void actionPerformed(ActionEvent e) {
  217.                     new Game().setVisible(true);
  218.                     dispose();
  219.                 }
  220.             });
  221.         } else if (chooserThing == 1) {
  222.             btnAnswer2.addActionListener(new ActionListener() {
  223.                 public void actionPerformed(ActionEvent e) {
  224.                     new Game().setVisible(true);
  225.                     dispose();
  226.                 }
  227.             });
  228.         } else if (chooserThing == 2) {
  229.             btnAnswer3.addActionListener(new ActionListener() {
  230.                 public void actionPerformed(ActionEvent e) {
  231.                     new Game().setVisible(true);
  232.                     dispose();
  233.                 }
  234.             });
  235.         } else if (chooserThing == 3) {
  236.             btnAnswer4.addActionListener(new ActionListener() {
  237.                 public void actionPerformed(ActionEvent e) {
  238.                     new Game().setVisible(true);
  239.                     dispose();
  240.                 }
  241.             });
  242.         } else if (chooserThing == 4) {
  243.             btnAnswer5.addActionListener(new ActionListener() {
  244.                 public void actionPerformed(ActionEvent e) {
  245.                     new Game().setVisible(true);
  246.                     dispose();
  247.                 }
  248.             });
  249.         } else if (chooserThing == 5) {
  250.             btnAnswer6.addActionListener(new ActionListener() {
  251.                 public void actionPerformed(ActionEvent e) {
  252.                     new Game().setVisible(true);
  253.                     dispose();
  254.                 }
  255.             });
  256.         }
  257.        
  258.        
  259.        
  260.         if (chooserThing != 0) {
  261.             btnAnswer1.addActionListener(new ActionListener() {
  262.                 public void actionPerformed(ActionEvent e) {
  263.                     Lives.subtractLife();
  264.                 }
  265.             });
  266.         }
  267.         if (chooserThing != 1) {
  268.             btnAnswer2.addActionListener(new ActionListener() {
  269.                 public void actionPerformed(ActionEvent e) {
  270.                     Lives.subtractLife();
  271.                 }
  272.             });
  273.         }
  274.         if (chooserThing != 2) {
  275.             btnAnswer3.addActionListener(new ActionListener() {
  276.                 public void actionPerformed(ActionEvent e) {
  277.                     Lives.subtractLife();
  278.                 }
  279.             });
  280.         }
  281.         if (chooserThing != 3) {
  282.             btnAnswer4.addActionListener(new ActionListener() {
  283.                 public void actionPerformed(ActionEvent e) {
  284.                     Lives.subtractLife();
  285.                 }
  286.             });
  287.         }
  288.         if (chooserThing != 4) {
  289.             btnAnswer5.addActionListener(new ActionListener() {
  290.                 public void actionPerformed(ActionEvent e) {
  291.                     Lives.subtractLife();
  292.                 }
  293.             });
  294.         }
  295.         if (chooserThing != 5) {
  296.             btnAnswer6.addActionListener(new ActionListener() {
  297.                 public void actionPerformed(ActionEvent e) {
  298.                     Lives.subtractLife();
  299.                 }
  300.             });
  301.         }
  302.     }
  303.        
  304.     public static class LivesSwingBackgroupWorker extends SwingWorker<Object, Object> {
  305.  
  306.         @Override
  307.         protected Object doInBackground() throws Exception {
  308.             while (true) {
  309.                 java.awt.EventQueue.invokeLater(new Runnable() {
  310.                     public void run() {
  311.                         lblLives.setText(Integer.toString(Lives.lives));
  312.                     }
  313.                 });
  314.                 try {
  315.                     Thread.sleep(100);
  316.                 } catch (InterruptedException e) {
  317.                     e.printStackTrace();
  318.                 }
  319.             }
  320.         }
  321.  
  322.     }
  323.     public static class GameOverSwingBackgroupWorker extends SwingWorker<Object, Object> {
  324.  
  325.         @Override
  326.         protected Object doInBackground() throws Exception {
  327.             while (true) {
  328.                 java.awt.EventQueue.invokeLater(new Runnable() {
  329.                     public void run() {
  330.                         int value = Integer.parseInt(lblLives.getText());
  331.                        
  332.                         if (value <= 0) {
  333.                             System.out.println("IT WORKED YAYYYYYYYYYYY");
  334.                             game.setEnabled(false);
  335.                             game.setVisible(false);
  336.                             game.dispose();
  337.                             new GameOver().setVisible(true);
  338.                             Lives.resetLives();
  339.                         }
  340.                     }
  341.                 });
  342.                 try {
  343.                     Thread.sleep(100);
  344.                 } catch (InterruptedException e) {
  345.                     e.printStackTrace();
  346.                 }
  347.             }
  348.         }
  349.  
  350.     }
  351.  
  352. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement