Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class QuizGame implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent actionEvent) {
- quizGame();
- }
- }
- void quizGame()
- {
- quizGame.setSize(500, 700);
- quizGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- quizGame.setVisible(true);
- quizGame.setLayout(null);
- try {
- BufferedReader br = new BufferedReader(new FileReader("question.txt"));
- String question=br.readLine();
- String[] questionOption;
- while(question!=null)
- {
- questionOption=question.split("-");
- IQ iq = new IQ(questionOption[0],questionOption[1],questionOption[2],questionOption[3],questionOption[4],questionOption[5]);
- questionList.add(iq);
- question=br.readLine();
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- skip.setEnabled(true);
- t.start();
- if ((questionList.get(i).Question==null)) {
- i=0;
- }
- question1.setText(questionList.get(i).Question);
- question1.setForeground(Color.white);
- ans1.setText(questionList.get(i).ans1);
- ans2.setText(questionList.get(i).ans2);
- ans3.setText(questionList.get(i).ans3);
- ans4.setText(questionList.get(i).ans4);
- quizGame.add(question1);
- question1.setVisible(true);
- question1.setBounds(0, 0, 700, 80);
- quizGame.add(ans1);
- ans1.setVisible(true);
- ans1.setBounds(0, 81, 200, 30);
- ans1.addActionListener(new AnsChoice1());
- quizGame.add(ans2);
- ans2.setVisible(true);
- ans2.setBounds(0, 161, 200, 30);
- ans2.addActionListener(new AnsChoice2());
- quizGame.add(ans3);
- ans3.setVisible(true);
- ans3.setBounds(0, 241, 200, 30);
- ans3.addActionListener(new AnsChoice3());
- quizGame.add(ans4);
- ans4.setVisible(true);
- ans4.setBounds(0, 321, 200, 30);
- ans4.addActionListener(new AnsChoice4());
- quizGame.add(correct);
- correct.setBounds(0,361,100,40);
- correct.setVisible(false);
- quizGame.add(wrong);
- wrong.setBounds(0,401,150,30);
- wrong.setVisible(false);
- quizGame.add(next);
- next.setVisible(true);
- next.setBounds(0,451,150,30);
- next.addActionListener(new QuizGameNext());
- quizGame.add(skip);
- skip.setVisible(true);
- skip.setBounds(200,451,150,30);
- skip.addActionListener(new SkipQuizGame());
- quizGame.add(time);
- time.setBounds(0,600,200,30);
- time.setVisible(true);
- quizGame.add(lifeLabel);
- lifeLabel.setVisible(true);
- lifeLabel.setBounds(200,501,150,30);
- JButton back = new JButton("Back");
- quizGame.add(back);
- back.setVisible(true);
- back.setBounds(0,551,150,30);
- back.addActionListener(new Back());
- quizGame.add(quizGameScore);
- quizGameScore.setText("Score is: " + String.valueOf(iqHighScore0));
- quizGameScore.setForeground(Color.green);
- quizGameScore.setVisible(true);
- quizGameScore.setBounds(0,501,150,30);
- quizGame.add(lifeLabel);
- lifeLabel.setForeground(Color.green);
- lifeLabel.setBounds(200,501,150,30);
- lifeLabel.setVisible(true);
- lifeLabel.setText("Life left: " + String.valueOf(life));
- quizGame.add(time);
- time.setBounds(0,651,150,30);
- time.setVisible(true);
- next.setEnabled(true);
- skip.setEnabled(true);
- submit.setEnabled(true);
- ans.setEditable(true);
- life();
- quizGame.add(bg);
- bg.setVisible(true);
- bg.setBounds(0,0,500,700);
- }
- public class QuizGameNext implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent actionEvent) {
- i++;
- quizGame();
- }
- }
- public class SkipQuizGame implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent actionEvent) {
- --life;
- i++;
- life();
- quizGame();
- lifeLabel.setText("Life left: " + String.valueOf(life));
- lifeLabel.setForeground(Color.green);
- }
- }
- public class AnsChoice1 implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent actionEvent) {
- ansChoice=1;
- next.setEnabled(false);
- if(ansChoice==1)
- {
- if(questionList.get(i).correctAnswer.equals(questionList.get(i).ans1))
- {
- correctAnswer();
- } else{
- wrongAnswer();
- }
- game.dispose();
- }
- }
- }
- public class AnsChoice2 implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent actionEvent) {
- ansChoice=2;
- if(ansChoice==2)
- {
- next.setEnabled(false);
- if(questionList.get(i).correctAnswer.equals(questionList.get(i).ans2))
- {
- correctAnswer();
- } else{
- wrongAnswer();
- }
- game.dispose();
- }
- }
- }
- public class AnsChoice3 implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent actionEvent) {
- ansChoice=3;
- if(ansChoice==3)
- {
- if(questionList.get(i).correctAnswer.equals(questionList.get(i).ans3))
- {
- correctAnswer();
- } else{
- wrongAnswer();
- }
- game.dispose();
- }
- }
- }
- public class AnsChoice4 implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent actionEvent) {
- ansChoice=4;
- if(ansChoice==4)
- {
- next.setEnabled(false);
- if(questionList.get(i).correctAnswer.equals(questionList.get(i).ans4))
- {
- correctAnswer();
- } else{
- wrongAnswer();
- }
- game.dispose();
- }
- }
- }
- void wrongAnswer(){
- life--;
- life();
- wrong.setVisible(true);
- correct.setVisible(false);
- skip.setEnabled(true);
- next.setEnabled(false);
- wrong.setForeground(Color.red);
- quizGameScore.setText("Score is: " + String.valueOf(iqHighScore0));
- quizGameScore.setForeground(Color.green);
- lifeLabel.setText("Life left: " + String.valueOf(life));
- lifeLabel.setForeground(Color.green);
- }
- void correctAnswer(){
- life();
- iqHighScore0++;
- quizGame.add(correct);
- correct.setVisible(true);
- correct.setBounds(0,401,150,30);
- wrong.setVisible(false);
- next.setEnabled(true);
- correct.setForeground(Color.GREEN);
- quizGameScore.setText("Score is: " + String.valueOf(iqHighScore0));
- quizGameScore.setForeground(Color.green);
- lifeLabel.setText("Life left: " + String.valueOf(life));
- lifeLabel.setForeground(Color.green);
- if(iqHighScore0>iqHighScore){
- iqHighScore=iqHighScore0;
- }
- submit.setEnabled(false);
- skip.setEnabled(false);
- }
- public class Skip implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent actionEvent) {
- life--;
- lifeLabel.setText("Life left: " + String.valueOf(life));
- lifeLabel.setForeground(Color.green);
- life();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment