ruhul0

Untitled

Apr 22nd, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.07 KB | None | 0 0
  1. public class QuizGame implements ActionListener {
  2.         @Override
  3.         public void actionPerformed(ActionEvent actionEvent) {
  4.             quizGame();
  5.  
  6.  
  7.  
  8.         }
  9.     }
  10.     void quizGame()
  11.     {
  12.         quizGame.setSize(500, 700);
  13.         quizGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  14.         quizGame.setVisible(true);
  15.         quizGame.setLayout(null);
  16.         try {
  17.             BufferedReader br = new BufferedReader(new FileReader("question.txt"));
  18.             String question=br.readLine();
  19.             String[] questionOption;
  20.             while(question!=null)
  21.             {
  22.                 questionOption=question.split("-");
  23.                 IQ iq = new IQ(questionOption[0],questionOption[1],questionOption[2],questionOption[3],questionOption[4],questionOption[5]);
  24.                 questionList.add(iq);
  25.                 question=br.readLine();
  26.             }
  27.         } catch (FileNotFoundException e) {
  28.             e.printStackTrace();
  29.         } catch (IOException e) {
  30.             e.printStackTrace();
  31.         }
  32.  
  33.         skip.setEnabled(true);
  34.         t.start();
  35.  
  36.         if ((questionList.get(i).Question==null)) {
  37.             i=0;
  38.         }
  39.         question1.setText(questionList.get(i).Question);
  40.         question1.setForeground(Color.white);
  41.         ans1.setText(questionList.get(i).ans1);
  42.         ans2.setText(questionList.get(i).ans2);
  43.         ans3.setText(questionList.get(i).ans3);
  44.         ans4.setText(questionList.get(i).ans4);
  45.         quizGame.add(question1);
  46.         question1.setVisible(true);
  47.         question1.setBounds(0, 0, 700, 80);
  48.         quizGame.add(ans1);
  49.         ans1.setVisible(true);
  50.         ans1.setBounds(0, 81, 200, 30);
  51.         ans1.addActionListener(new AnsChoice1());
  52.         quizGame.add(ans2);
  53.         ans2.setVisible(true);
  54.         ans2.setBounds(0, 161, 200, 30);
  55.         ans2.addActionListener(new AnsChoice2());
  56.         quizGame.add(ans3);
  57.         ans3.setVisible(true);
  58.         ans3.setBounds(0, 241, 200, 30);
  59.         ans3.addActionListener(new AnsChoice3());
  60.         quizGame.add(ans4);
  61.         ans4.setVisible(true);
  62.         ans4.setBounds(0, 321, 200, 30);
  63.         ans4.addActionListener(new AnsChoice4());
  64.  
  65.  
  66.         quizGame.add(correct);
  67.         correct.setBounds(0,361,100,40);
  68.         correct.setVisible(false);
  69.  
  70.         quizGame.add(wrong);
  71.         wrong.setBounds(0,401,150,30);
  72.         wrong.setVisible(false);
  73.  
  74.         quizGame.add(next);
  75.         next.setVisible(true);
  76.         next.setBounds(0,451,150,30);
  77.         next.addActionListener(new QuizGameNext());
  78.  
  79.         quizGame.add(skip);
  80.         skip.setVisible(true);
  81.         skip.setBounds(200,451,150,30);
  82.         skip.addActionListener(new SkipQuizGame());
  83.  
  84.         quizGame.add(time);
  85.         time.setBounds(0,600,200,30);
  86.         time.setVisible(true);
  87.  
  88.         quizGame.add(lifeLabel);
  89.         lifeLabel.setVisible(true);
  90.         lifeLabel.setBounds(200,501,150,30);
  91.  
  92.         JButton back = new JButton("Back");
  93.         quizGame.add(back);
  94.         back.setVisible(true);
  95.         back.setBounds(0,551,150,30);
  96.         back.addActionListener(new Back());
  97.  
  98.         quizGame.add(quizGameScore);
  99.         quizGameScore.setText("Score is: " + String.valueOf(iqHighScore0));
  100.         quizGameScore.setForeground(Color.green);
  101.         quizGameScore.setVisible(true);
  102.         quizGameScore.setBounds(0,501,150,30);
  103.  
  104.         quizGame.add(lifeLabel);
  105.         lifeLabel.setForeground(Color.green);
  106.         lifeLabel.setBounds(200,501,150,30);
  107.         lifeLabel.setVisible(true);
  108.         lifeLabel.setText("Life left: " + String.valueOf(life));
  109.  
  110.         quizGame.add(time);
  111.         time.setBounds(0,651,150,30);
  112.         time.setVisible(true);
  113.  
  114.         next.setEnabled(true);
  115.         skip.setEnabled(true);
  116.         submit.setEnabled(true);
  117.         ans.setEditable(true);
  118.  
  119.         life();
  120.         quizGame.add(bg);
  121.         bg.setVisible(true);
  122.         bg.setBounds(0,0,500,700);
  123.     }
  124.     public class QuizGameNext implements ActionListener{
  125.  
  126.         @Override
  127.         public void actionPerformed(ActionEvent actionEvent) {
  128.             i++;
  129.             quizGame();
  130.         }
  131.     }
  132.     public class SkipQuizGame implements ActionListener{
  133.  
  134.         @Override
  135.         public void actionPerformed(ActionEvent actionEvent) {
  136.             --life;
  137.             i++;
  138.             life();
  139.             quizGame();
  140.             lifeLabel.setText("Life left: " + String.valueOf(life));
  141.             lifeLabel.setForeground(Color.green);
  142.  
  143.         }
  144.     }
  145.     public class AnsChoice1 implements ActionListener {
  146.  
  147.         @Override
  148.         public void actionPerformed(ActionEvent actionEvent) {
  149.                 ansChoice=1;
  150.                 next.setEnabled(false);
  151.             if(ansChoice==1)
  152.             {
  153.                 if(questionList.get(i).correctAnswer.equals(questionList.get(i).ans1))
  154.                 {
  155.  
  156.                     correctAnswer();
  157.  
  158.                 } else{
  159.                     wrongAnswer();
  160.                 }
  161.                 game.dispose();
  162.             }
  163.  
  164.  
  165.         }
  166.     }
  167.  
  168.     public class AnsChoice2 implements ActionListener{
  169.  
  170.         @Override
  171.         public void actionPerformed(ActionEvent actionEvent) {
  172.             ansChoice=2;
  173.             if(ansChoice==2)
  174.             {
  175.                 next.setEnabled(false);
  176.                 if(questionList.get(i).correctAnswer.equals(questionList.get(i).ans2))
  177.                 {
  178.                     correctAnswer();
  179.  
  180.  
  181.                 } else{
  182.                     wrongAnswer();
  183.                 }
  184.                 game.dispose();
  185.             }
  186.  
  187.         }
  188.     }
  189.     public class AnsChoice3 implements ActionListener{
  190.  
  191.         @Override
  192.         public void actionPerformed(ActionEvent actionEvent) {
  193.             ansChoice=3;
  194.             if(ansChoice==3)
  195.             {
  196.                 if(questionList.get(i).correctAnswer.equals(questionList.get(i).ans3))
  197.                 {
  198.                     correctAnswer();
  199.  
  200.  
  201.             } else{
  202.                     wrongAnswer();
  203.             }
  204.                 game.dispose();
  205.  
  206.             }
  207.  
  208.  
  209.  
  210.  
  211.         }
  212.     }
  213.     public class AnsChoice4 implements ActionListener{
  214.  
  215.         @Override
  216.         public void actionPerformed(ActionEvent actionEvent) {
  217.             ansChoice=4;
  218.             if(ansChoice==4)
  219.             {
  220.  
  221.                 next.setEnabled(false);
  222.                 if(questionList.get(i).correctAnswer.equals(questionList.get(i).ans4))
  223.                 {
  224.                     correctAnswer();
  225.  
  226.  
  227.                 } else{
  228.                     wrongAnswer();
  229.                 }
  230.                 game.dispose();
  231.  
  232.             }
  233.  
  234.         }
  235.     }
  236.     void wrongAnswer(){
  237.         life--;
  238.         life();
  239.         wrong.setVisible(true);
  240.         correct.setVisible(false);
  241.         skip.setEnabled(true);
  242.         next.setEnabled(false);
  243.         wrong.setForeground(Color.red);
  244.         quizGameScore.setText("Score is: " + String.valueOf(iqHighScore0));
  245.         quizGameScore.setForeground(Color.green);
  246.         lifeLabel.setText("Life left: " + String.valueOf(life));
  247.         lifeLabel.setForeground(Color.green);
  248.  
  249.     }
  250.     void correctAnswer(){
  251.         life();
  252.         iqHighScore0++;
  253.         quizGame.add(correct);
  254.         correct.setVisible(true);
  255.         correct.setBounds(0,401,150,30);
  256.         wrong.setVisible(false);
  257.         next.setEnabled(true);
  258.         correct.setForeground(Color.GREEN);
  259.         quizGameScore.setText("Score is: " + String.valueOf(iqHighScore0));
  260.         quizGameScore.setForeground(Color.green);
  261.         lifeLabel.setText("Life left: " + String.valueOf(life));
  262.         lifeLabel.setForeground(Color.green);
  263.         if(iqHighScore0>iqHighScore){
  264.             iqHighScore=iqHighScore0;
  265.         }
  266.         submit.setEnabled(false);
  267.         skip.setEnabled(false);
  268.     }
  269.     public class Skip implements ActionListener{
  270.  
  271.         @Override
  272.         public void actionPerformed(ActionEvent actionEvent) {
  273.             life--;
  274.             lifeLabel.setText("Life left: " + String.valueOf(life));
  275.             lifeLabel.setForeground(Color.green);
  276.             life();
  277.  
  278.         }
  279.     }
Advertisement
Add Comment
Please, Sign In to add comment