Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.Color;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Random;
- public class Gui {
- public static ArrayList<IQ> questionList = new ArrayList<IQ>();
- JFrame game = new JFrame("Quiz");
- JButton back = new JButton("Back");
- JFrame gameOver = new JFrame();
- JFrame gameOption = new JFrame("Choose Game");
- JFrame highScoreOption = new JFrame("Choose High Score");
- JFrame mathGame = new JFrame("Math Game");
- JFrame quizGame = new JFrame("Quiz Game");
- JLabel firstNumberDigit = new JLabel();
- JLabel wrong = new JLabel ("Wrong Answer");
- JLabel correct = new JLabel ("Correct Answer");
- JLabel secondNumberDigit = new JLabel();
- JTextField ans = new JTextField();
- static JButton next = new JButton("Next");
- static JButton skip = new JButton("Skip");
- JLabel secondNumber = new JLabel();
- JLabel firstNumber = new JLabel();
- Random random = new Random();
- JLabel math = new JLabel();
- JLabel mathChoice = new JLabel();
- JLabel lifeLabel = new JLabel();
- Timer t = new Timer(1000,new TimeShow());
- static JButton submit = new JButton("Submit");
- JLabel time = new JLabel();
- JLabel loss = new JLabel(new ImageIcon("loss.jpg"));
- JLabel image = new JLabel(new ImageIcon("game.png"));
- JLabel bg = new JLabel(new ImageIcon("bg.jpg"));
- JLabel quizGameScore = new JLabel();
- JLabel question1 = new JLabel();
- JButton ans1 = new JButton();
- JButton ans2 = new JButton();
- JButton ans3 = new JButton();
- JButton ans4 = new JButton();
- boolean correctAnswer = false;
- //String playerNameMath="";
- //String playerNameIQ="";
- int first,second;
- int sum,choice,i=0;
- static int ansChoice=0;
- int ansValue,mathHighScore0=0,mathHighScore=0,life=10,timeValue=60,iqHighScore=0,iqHighScore0=0;
- JLabel mathScore = new JLabel();
- JLabel IQ = new JLabel();
- public Gui()
- {
- game.setSize(500,281);
- game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- game.setVisible(true);
- game.setLayout(null);
- JButton start = new JButton("Start");
- game.add(start);
- //start.setBackground(Color.red);
- start.setBounds(10,10,150,30);
- start.setVisible(true);
- JButton highScore = new JButton("High Score");
- game.add(highScore);
- highScore.setBounds(10,40,150,30);
- highScore.setVisible(true);
- start.addActionListener(new GameOption());
- highScore.addActionListener(new HighScoreOption());
- game.add(image);
- image.setBounds(0,0,500,281);
- image.setVisible(true);
- gameOver.setSize(625,508);
- gameOver.add(back);
- back.setBounds(0,400,100,30);
- back.addActionListener(new Back());
- gameOver.add(time);
- time.setVisible(true);
- time.setBounds(0,0,150,50);
- time.setForeground(Color.red);
- gameOver.add(lifeLabel);
- lifeLabel.setBounds(0,51,150,50);
- lifeLabel.setForeground(Color.red);
- gameOver.add(loss);
- loss.setBounds(0,0,625,508);
- loss.setVisible(true);
- }
- public static void main(String[] args) {
- new Gui();
- }
- public class TimeShow implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent actionEvent) {
- timeValue--;
- time.setText("Time Left: " + String.valueOf(timeValue) + "s");
- time.setForeground(Color.green);
- if(timeValue<=0)
- {
- t.stop();
- time.setText("No Time left. Restart the game");
- time.setForeground(Color.red);
- next.setEnabled(false);
- skip.setEnabled(false);
- submit.setEnabled(false);
- ans.setEditable(false);
- gameOver.setVisible(true);
- mathGame.dispose();
- if(life<=0)
- {
- lifeLabel.setText("No life left. Restart the game");
- lifeLabel.setForeground(Color.red);
- next.setEnabled(false);
- skip.setEnabled(false);
- submit.setEnabled(false);
- ans.setEditable(false);
- t.stop();
- gameOver.setVisible(true);
- mathGame.dispose();
- }
- }
- }
- }
- public class GameOption implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent actionEvent) {
- gameOption.setSize(500,281);
- gameOption.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- gameOption.setVisible(true);
- gameOption.setLayout(null);
- JButton quiz = new JButton("IQ Quiz");
- gameOption.add(quiz);
- quiz.setBounds(10,10,150,30);
- quiz.setVisible(true);
- quiz.setEnabled(true);
- JButton math = new JButton("Math Quiz");
- gameOption.add(math);
- math.setBounds(10,40,150,30);
- math.setVisible(true);
- math.addActionListener(new MathGame());
- quiz.addActionListener(new QuizGame());
- game.dispose();
- gameOption.add(image);
- image.setBounds(0,0,500,281);
- image.setVisible(true);
- }
- }
- public class HighScoreOption implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent actionEvent) {
- highScoreOption.setSize(500,281);
- highScoreOption.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- highScoreOption.setVisible(true);
- highScoreOption.setLayout(null);
- IQ.setText("IQ Quiz High Score: ");
- highScoreOption.add(IQ);
- IQ.setBounds(10,10,150,30);
- IQ.setVisible(true);
- math.setText("Math Game High Score: " + mathHighScore);
- highScoreOption.add(math);
- math.setBounds(10,40,150,30);
- math.setVisible(true);
- highScoreOption.add(back);
- back.setVisible(true);
- back.setBounds(0,551,150,30);
- back.addActionListener(new Back());
- game.dispose();
- highScoreOption.add(image);
- image.setBounds(0,0,500,281);
- image.setVisible(true);
- }
- }
- public class MathGame implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent actionEvent) {
- t.start();
- first = random.nextInt(99) + 1;
- second= random.nextInt(99) + 1;
- while(second>first)
- {
- first = random.nextInt(99) + 1;
- second= random.nextInt(99) + 1;
- }
- choice = random.nextInt(4)+1;
- mathGame.setSize(500,700);
- mathGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- mathGame.setVisible(true);
- mathGame.setLayout(null);
- firstNumber.setText("First Number");
- firstNumber.setForeground(Color.white);
- mathGame.add(firstNumber);
- firstNumber.setVisible(true);
- firstNumber.setBounds(0,0,150,30);
- //firstNumberDigit = new JLabel(String.valueOf(first));
- firstNumberDigit.setText(String.valueOf(first));
- firstNumberDigit.setForeground(Color.white);
- mathGame.add(firstNumberDigit);
- firstNumberDigit.setBounds(0,51,150,30);
- firstNumberDigit.setVisible(true);
- secondNumber.setText("Second Number");
- secondNumber.setForeground(Color.white);
- mathGame.add(secondNumber);
- secondNumber.setVisible(true);
- secondNumber.setBounds(151,0,150,30);
- //secondNumberDigit = new JLabel(String.valueOf(second));
- secondNumberDigit.setText(String.valueOf(second));
- secondNumberDigit.setForeground(Color.white);
- mathGame.add(secondNumberDigit);
- secondNumberDigit.setVisible(true);
- secondNumberDigit.setBounds(151,51,150,30);
- mathGame.add(ans);
- ans.setBounds(0,301,150,30);
- mathGame.add(submit);
- submit.setVisible(true);
- submit.setBounds(0,351,100,30);
- submit.addActionListener(new Ans());
- mathGame.add(next);
- next.setVisible(true);
- next.setBounds(0,451,150,30);
- next.addActionListener(this);
- mathGame.add(skip);
- skip.setVisible(true);
- skip.setBounds(200,451,150,30);
- skip.addActionListener(new Skip());
- mathGame.add(mathChoice);
- mathChoice.setVisible(true);
- mathChoice.setBounds(0,201,100,30);
- mathChoice.setForeground(Color.white);
- if(choice==1)
- {
- sum=first+second;
- mathChoice.setText("Addition");
- }
- else if(choice==2)
- {
- sum=first-second;
- mathChoice.setText("Substraction");
- }
- else if(choice==3)
- {
- sum=first*second;
- mathChoice.setText("Multiplication");
- }
- else if(choice==4)
- {
- sum=first/second;
- mathChoice.setText("Division");
- }
- next.setEnabled(false);
- mathScore.setText("Score is: " + String.valueOf(mathHighScore0));
- mathScore.setForeground(Color.white);
- mathGame.add(mathScore);
- mathScore.setVisible(true);
- mathScore.setBounds(0,501,150,30);
- lifeLabel.setText("Life left: " + String.valueOf(life));
- lifeLabel.setForeground(Color.green);
- mathGame.add(lifeLabel);
- lifeLabel.setVisible(true);
- lifeLabel.setBounds(200,501,150,30);
- JButton back = new JButton("Back");
- mathGame.add(back);
- back.setVisible(true);
- back.setBounds(0,551,150,30);
- back.addActionListener(new Back());
- mathGame.add(time);
- time.setBounds(0,600,200,30);
- time.setVisible(true);
- if(life<=0)
- {
- lifeLabel.setText("No life left. Restart the game");
- lifeLabel.setForeground(Color.red);
- next.setEnabled(false);
- skip.setEnabled(false);
- submit.setEnabled(false);
- ans.setEditable(false);
- t.stop();
- gameOver.setVisible(true);
- mathGame.dispose();
- }
- mathGame.add(bg);
- bg.setVisible(true);
- bg.setBounds(0,0,500,700);
- gameOption.dispose();
- next.setEnabled(true);
- skip.setEnabled(true);
- submit.setEnabled(true);
- }
- }
- public class QuizGame implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent actionEvent) {
- 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();
- 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.setVisible(false);
- correct.setBounds(0,361,100,40);
- quizGame.add(wrong);
- wrong.setVisible(true);
- correct.setBounds(0,361,100,40);
- quizGame.add(next);
- next.setVisible(true);
- next.setBounds(0,451,150,30);
- next.addActionListener(this);
- quizGame.add(skip);
- skip.setVisible(true);
- skip.setBounds(200,451,150,30);
- skip.addActionListener(new Skip());
- 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());
- quizGameScore.setText("Score is: " + String.valueOf(iqHighScore0));
- quizGameScore.setForeground(Color.green);
- lifeLabel.setForeground(Color.green);
- next.setEnabled(true);
- skip.setEnabled(true);
- submit.setEnabled(true);
- ans.setEditable(true);
- t.stop();
- if(life<=0) {
- gameOver.setVisible(true);
- mathGame.dispose();
- }
- quizGame.add(bg);
- bg.setVisible(true);
- bg.setBounds(0,0,500,700);
- i++;
- }
- }
- public class AnsChoice1 implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent actionEvent) {
- ansChoice=1;
- if(ansChoice==1)
- {
- next.setEnabled(false);
- if(questionList.get(i).correctAnswer.equals(questionList.get(i).ans4))
- {
- 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));
- lifeLabel.setText("Life left: " + String.valueOf(life));
- lifeLabel.setForeground(Color.green);
- skip.setEnabled(false);
- if(iqHighScore0>iqHighScore){
- iqHighScore=iqHighScore0;
- }
- submit.setEnabled(false);
- skip.setEnabled(false);
- } else{
- life--;
- quizGame.add(wrong);
- wrong.setVisible(true);
- wrong.setBounds(0,401,150,30);
- correct.setVisible(false);
- next.setEnabled(false);
- wrong.setForeground(Color.red);
- quizGameScore.setText("Score is: " + String.valueOf(iqHighScore0));
- lifeLabel.setText("Life left: " + String.valueOf(life));
- lifeLabel.setForeground(Color.green);
- }
- 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))
- {
- 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);
- lifeLabel.setForeground(Color.green);
- if(iqHighScore0>iqHighScore){
- iqHighScore=iqHighScore0;
- }
- submit.setEnabled(false);
- skip.setEnabled(false);
- } else{
- life--;
- quizGame.add(wrong);
- wrong.setVisible(true);
- wrong.setBounds(0,401,150,30);
- correct.setVisible(false);
- //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);
- }
- game.dispose();
- }
- }
- }
- public class AnsChoice3 implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent actionEvent) {
- ansChoice=3;
- if(ansChoice==3)
- next.setEnabled(false);
- if(questionList.get(i).correctAnswer.equals(questionList.get(i).ans3))
- {
- 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);
- lifeLabel.setForeground(Color.green);
- skip.setEnabled(false);
- if(iqHighScore0>iqHighScore){
- iqHighScore=iqHighScore0;
- }
- submit.setEnabled(false);
- skip.setEnabled(false);
- } else{
- life--;
- quizGame.add(wrong);
- wrong.setVisible(true);
- wrong.setBounds(0,401,150,30);
- correct.setVisible(false);
- 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);
- lifeLabel.setForeground(Color.green);
- }
- 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))
- {
- 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);
- lifeLabel.setForeground(Color.green);
- skip.setEnabled(false);
- if(iqHighScore0>iqHighScore){
- iqHighScore=iqHighScore0;
- }
- submit.setEnabled(false);
- skip.setEnabled(false);
- } else{
- life--;
- quizGame.add(wrong);
- wrong.setVisible(true);
- wrong.setBounds(0,401,150,30);
- correct.setVisible(false);
- //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);
- }
- game.dispose();
- }
- }
- }
- public class Skip implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent actionEvent) {
- life--;
- lifeLabel.setText("Life left: " + String.valueOf(life));
- lifeLabel.setForeground(Color.green);
- if(life<=0)
- {
- lifeLabel.setText("No life left. Restart the game");
- lifeLabel.setForeground(Color.red);
- next.setEnabled(false);
- skip.setEnabled(false);
- submit.setEnabled(false);
- ans.setEditable(false);
- t.stop();
- gameOver.setVisible(true);
- mathGame.dispose();
- quizGame.dispose();
- }
- }
- }
- class Ans implements ActionListener
- {
- public void actionPerformed(ActionEvent actionEvent) {
- {
- next.setEnabled(false);
- ansValue = Integer.parseInt(ans.getText());
- if(ansValue==sum)
- {
- mathHighScore0++;
- mathGame.add(correct);
- correct.setVisible(true);
- correct.setBounds(0,401,150,30);
- wrong.setVisible(false);
- firstNumberDigit.setVisible(false);
- secondNumberDigit.setVisible(false);
- next.setEnabled(true);
- correct.setForeground(Color.GREEN);
- mathScore.setText("Score is: " + String.valueOf(mathHighScore0));
- lifeLabel.setText("Life left: " + String.valueOf(life));
- lifeLabel.setForeground(Color.green);
- //skip.setEnabled(false);
- if(mathHighScore0>mathHighScore){
- mathHighScore=mathHighScore0;
- }
- submit.setEnabled(false);
- skip.setEnabled(false);
- }
- else{
- life--;
- mathGame.add(wrong);
- wrong.setVisible(true);
- wrong.setBounds(0,401,150,30);
- correct.setVisible(false);
- //next.setEnabled(false);
- wrong.setForeground(Color.red);
- mathScore.setText("Score is: " + String.valueOf(mathHighScore0));
- lifeLabel.setText("Life left: " + String.valueOf(life));
- lifeLabel.setForeground(Color.green);
- }
- game.dispose();
- }
- }
- }
- class Back implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- new Gui();
- gameOver.dispose();
- gameOption.dispose();
- highScoreOption.dispose();
- mathGame.dispose();
- quizGame.dispose();
- mathHighScore0=0;
- life=10;
- timeValue=60;
- }
- }
- }
Add Comment
Please, Sign In to add comment