Trivia Game
<< Timer Applicaiton | Applications | Ten Apples Puzzle >>
This is a beginning of a trivia game. Click the Q for a new question.
To Do:
- Check to see if the answer is correct
- Keep score
- Make the questions an ArrayList rather than an array, so you can remove a question once it has been asked.
Question.java
public class Question
{
// instance variables - replace the example below with your own
private String question, answer;
private String[] distractors;
/**
* Constructor for objects of class Question
*/
public Question(String q, String a, String d1, String d2, String d3 )
{
question = q;
answer= a;
distractors = new String[3];
distractors[0] = d1;
distractors[1] = d2;
distractors[2] = d3;
}
public void assignChoices(Choice[] cArray){
String[] arr=new String[4];
arr[3]=answer;
for(int i=0; i<distractors.length; i++)
arr[i]=distractors[i];
//shuffle 5 to 25 times
int n = 5+(int)(20*Math.random());
for(int i=0;i<n;i++){
swap(arr);
}
// load up choice array
for (int i=0; i< arr.length; i++)
cArray[i].setText(arr[i]);
}
public void swap(String[] arr){
int a = (int)(arr.length*Math.random());
int b = (int)(arr.length*Math.random());
String temp=arr[a];
arr[a]=arr[b];
arr[b]=temp;
}
public String getQuestion(){
return question;
}
public String getAnswer(){
return question;
}
public boolean isCorrect(String s){
return s.equals(answer);
}
}
Choice.java
import java.awt.*;
public class Choice
{
// instance variables - replace the example below with your own
private int x, y, size;
private String letter, text;
private Rectangle box;
/**
* Constructor for objects of class Choice
*/
public Choice(int x, int y, int size, String letter)
{
// initialise instance variables
this.x = x;
this.y =y;
this.letter=letter;
box = new Rectangle (x,y, size, size);
text="";
}
public void draw(Graphics g){
g.setColor(Color.BLACK);
g.fillRect(box.x, box.y, box.width, box.height);
g.setColor(Color.YELLOW);
g.fillRect(box.x+5, box.y+5, box.width-10, box.height-10);
g.setColor(Color.BLACK);
g.setFont(new Font("Sans", Font.BOLD, 20));
g.drawString(letter, box.x+30, box.y+45);
g.setFont(new Font("Sans", Font.BOLD, 18));
g.drawString(text, box.x+box.width+20, box.y+45);
}
public boolean contains(int x, int y){
return box.contains(x,y);
}
public String getLetter(){return letter;}
public String getText(){return text;}
public void setText(String t){ text=t;}
}
TriviaGame.java
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TriviaGame extends JPanel implements MouseListener
{
public static int WIDTH=800;
public static int HEIGHT=600;
private Font titleFont, regularFont;
private int x,y;
private Choice[] choices;
private Choice nextButton;
private String chosen;
private Question[] questions;
public TriviaGame()
{
//initialize variables here...
titleFont = new Font("Roman", Font.BOLD, 18);
regularFont = new Font("Helvetica", Font.PLAIN, 12);
x=0;
y=0;
chosen="";
nextButton = new Choice (30, 50, 75, "Q");
choices = new Choice[4];
for (int i=0; i<choices.length; i++){
choices[i]=new Choice(30, 155+i*100, 75, "ABCD".substring(i, i+1) );
}
loadQuestions();
}
public void loadQuestions(){
questions = new Question[3];
questions[0]=new Question("What is 2 + 2?",
"4",
"0", "1", "2" );
questions[1]=new Question("What is 2 squared?",
"4",
"0", "1", "2" );
questions[2]=new Question("What is 54 % 10?",
"4",
"0", "1", "2" );
}
public static void main(String[] args) {
TriviaGame app= new TriviaGame();
JFrame window = new JFrame("Trivia Game");
window.setSize(WIDTH, HEIGHT);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(app);
window.getContentPane().addMouseListener(app);
window.setVisible(true);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(),getHeight());
g.setColor(Color.BLUE);
g.setFont(titleFont);
g.drawString("Trivia Game", 20, 20);
g.drawString("X = "+x+" Y = "+y+" CHOSEN ="+chosen, 20, 560);
g.setColor(Color.BLACK);
g.setFont(regularFont);
g.drawString("Click on 'Q' for a new question", 20, 40);
g.setColor(Color.BLACK);
g.setFont(titleFont);
for (Choice c:choices){
c.draw(g);
}
nextButton.draw(g);
}
// update is a workaround to cure Windows screen flicker problem
public void update(Graphics g){
paint(g);
}
/**
* These are the Methods needed to implement the MouseListener Interface
*/
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
x=e.getX();
y=e.getY();
chosen ="";
for(Choice c :choices)
if (c.contains(x, y))
chosen=c.getLetter();
if (nextButton.contains(x,y) ){
// get new question
int r = (int)(questions.length*Math.random());
nextButton.setText(questions[r].getQuestion() );
questions[r].assignChoices(choices);
}
repaint();
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
