ruhul0

Untitled

Apr 21st, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 25.73 KB | None | 0 0
  1. import javax.swing.*;
  2.  
  3. import java.awt.Color;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.io.BufferedReader;
  7. import java.io.FileNotFoundException;
  8. import java.io.FileReader;
  9. import java.io.IOException;
  10. import java.util.ArrayList;
  11. import java.util.Random;
  12.  
  13.  
  14. public class Gui {
  15.     public static ArrayList<IQ> questionList = new ArrayList<IQ>();
  16.     JFrame game = new JFrame("Quiz");
  17.     JButton back = new JButton("Back");
  18.     JFrame gameOver = new JFrame();
  19.     JFrame gameOption = new JFrame("Choose Game");
  20.     JFrame highScoreOption = new JFrame("Choose High Score");
  21.     JFrame mathGame = new JFrame("Math Game");
  22.     JFrame quizGame = new JFrame("Quiz Game");
  23.     JLabel firstNumberDigit = new JLabel();
  24.     JLabel wrong = new JLabel ("Wrong Answer");
  25.     JLabel correct = new JLabel ("Correct Answer");
  26.     JLabel secondNumberDigit = new JLabel();
  27.     JTextField ans = new JTextField();
  28.     static JButton next = new JButton("Next");
  29.     static JButton skip = new JButton("Skip");
  30.     JLabel secondNumber = new JLabel();
  31.     JLabel firstNumber = new JLabel();
  32.     Random random = new Random();
  33.     JLabel math = new JLabel();
  34.     JLabel mathChoice = new JLabel();
  35.     JLabel lifeLabel = new JLabel();
  36.     Timer t = new Timer(1000,new TimeShow());
  37.     static JButton submit = new JButton("Submit");
  38.     JLabel time = new JLabel();
  39.     JLabel loss = new JLabel(new ImageIcon("loss.jpg"));
  40.     JLabel image = new JLabel(new ImageIcon("game.png"));
  41.     JLabel bg = new JLabel(new ImageIcon("bg.jpg"));
  42.     JLabel quizGameScore = new JLabel();
  43.  
  44.  
  45.     JLabel question1 = new JLabel();
  46.  
  47.     JButton ans1 = new JButton();
  48.     JButton ans2 = new JButton();
  49.     JButton ans3 = new JButton();
  50.     JButton ans4 = new JButton();
  51.  
  52.     boolean correctAnswer = false;
  53.     //String playerNameMath="";
  54.     //String playerNameIQ="";
  55.     int first,second;
  56.     int sum,choice,i=0;
  57.     static int ansChoice=0;
  58.     int ansValue,mathHighScore0=0,mathHighScore=0,life=10,timeValue=60,iqHighScore=0,iqHighScore0=0;
  59.     JLabel mathScore = new JLabel();
  60.     JLabel IQ = new JLabel();
  61.     public Gui()
  62.     {
  63.         game.setSize(500,281);
  64.         game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  65.         game.setVisible(true);
  66.         game.setLayout(null);
  67.         JButton start = new JButton("Start");
  68.         game.add(start);
  69.         //start.setBackground(Color.red);
  70.         start.setBounds(10,10,150,30);
  71.         start.setVisible(true);
  72.         JButton highScore = new JButton("High Score");
  73.         game.add(highScore);
  74.         highScore.setBounds(10,40,150,30);
  75.         highScore.setVisible(true);
  76.         start.addActionListener(new GameOption());
  77.         highScore.addActionListener(new HighScoreOption());
  78.         game.add(image);
  79.         image.setBounds(0,0,500,281);
  80.         image.setVisible(true);
  81.         gameOver.setSize(625,508);
  82.         gameOver.add(back);
  83.         back.setBounds(0,400,100,30);
  84.         back.addActionListener(new Back());
  85.         gameOver.add(time);
  86.         time.setVisible(true);
  87.         time.setBounds(0,0,150,50);
  88.         time.setForeground(Color.red);
  89.         gameOver.add(lifeLabel);
  90.         lifeLabel.setBounds(0,51,150,50);
  91.         lifeLabel.setForeground(Color.red);
  92.         gameOver.add(loss);
  93.         loss.setBounds(0,0,625,508);
  94.         loss.setVisible(true);
  95.     }
  96.     public static void main(String[] args) {
  97.  
  98.         new Gui();
  99.     }
  100.     public class TimeShow implements ActionListener{
  101.  
  102.         @Override
  103.         public void actionPerformed(ActionEvent actionEvent) {
  104.             timeValue--;
  105.             time.setText("Time Left: " + String.valueOf(timeValue) + "s");
  106.             time.setForeground(Color.green);
  107.             if(timeValue<=0)
  108.             {
  109.                 t.stop();
  110.                 time.setText("No Time left. Restart the game");
  111.                 time.setForeground(Color.red);
  112.                 next.setEnabled(false);
  113.                 skip.setEnabled(false);
  114.                 submit.setEnabled(false);
  115.                 ans.setEditable(false);
  116.                 gameOver.setVisible(true);
  117.                 mathGame.dispose();
  118.                 if(life<=0)
  119.                 {
  120.                     lifeLabel.setText("No life left. Restart the game");
  121.                     lifeLabel.setForeground(Color.red);
  122.                     next.setEnabled(false);
  123.                     skip.setEnabled(false);
  124.                     submit.setEnabled(false);
  125.                     ans.setEditable(false);
  126.                     t.stop();
  127.  
  128.                     gameOver.setVisible(true);
  129.                     mathGame.dispose();
  130.                 }
  131.             }
  132.  
  133.         }
  134.     }
  135.     public class GameOption implements ActionListener {
  136.         @Override
  137.         public void actionPerformed(ActionEvent actionEvent) {
  138.             gameOption.setSize(500,281);
  139.             gameOption.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  140.             gameOption.setVisible(true);
  141.             gameOption.setLayout(null);
  142.             JButton quiz = new JButton("IQ Quiz");
  143.             gameOption.add(quiz);
  144.             quiz.setBounds(10,10,150,30);
  145.             quiz.setVisible(true);
  146.             quiz.setEnabled(true);
  147.             JButton math = new JButton("Math Quiz");
  148.             gameOption.add(math);
  149.             math.setBounds(10,40,150,30);
  150.             math.setVisible(true);
  151.             math.addActionListener(new MathGame());
  152.             quiz.addActionListener(new QuizGame());
  153.             game.dispose();
  154.             gameOption.add(image);
  155.             image.setBounds(0,0,500,281);
  156.             image.setVisible(true);
  157.         }
  158.     }
  159.  
  160.     public class HighScoreOption implements ActionListener {
  161.         @Override
  162.         public void actionPerformed(ActionEvent actionEvent) {
  163.             highScoreOption.setSize(500,281);
  164.             highScoreOption.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  165.             highScoreOption.setVisible(true);
  166.             highScoreOption.setLayout(null);
  167.             IQ.setText("IQ Quiz High Score: ");
  168.             highScoreOption.add(IQ);
  169.             IQ.setBounds(10,10,150,30);
  170.             IQ.setVisible(true);
  171.             math.setText("Math Game High Score: " +  mathHighScore);
  172.             highScoreOption.add(math);
  173.             math.setBounds(10,40,150,30);
  174.             math.setVisible(true);
  175.  
  176.  
  177.             highScoreOption.add(back);
  178.             back.setVisible(true);
  179.             back.setBounds(0,551,150,30);
  180.             back.addActionListener(new Back());
  181.             game.dispose();
  182.             highScoreOption.add(image);
  183.             image.setBounds(0,0,500,281);
  184.             image.setVisible(true);
  185.         }
  186.     }
  187.  
  188.     public class MathGame implements ActionListener {
  189.         @Override
  190.         public void actionPerformed(ActionEvent actionEvent) {
  191.             t.start();
  192.             first = random.nextInt(99) + 1;
  193.             second= random.nextInt(99) + 1;
  194.             while(second>first)
  195.             {
  196.                 first = random.nextInt(99) + 1;
  197.                 second= random.nextInt(99) + 1;
  198.             }
  199.             choice = random.nextInt(4)+1;
  200.             mathGame.setSize(500,700);
  201.             mathGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  202.             mathGame.setVisible(true);
  203.             mathGame.setLayout(null);
  204.             firstNumber.setText("First Number");
  205.             firstNumber.setForeground(Color.white);
  206.             mathGame.add(firstNumber);
  207.             firstNumber.setVisible(true);
  208.             firstNumber.setBounds(0,0,150,30);
  209.             //firstNumberDigit = new JLabel(String.valueOf(first));
  210.             firstNumberDigit.setText(String.valueOf(first));
  211.             firstNumberDigit.setForeground(Color.white);
  212.             mathGame.add(firstNumberDigit);
  213.             firstNumberDigit.setBounds(0,51,150,30);
  214.             firstNumberDigit.setVisible(true);
  215.             secondNumber.setText("Second Number");
  216.             secondNumber.setForeground(Color.white);
  217.             mathGame.add(secondNumber);
  218.             secondNumber.setVisible(true);
  219.             secondNumber.setBounds(151,0,150,30);
  220.             //secondNumberDigit = new JLabel(String.valueOf(second));
  221.             secondNumberDigit.setText(String.valueOf(second));
  222.  
  223.             secondNumberDigit.setForeground(Color.white);
  224.             mathGame.add(secondNumberDigit);
  225.             secondNumberDigit.setVisible(true);
  226.             secondNumberDigit.setBounds(151,51,150,30);
  227.             mathGame.add(ans);
  228.             ans.setBounds(0,301,150,30);
  229.             mathGame.add(submit);
  230.             submit.setVisible(true);
  231.             submit.setBounds(0,351,100,30);
  232.             submit.addActionListener(new Ans());
  233.             mathGame.add(next);
  234.             next.setVisible(true);
  235.             next.setBounds(0,451,150,30);
  236.             next.addActionListener(this);
  237.             mathGame.add(skip);
  238.             skip.setVisible(true);
  239.             skip.setBounds(200,451,150,30);
  240.             skip.addActionListener(new Skip());
  241.             mathGame.add(mathChoice);
  242.             mathChoice.setVisible(true);
  243.             mathChoice.setBounds(0,201,100,30);
  244.             mathChoice.setForeground(Color.white);
  245.             if(choice==1)
  246.             {
  247.  
  248.                 sum=first+second;
  249.                 mathChoice.setText("Addition");
  250.             }
  251.             else if(choice==2)
  252.             {
  253.                 sum=first-second;
  254.                 mathChoice.setText("Substraction");
  255.             }
  256.             else if(choice==3)
  257.             {
  258.                 sum=first*second;
  259.                 mathChoice.setText("Multiplication");
  260.             }
  261.             else if(choice==4)
  262.             {
  263.                 sum=first/second;
  264.                 mathChoice.setText("Division");
  265.             }
  266.             next.setEnabled(false);
  267.             mathScore.setText("Score is: " + String.valueOf(mathHighScore0));
  268.             mathScore.setForeground(Color.white);
  269.             mathGame.add(mathScore);
  270.             mathScore.setVisible(true);
  271.             mathScore.setBounds(0,501,150,30);
  272.             lifeLabel.setText("Life left: " + String.valueOf(life));
  273.             lifeLabel.setForeground(Color.green);
  274.             mathGame.add(lifeLabel);
  275.             lifeLabel.setVisible(true);
  276.             lifeLabel.setBounds(200,501,150,30);
  277.             JButton back = new JButton("Back");
  278.             mathGame.add(back);
  279.             back.setVisible(true);
  280.             back.setBounds(0,551,150,30);
  281.             back.addActionListener(new Back());
  282.             mathGame.add(time);
  283.             time.setBounds(0,600,200,30);
  284.             time.setVisible(true);
  285.             if(life<=0)
  286.             {
  287.                 lifeLabel.setText("No life left. Restart the game");
  288.                 lifeLabel.setForeground(Color.red);
  289.                 next.setEnabled(false);
  290.                 skip.setEnabled(false);
  291.                 submit.setEnabled(false);
  292.                 ans.setEditable(false);
  293.                 t.stop();
  294.  
  295.                 gameOver.setVisible(true);
  296.                 mathGame.dispose();
  297.  
  298.             }
  299.             mathGame.add(bg);
  300.             bg.setVisible(true);
  301.             bg.setBounds(0,0,500,700);
  302.             gameOption.dispose();
  303.             next.setEnabled(true);
  304.             skip.setEnabled(true);
  305.             submit.setEnabled(true);
  306.  
  307.  
  308.         }
  309.     }
  310.     public class QuizGame implements ActionListener {
  311.         @Override
  312.         public void actionPerformed(ActionEvent actionEvent) {
  313.             quizGame.setSize(500, 700);
  314.             quizGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  315.             quizGame.setVisible(true);
  316.             quizGame.setLayout(null);
  317.             try {
  318.                 BufferedReader br = new BufferedReader(new FileReader("question.txt"));
  319.                 String question=br.readLine();
  320.                 String[] questionOption;
  321.                 while(question!=null)
  322.                 {
  323.                     questionOption=question.split("-");
  324.                     IQ iq = new IQ(questionOption[0],questionOption[1],questionOption[2],questionOption[3],questionOption[4],questionOption[5]);
  325.                     questionList.add(iq);
  326.                     question=br.readLine();
  327.                 }
  328.             } catch (FileNotFoundException e) {
  329.                 e.printStackTrace();
  330.             } catch (IOException e) {
  331.                 e.printStackTrace();
  332.             }
  333.             skip.setEnabled(true);
  334.             t.start();
  335.             question1.setText(questionList.get(i).Question);
  336.             question1.setForeground(Color.white);
  337.             ans1.setText(questionList.get(i).ans1);
  338.             ans2.setText(questionList.get(i).ans2);
  339.             ans3.setText(questionList.get(i).ans3);
  340.             ans4.setText(questionList.get(i).ans4);
  341.             quizGame.add(question1);
  342.             question1.setVisible(true);
  343.             question1.setBounds(0, 0, 700, 80);
  344.             quizGame.add(ans1);
  345.             ans1.setVisible(true);
  346.             ans1.setBounds(0, 81, 200, 30);
  347.             ans1.addActionListener(new AnsChoice1());
  348.             quizGame.add(ans2);
  349.             ans2.setVisible(true);
  350.             ans2.setBounds(0, 161, 200, 30);
  351.             ans2.addActionListener(new AnsChoice2());
  352.             quizGame.add(ans3);
  353.             ans3.setVisible(true);
  354.             ans3.setBounds(0, 241, 200, 30);
  355.             ans3.addActionListener(new AnsChoice3());
  356.             quizGame.add(ans4);
  357.             ans4.setVisible(true);
  358.             ans4.setBounds(0, 321, 200, 30);
  359.             ans4.addActionListener(new AnsChoice4());
  360.             quizGame.add(correct);
  361.             correct.setVisible(false);
  362.             correct.setBounds(0,361,100,40);
  363.             quizGame.add(wrong);
  364.             wrong.setVisible(true);
  365.             correct.setBounds(0,361,100,40);
  366.             quizGame.add(next);
  367.             next.setVisible(true);
  368.             next.setBounds(0,451,150,30);
  369.             next.addActionListener(this);
  370.             quizGame.add(skip);
  371.             skip.setVisible(true);
  372.             skip.setBounds(200,451,150,30);
  373.             skip.addActionListener(new Skip());
  374.             quizGame.add(time);
  375.             time.setBounds(0,600,200,30);
  376.             time.setVisible(true);
  377.  
  378.             quizGame.add(lifeLabel);
  379.             lifeLabel.setVisible(true);
  380.             lifeLabel.setBounds(200,501,150,30);
  381.  
  382.             JButton back = new JButton("Back");
  383.             quizGame.add(back);
  384.             back.setVisible(true);
  385.             back.setBounds(0,551,150,30);
  386.             back.addActionListener(new Back());
  387.                 quizGameScore.setText("Score is: " + String.valueOf(iqHighScore0));
  388.                 quizGameScore.setForeground(Color.green);
  389.                 lifeLabel.setForeground(Color.green);
  390.                 next.setEnabled(true);
  391.                 skip.setEnabled(true);
  392.                 submit.setEnabled(true);
  393.                 ans.setEditable(true);
  394.                 t.stop();
  395.             if(life<=0) {
  396.  
  397.                 gameOver.setVisible(true);
  398.                 mathGame.dispose();
  399.  
  400.             }
  401.             quizGame.add(bg);
  402.             bg.setVisible(true);
  403.             bg.setBounds(0,0,500,700);
  404.             i++;
  405.  
  406.  
  407.  
  408.         }
  409.     }
  410.     public class AnsChoice1 implements ActionListener{
  411.  
  412.         @Override
  413.         public void actionPerformed(ActionEvent actionEvent) {
  414.             ansChoice=1;
  415.             if(ansChoice==1)
  416.             {
  417.                 next.setEnabled(false);
  418.                 if(questionList.get(i).correctAnswer.equals(questionList.get(i).ans4))
  419.                 {
  420.                     iqHighScore0++;
  421.                     quizGame.add(correct);
  422.                     correct.setVisible(true);
  423.                     correct.setBounds(0,401,150,30);
  424.                     wrong.setVisible(false);
  425.                     next.setEnabled(true);
  426.                     correct.setForeground(Color.GREEN);
  427.                     quizGameScore.setText("Score is: " + String.valueOf(iqHighScore0));
  428.                     lifeLabel.setText("Life left: " + String.valueOf(life));
  429.                     lifeLabel.setForeground(Color.green);
  430.                     skip.setEnabled(false);
  431.                     if(iqHighScore0>iqHighScore){
  432.                         iqHighScore=iqHighScore0;
  433.                     }
  434.                     submit.setEnabled(false);
  435.                     skip.setEnabled(false);
  436.  
  437.  
  438.                 } else{
  439.                     life--;
  440.                     quizGame.add(wrong);
  441.                     wrong.setVisible(true);
  442.                     wrong.setBounds(0,401,150,30);
  443.                     correct.setVisible(false);
  444.                     next.setEnabled(false);
  445.                     wrong.setForeground(Color.red);
  446.                     quizGameScore.setText("Score is: " + String.valueOf(iqHighScore0));
  447.                     lifeLabel.setText("Life left: " + String.valueOf(life));
  448.                     lifeLabel.setForeground(Color.green);
  449.                 }
  450.                 game.dispose();
  451.             }
  452.         }
  453.     }
  454.     public class AnsChoice2 implements ActionListener{
  455.  
  456.         @Override
  457.         public void actionPerformed(ActionEvent actionEvent) {
  458.             ansChoice=2;
  459.             if(ansChoice==2)
  460.             {
  461.                 next.setEnabled(false);
  462.                 if(questionList.get(i).correctAnswer.equals(questionList.get(i).ans2))
  463.                 {
  464.                     iqHighScore0++;
  465.                     quizGame.add(correct);
  466.                     correct.setVisible(true);
  467.                     correct.setBounds(0,401,150,30);
  468.                     wrong.setVisible(false);
  469.                     next.setEnabled(true);
  470.                     correct.setForeground(Color.GREEN);
  471.                     quizGameScore.setText("Score is: " + String.valueOf(iqHighScore0));
  472.                     quizGameScore.setForeground(Color.green);
  473.                     lifeLabel.setText("Life left: " + String.valueOf(life));
  474.                     lifeLabel.setForeground(Color.green);
  475.                     lifeLabel.setForeground(Color.green);
  476.                     if(iqHighScore0>iqHighScore){
  477.                         iqHighScore=iqHighScore0;
  478.                     }
  479.                     submit.setEnabled(false);
  480.                     skip.setEnabled(false);
  481.  
  482.  
  483.                 } else{
  484.                     life--;
  485.                     quizGame.add(wrong);
  486.                     wrong.setVisible(true);
  487.                     wrong.setBounds(0,401,150,30);
  488.                     correct.setVisible(false);
  489.                     //next.setEnabled(false);
  490.                     wrong.setForeground(Color.red);
  491.                     quizGameScore.setText("Score is: " + String.valueOf(iqHighScore0));
  492.                     quizGameScore.setForeground(Color.green);
  493.                     lifeLabel.setText("Life left: " + String.valueOf(life));
  494.                     lifeLabel.setForeground(Color.green);
  495.                 }
  496.                 game.dispose();
  497.             }
  498.         }
  499.     }
  500.     public class AnsChoice3 implements ActionListener{
  501.  
  502.         @Override
  503.         public void actionPerformed(ActionEvent actionEvent) {
  504.             ansChoice=3;
  505.             if(ansChoice==3)
  506.                 next.setEnabled(false);
  507.             if(questionList.get(i).correctAnswer.equals(questionList.get(i).ans3))
  508.             {
  509.                 iqHighScore0++;
  510.                 quizGame.add(correct);
  511.                 correct.setVisible(true);
  512.                 correct.setBounds(0,401,150,30);
  513.                 wrong.setVisible(false);
  514.                 next.setEnabled(true);
  515.                 correct.setForeground(Color.GREEN);
  516.                 quizGameScore.setText("Score is: " + String.valueOf(iqHighScore0));
  517.                 quizGameScore.setForeground(Color.green);
  518.                 lifeLabel.setText("Life left: " + String.valueOf(life));
  519.                 lifeLabel.setForeground(Color.green);
  520.                 lifeLabel.setForeground(Color.green);
  521.                 skip.setEnabled(false);
  522.                 if(iqHighScore0>iqHighScore){
  523.                     iqHighScore=iqHighScore0;
  524.                 }
  525.                 submit.setEnabled(false);
  526.                 skip.setEnabled(false);
  527.  
  528.  
  529.             } else{
  530.                 life--;
  531.                 quizGame.add(wrong);
  532.                 wrong.setVisible(true);
  533.                 wrong.setBounds(0,401,150,30);
  534.                 correct.setVisible(false);
  535.                 next.setEnabled(false);
  536.                 wrong.setForeground(Color.red);
  537.                 quizGameScore.setText("Score is: " + String.valueOf(iqHighScore0));
  538.                 quizGameScore.setForeground(Color.green);
  539.                 lifeLabel.setText("Life left: " + String.valueOf(life));
  540.                 lifeLabel.setForeground(Color.green);
  541.                 lifeLabel.setForeground(Color.green);
  542.             }
  543.             game.dispose();
  544.  
  545.         }
  546.     }
  547.     public class AnsChoice4 implements ActionListener{
  548.  
  549.         @Override
  550.         public void actionPerformed(ActionEvent actionEvent) {
  551.             ansChoice=4;
  552.             if(ansChoice==4)
  553.             {
  554.  
  555.                 next.setEnabled(false);
  556.                 if(questionList.get(i).correctAnswer.equals(questionList.get(i).ans4))
  557.                 {
  558.                     iqHighScore0++;
  559.                     quizGame.add(correct);
  560.                     correct.setVisible(true);
  561.                     correct.setBounds(0,401,150,30);
  562.                     wrong.setVisible(false);
  563.                     next.setEnabled(true);
  564.                     correct.setForeground(Color.GREEN);
  565.                     quizGameScore.setText("Score is: " + String.valueOf(iqHighScore0));
  566.                     quizGameScore.setForeground(Color.green);
  567.                     lifeLabel.setText("Life left: " + String.valueOf(life));
  568.                     lifeLabel.setForeground(Color.green);
  569.                     lifeLabel.setForeground(Color.green);
  570.                     skip.setEnabled(false);
  571.                     if(iqHighScore0>iqHighScore){
  572.                         iqHighScore=iqHighScore0;
  573.                     }
  574.                     submit.setEnabled(false);
  575.                     skip.setEnabled(false);
  576.  
  577.  
  578.                 } else{
  579.                     life--;
  580.                     quizGame.add(wrong);
  581.                     wrong.setVisible(true);
  582.                     wrong.setBounds(0,401,150,30);
  583.                     correct.setVisible(false);
  584.                     //next.setEnabled(false);
  585.                     wrong.setForeground(Color.red);
  586.                     quizGameScore.setText("Score is: " + String.valueOf(iqHighScore0));
  587.                     quizGameScore.setForeground(Color.green);
  588.                     lifeLabel.setText("Life left: " + String.valueOf(life));
  589.                     lifeLabel.setForeground(Color.green);
  590.                 }
  591.                 game.dispose();
  592.             }
  593.         }
  594.     }
  595.  
  596.     public class Skip implements ActionListener{
  597.  
  598.         @Override
  599.         public void actionPerformed(ActionEvent actionEvent) {
  600.             life--;
  601.             lifeLabel.setText("Life left: " + String.valueOf(life));
  602.             lifeLabel.setForeground(Color.green);
  603.             if(life<=0)
  604.             {
  605.                 lifeLabel.setText("No life left. Restart the game");
  606.                 lifeLabel.setForeground(Color.red);
  607.                 next.setEnabled(false);
  608.                 skip.setEnabled(false);
  609.                 submit.setEnabled(false);
  610.                 ans.setEditable(false);
  611.                 t.stop();
  612.                 gameOver.setVisible(true);
  613.                 mathGame.dispose();
  614.                 quizGame.dispose();
  615.             }
  616.  
  617.         }
  618.     }
  619.     class Ans implements ActionListener
  620.     {
  621.         public void actionPerformed(ActionEvent actionEvent) {
  622.             {
  623.  
  624.                 next.setEnabled(false);
  625.                 ansValue = Integer.parseInt(ans.getText());
  626.                 if(ansValue==sum)
  627.                 {
  628.                     mathHighScore0++;
  629.                     mathGame.add(correct);
  630.                     correct.setVisible(true);
  631.                     correct.setBounds(0,401,150,30);
  632.                     wrong.setVisible(false);
  633.                     firstNumberDigit.setVisible(false);
  634.                     secondNumberDigit.setVisible(false);
  635.                     next.setEnabled(true);
  636.                     correct.setForeground(Color.GREEN);
  637.                     mathScore.setText("Score is: " + String.valueOf(mathHighScore0));
  638.                     lifeLabel.setText("Life left: " + String.valueOf(life));
  639.                     lifeLabel.setForeground(Color.green);
  640.                     //skip.setEnabled(false);
  641.                     if(mathHighScore0>mathHighScore){
  642.                         mathHighScore=mathHighScore0;
  643.                     }
  644.                     submit.setEnabled(false);
  645.                     skip.setEnabled(false);
  646.  
  647.  
  648.                 }
  649.                 else{
  650.                     life--;
  651.                     mathGame.add(wrong);
  652.                     wrong.setVisible(true);
  653.                     wrong.setBounds(0,401,150,30);
  654.                     correct.setVisible(false);
  655.                     //next.setEnabled(false);
  656.                     wrong.setForeground(Color.red);
  657.                     mathScore.setText("Score is: " + String.valueOf(mathHighScore0));
  658.                     lifeLabel.setText("Life left: " + String.valueOf(life));
  659.                     lifeLabel.setForeground(Color.green);
  660.                 }
  661.                 game.dispose();
  662.             }
  663.         }
  664.     }
  665.     class Back implements ActionListener{
  666.  
  667.         @Override
  668.         public void actionPerformed(ActionEvent e) {
  669.             // TODO Auto-generated method stub
  670.             new Gui();
  671.             gameOver.dispose();
  672.             gameOption.dispose();
  673.             highScoreOption.dispose();
  674.             mathGame.dispose();
  675.             quizGame.dispose();
  676.             mathHighScore0=0;
  677.             life=10;
  678.             timeValue=60;
  679.         }
  680.  
  681.     }
  682. }
Add Comment
Please, Sign In to add comment