Blog about Programming Languages & Coding

Blog about Programming Languages & Coding
Contents for Computer Science, IT, B.Sc. CS & IT, M.Sc. CS & IT, MCA, BE CS & IT, ME CS & IT , Interview Questions, Books and Online Course Recommendations from Udemy, Coursera, etc

Quiz Game in Java

Quiz Game in Java

 Technology Used in This Project:

JAVA SWING & JAVA AWT are the two toolkits for building interactive Graphical User Interfaces. (GUI)

This game consists of 4 frames:

START WINDOW:

CODE:

(START WINDOW):

For the first window, we have a JFrame in which we added a picture on left corner and a JTextField followed by two JButtons Rules and Next.

JLabel Code:


JButton Code:


RULES.java:


 

This screen shows us the rules of the game and contains two buttons.

Back and Start Quiz.

· The Back button takes you to the previous screen.

· The Start Quiz button takes you to the quiz screen.

When we press the Start Quiz button the username is being passed on the Quiz page.

There is not a lot happening on this Screen its just an introductory passage.

 Youtube Video Link

CODE:

package quizgamebyatr;

 

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

/**

 *

 * @author Atiq Tushal Rohit

 */

public class Rules extends JFrame implements ActionListener {

    

    JButton b1, b2;

    String username;

    Rules(String username){

        this.username= username;

        setBounds(100, 50, 1100, 572 );

        getContentPane().setBackground(Color.lightGray);

        setLayout(null);

        

       JLabel l1 = new JLabel("Welcome "+username + " to QuizbyATR");

       l1.setForeground(Color.red);

       l1.setFont(new Font("Viner Hand ITC", Font.BOLD, 28));

       l1.setBounds(320, 20, 700, 30);

       add(l1);

       //labels for RULES

       JLabel l2 = new JLabel("");

       l2.setForeground(Color.black);

       l2.setFont(new Font("Tahoma", Font.PLAIN, 16));

       l2.setBounds(350, 0, 400, 550);

       l2.setText(

        "<html>"+

                "1. All questions are compulsory."+ "<br><br>"+

                "2. There are only 30 seconds for each question."+ "<br><br>"+

                "3. Do not open any tab aur press back button."+ "<br><br>"+

                "4. There is no any negative marking."+ "<br><br>"+

                "5. There are total 10 quesions each 10 marks."+ "<br><br>"+

        "</html>"

       );

       add(l2);

       

       //Back button

       b1 = new JButton("Back");

       b1.setBounds(350, 400, 100, 30);

       b1.setBackground(Color.lightGray);

       b1.setForeground(Color.black);

       b1.addActionListener(this);

       add(b1);

       

       //next button

       b2 = new JButton("Start Quiz");

       b2.setBounds(575, 400, 100, 30);

       b2.setBackground(Color.red);

       b2.setForeground(Color.black);

       b2.addActionListener(this);

       add(b2);

        setVisible(true);

    }

    

    public static void main(String[] args){

        new Rules("");

    }

 

    @Override

    public void actionPerformed(ActionEvent ae) {

        if(ae.getSource() == b1){

            this.setVisible(false);

            new QuizGamebyatr().setVisible(true);

        }else if(ae.getSource() == b2){

            new Quiz(username).setVisible(true);

        }

    }

}

 Questions window:

In this Frame we have 4 sections:



At Top we have one Picture (QUIZ BANNER).

Question and Multiple options (Radio Buttons)

 

Three JButtons at extreme Bottom Left (i.e. LIFELINE, NEXT, SUBMIT).

     

At Middle right we have Timer of 15 seconds.

 

Score.java:

  

 

This page shows us the score and offers us an option to check the score by using a players name.

A jdbc connection is made and the score and name of the player is stored in a mysql database.

As soon as the submit button is pressed the data is saved to the database.

We can use the Check button to check the score by using the name:

 

Code for Storing Data into DATABASE:

