import java.awt.*;
import java.util.*;
public class AlphabetsColoured
{
private Color quescolour, anscolour;
private int x, y, width, height;
public void getQuestion(Graphics g,String alphabet,int x, int y, Color colour)
{
Graphics2D g2 = (Graphics2D) g;
Font font = new Font("Chalkduster", Font.BOLD+Font.PLAIN, 70);
g2.setFont(font);
g2.setColor(this.quescolour = colour);
g2.drawString(alphabet, x, y);
}
public void getAnswers(Graphics g, String alphabet,int x, int y, Color colour)
{
Graphics2D g2 = (Graphics2D) g;
Font font1 = new Font("Bloody", Font.BOLD+Font.PLAIN, 30);
g2.setFont(font1);
g2.setColor(this.anscolour = colour);
g2.drawString(alphabet, x, y);
}
public Color getQuestionColour()
{
if (this.quescolour.equals(Color.BLACK))
{
return Color.BLACK;
}
if (this.quescolour.equals(Color.PINK))
{
return Color.PINK;
}
if (this.quescolour.equals(Color.BLUE))
{
return Color.BLUE;
}
if (this.quescolour.equals(Color.GREEN))
{
return Color.GREEN;
}
if (this.quescolour.equals(Color.CYAN))
{
return Color.CYAN;
}
if (this.quescolour.equals(Color.WHITE))
{
return Color.WHITE;
}
if (this.quescolour.equals(Color.YELLOW))
{
return Color.YELLOW;
}
if (this.quescolour.equals(Color.RED))
{
return Color.RED;
}
if (this.quescolour.equals(Color.ORANGE))
{
return Color.ORANGE;
}
if (this.quescolour.equals(Color.GRAY))
{
return Color.GRAY;
}
return Color.MAGENTA;
}
public Color getAnswerColour()
{
if (this.anscolour.equals(Color.BLACK))
{
return Color.BLACK;
}
if (this.anscolour.equals(Color.PINK))
{
return Color.PINK;
}
if (this.anscolour.equals(Color.BLUE))
{
return Color.BLUE;
}
if (this.anscolour.equals(Color.GREEN))
{
return Color.GREEN;
}
if (this.anscolour.equals(Color.CYAN))
{
return Color.CYAN;
}
if (this.anscolour.equals(Color.WHITE))
{
return Color.WHITE;
}
if (this.anscolour.equals(Color.YELLOW))
{
return Color.YELLOW;
}
if (this.anscolour.equals(Color.RED))
{
return Color.RED;
}
if (this.anscolour.equals(Color.ORANGE))
{
return Color.ORANGE;
}
if (this.anscolour.equals(Color.GRAY))
{
return Color.GRAY;
}
return Color.MAGENTA;
}
public boolean ColourComparisonTest(AlphabetsColoured alpha1, AlphabetsColoured alpha2)
{
if (alpha1.getQuestionColour().equals(alpha2.getAnswerColour()) == true)
{
return true;
}
return false;
}
public Rectangle getAlphabetBounds()
{
return new Rectangle(x, y, width, height);
}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;
import java.awt.geom.*;
import java.util.*;
public class testAlphabets extends JPanel implements ActionListener, KeyListener
{
AlphabetsColoured ques = new AlphabetsColoured();
AlphabetsColoured ans1 = new AlphabetsColoured();
AlphabetsColoured ans2 = new AlphabetsColoured();
AlphabetsColoured ans3 = new AlphabetsColoured();
AlphabetsColoured ans4 = new AlphabetsColoured();
AlphabetsColoured comparison = new AlphabetsColoured();
private int y=0, vely=1; //alphabets velocity
private int x1=650, y1=650, velx1=5, vely1=5; //ball velocites
Timer timer = new Timer(10, this);
public testAlphabets()
{
timer.start();
addKeyListener(this);
this.setFocusTraversalKeysEnabled(false);
this.setFocusable(true);
}
public void paintComponent(Graphics g)
{
ques.getQuestion(g, "Red", 20, y, Color.cyan);
ans1.getAnswers(g, "Orange", 400, y+20, Color.ORANGE);
ans2.getAnswers(g, "Yellow", 600, y-20, Color.PINK);
ans3.getAnswers(g, "Green", 800, y+50, Color.BLUE);
ans4.getAnswers(g, "Blue", 1000, y-50, Color.cyan);
g.fillOval(x1, y1, 50, 50);
if(comparison.ColourComparisonTest(ques, ans1) == true && Collides(g) == true && (y1==y+20))
{
g.setColor(Color.ORANGE);
g.drawString("You hit orange" , 500, 500);
try{Thread.sleep(1000);} catch(InterruptedException ex) {System.err.println(ex.getMessage());}
}
if(comparison.ColourComparisonTest(ques, ans2) == true && Collides(g) == true && (y1==y-20))
{
g.setColor(Color.YELLOW);
g.drawString("You hit yellow" , 500, 500);
try{Thread.sleep(1000);} catch(InterruptedException ex) {System.err.println(ex.getMessage());}
}
if(comparison.ColourComparisonTest(ques, ans3) == true && Collides(g) == true && (y1==y+50))
{
g.setColor(Color.GREEN);
g.drawString("You hit green" , 500, 500);
try{Thread.sleep(1000);} catch(InterruptedException ex) {System.err.println(ex.getMessage());}
}
if(comparison.ColourComparisonTest(ques, ans4) == true && Collides(g) == true && (y1==y-50))
{
g.setColor(Color.BLUE);
g.drawString("You hit blue" , 500, 500);
try{Thread.sleep(1000);} catch(InterruptedException ex) {System.err.println(ex.getMessage());}
} ///this part of the code is for collision detection of the oval with the 4 colours buts its not working. please help me. im not sure if my Collides and ColourComparisonTest methods are correct. thanks
}
public void actionPerformed(ActionEvent e)
{
if(y < 0)
{
vely++;
}
if(y > 800)
{
vely--;
}
if(x1 < 0 || y1 < 0)
{
velx1++;
vely1++;
}
if(x1 > 1450 || y1 > 750)
{
velx1--;
vely1--;
}
repaint();
y += vely;
x1 += velx1;
y1 += vely1;
}
public void up() {
velx1 = 0;
vely1 = -4;
}
public void down() {
velx1 = 0;
vely1 = 4;
}
public void left() {
velx1 = -4;
vely1 = 0;
}
public void right() {
velx1 = 4;
vely1 = 0;
}
public void keyPressed(KeyEvent e)
{
int keycode = e.getKeyCode();
if(keycode == KeyEvent.VK_UP)
{
up();
}
if(keycode == KeyEvent.VK_DOWN)
{
down();
}
if(keycode == KeyEvent.VK_LEFT)
{
left();
}
if(keycode == KeyEvent.VK_RIGHT)
{
right();
}
}
public void keyTyped(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
velx1 = 0;
vely1 = 0;
}
public boolean Collides(Graphics g)
{
if(ans1.getAlphabetBounds().intersects(g.getClipBounds()))
{
return true;
}
if(ans2.getAlphabetBounds().intersects(g.getClipBounds()))
{
return true;
}
if(ans3.getAlphabetBounds().intersects(g.getClipBounds()))
{
return true;
}
if(ans4.getAlphabetBounds().intersects(g.getClipBounds()))
{
return true;
}
return false;
}
public static void main(String args [])
{
JFrame f = new JFrame();
f.setSize(1500, 850);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
new testAlphabets();
testAlphabets ggg = new testAlphabets();
f.add(ggg);
}
}