try {

            Class.forName("com.mysql.cj.jdbc.Driver");

            con = DriverManager.getConnection("jdbc:mysql://localhost/batch2","root","Ehacker1#");

            System.out.println("Connection created");

            ps = con.prepareStatement("insert into quiz values(?,?);");

            ps.setString(1, username);

            ps.setInt(2, score);

            ps.executeUpdate();

            ps.close();

}

        

catch (Exception e1) {e1.printStackTrace();}

 

 

 

 

 

 

 

CODE for retrieving the Data:

try {

            String name = JOptionPane.showInputDialog(b1, "Enter name: ");

            ps = con.prepareStatement("select * from quiz where name=?");

            ps.setString(1, name);

            rs = ps.executeQuery();

            if(rs.next()) {

            JOptionPane.showMessageDialog(b1, rs.getString(2));

            }

            else {

            JOptionPane.showMessageDialog(b1, "Wrong Credentials.");

            }

}

catch (Exception e1) {

e1.printStackTrace();

}

 

 

 

 

 

CODE:

package quizgamebyatr;

 

/**

 *

 * @author Atiq Tushal Rohit

 */

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

 

public class Score extends JFrame implements ActionListener{

    

PreparedStatement ps;

Connection con;

ResultSet rs;

 

    

    Score(String username, int score){

        setBounds(100, 50, 1100, 572);

        getContentPane().setBackground(Color.lightGray);

        setLayout(null);

        

        try {

            Class.forName("com.mysql.cj.jdbc.Driver");

            con = DriverManager.getConnection("jdbc:mysql://localhost/batch2","root","Ehacker1#");

            System.out.println("Connection created");

            ps = con.prepareStatement("insert into quiz values(?,?);");

            ps.setString(1, username);

            ps.setInt(2, score);

            ps.executeUpdate();

            ps.close();

}

        

catch (Exception e1) {e1.printStackTrace();}

        

        ImageIcon i1= new ImageIcon(ClassLoader.getSystemResource("quizgamebyatr/icons/score.jpg"));

        Image i2=i1.getImage().getScaledInstance(550, 572, Image.SCALE_DEFAULT);

        ImageIcon i3= new ImageIcon(i2);

        JLabel l1= new JLabel(i3);

        l1.setBounds(0, 0, 550, 572);

        add(l1);

        

        JLabel l2= new JLabel("Thank you For Playing.");

        l2.setBounds(650, 200, 300, 150);

        l2.setFont(new Font("RALEWAY", Font.PLAIN, 22));

        add(l2);

        

        JLabel l3= new JLabel("Your Score Is "+ score);

        l3.setBounds(720, 300, 200, 30);

        l3.setFont(new Font("Jokerman", Font.PLAIN, 22));

        l3.setForeground(Color.magenta);

        add(l3);

        

        JButton b1= new JButton("Check");

        b1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

try {

            String name = JOptionPane.showInputDialog(b1, "Enter name: ");

            ps = con.prepareStatement("select * from quiz where name=?");

            ps.setString(1, name);

            rs = ps.executeQuery();

            if(rs.next()) {

            JOptionPane.showMessageDialog(b1, rs.getString(2));

            }

            else {

            JOptionPane.showMessageDialog(b1, "Wrong Credentials.");

            }

}

catch (Exception e1) {

e1.printStackTrace();

}

}});

        b1.setBackground(Color.YELLOW);

        b1.setForeground(Color.BLACK);

        b1.setBounds(750, 400, 100, 50);

        add(b1);

        

    }

    

    public static void main(String[] args){

        new Score("", 0).setVisible(true);

    }

 

    @Override

    public void actionPerformed(ActionEvent ae) {     

        this.setVisible(false);

        new QuizGamebyatr().setVisible(true);

}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  



Quiz Game in Java Quiz Game in Java Reviewed by Asst. Prof. Sunita Rai on June 29, 2022 Rating: 5

No comments:

Powered by Blogger.