Fool Game in java
Fool Game
Game details:
The Game looks like this. There is one label asking the question whether you are a fool or not and two Buttons Yes and No.If you click on the Yes Button, the following thing happens. Clicking on the Again Button, the game restarts. Clicking on the Exit Button the game Exits.
If you try to click on the No Button, the Button jumps to another location randomly. If only you are not a fool, you will be able to click on the No Button and the following thing will happen. Clicking on the Again Button, the game restarts. Clicking on the Exit Button the game Exits
solution
part 1import java.lang.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Game extends JFrame implements MouseListener,ActionListener,KeyListener{
private JLabel textLable,yesText,noText;
private JButton yes,no,again,exit;
private JPanel panel;
public Game() {
super("Foolish Game");
this.setSize(800, 450);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
panel = new JPanel();
panel.setLayout(null);
panel.setBackground(Color.decode("#11999e"));
textLable = new JLabel("Are you a Fool..?");
textLable.setBounds(300, 50, 200, 90);
textLable.setFont(new Font("Serif",Font.PLAIN,20));
textLable.addKeyListener(this);
panel.add(textLable);
yesText = new JLabel(" Yes,You are a Fool");
yesText.setFont(new Font("Serif",Font.PLAIN,20));
yesText.setBounds(300, 150, 200, 35);
yesText.setBackground(Color.red);
yesText.setOpaque(true);
//yesText.setBackground(Color.blue);
panel.add(yesText);
yesText.setVisible(false);
//yesText.addKeyListener(this);
noText = new JLabel(" You are Not a Fool");
noText.setFont(new Font("Serif",Font.PLAIN,20));
noText.setBounds(300, 150, 200, 35);
noText.setBackground(Color.red);
noText.setOpaque(true);
panel.add(noText);
noText.setVisible(false);
//noText.addKeyListener(this);
yes = new JButton("YES");
yes.setBounds(280, 190, 120, 50);
yes.setBackground(Color.green);
yes.addMouseListener(this);
yes.addActionListener(this);
yes.addKeyListener(this);
panel.add(yes);
again = new JButton("Again");
again.setBounds(280, 190, 120, 50);
//again.setBackground(Color.green);
again.addMouseListener(this);
again.addActionListener(this);
again.setBackground(Color.CYAN);
panel.add(again);
again.setVisible(false);
no = new JButton("NO");
no.setBounds(420, 190, 120, 50);
no.setBackground(Color.red);
no.addMouseListener(this);
no.addKeyListener(this);
panel.add(no);
exit = new JButton("Exit");
exit.setBounds(420, 190, 120, 50);
//exit.setBackground(Color.red);
exit.addMouseListener(this);
exit.addActionListener(this);
exit.setBackground(Color.CYAN);
panel.add(exit);
exit.setVisible(false);
this.add(panel);
}
public void mouseClicked(MouseEvent me){}
public void mousePressed(MouseEvent me){}
public void mouseReleased(MouseEvent me){}
public void mouseExited(MouseEvent me) {}
public void mouseEntered(MouseEvent me)
{
Random r=new Random();
if(me.getSource() == no)
{
for(int i=0;i<30;i++) {
int x =r.nextInt(300)*2;
int y=r.nextInt(200)+20;
no.setBounds(x, y, 120, 50);
}
}
else{}
}
public void actionPerformed(ActionEvent ae)
{
String Command = ae.getActionCommand();
if (Command.equals(yes.getText())){
yesText.setVisible(true);
yes.setVisible(false);
no.setVisible(false);
again.setVisible(true);
exit.setVisible(true);
}
else if(Command.equals(no.getText()))
{
noText.setVisible(true);
yes.setVisible(false);
no.setVisible(false);
again.setVisible(true);
exit.setVisible(true);
}
else if(Command.equals(again.getText()))
{
again.setVisible(false);
exit.setVisible(false);
yesText.setVisible(false);
noText.setVisible(false);
textLable.setVisible(true);
yes.setVisible(true);
no.setVisible(true);
no.setBounds(420, 190, 120, 50);
}
else if(Command.equals(exit.getText())) {
System.exit(0);
}
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER) {
again.setVisible(true);
exit.setVisible(true);
yesText.setVisible(false);
textLable.setVisible(true);
yes.setVisible(false);
no.setVisible(false);
noText.setVisible(true);
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {
}
}
part-2:import java.lang.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Game extends JFrame implements MouseListener,ActionListener,KeyListener{
private JLabel textLable,yesText,noText;
private JButton yes,no,again,exit;
private JPanel panel;
public Game() {
super("Foolish Game");
this.setSize(800, 450);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
panel = new JPanel();
panel.setLayout(null);
panel.setBackground(Color.decode("#11999e"));
textLable = new JLabel("Are you a Fool..?");
textLable.setBounds(300, 50, 200, 90);
textLable.setFont(new Font("Serif",Font.PLAIN,20));
textLable.addKeyListener(this);
panel.add(textLable);
yesText = new JLabel(" Yes,You are a Fool");
yesText.setFont(new Font("Serif",Font.PLAIN,20));
yesText.setBounds(300, 150, 200, 35);
yesText.setBackground(Color.red);
yesText.setOpaque(true);
//yesText.setBackground(Color.blue);
panel.add(yesText);
yesText.setVisible(false);
//yesText.addKeyListener(this);
noText = new JLabel(" You are Not a Fool");
noText.setFont(new Font("Serif",Font.PLAIN,20));
noText.setBounds(300, 150, 200, 35);
noText.setBackground(Color.red);
noText.setOpaque(true);
panel.add(noText);
noText.setVisible(false);
//noText.addKeyListener(this);
yes = new JButton("YES");
yes.setBounds(280, 190, 120, 50);
yes.setBackground(Color.green);
yes.addMouseListener(this);
yes.addActionListener(this);
yes.addKeyListener(this);
panel.add(yes);
again = new JButton("Again");
again.setBounds(280, 190, 120, 50);
//again.setBackground(Color.green);
again.addMouseListener(this);
again.addActionListener(this);
again.setBackground(Color.CYAN);
panel.add(again);
again.setVisible(false);
no = new JButton("NO");
no.setBounds(420, 190, 120, 50);
no.setBackground(Color.red);
no.addMouseListener(this);
no.addKeyListener(this);
panel.add(no);
exit = new JButton("Exit");
exit.setBounds(420, 190, 120, 50);
//exit.setBackground(Color.red);
exit.addMouseListener(this);
exit.addActionListener(this);
exit.setBackground(Color.CYAN);
panel.add(exit);
exit.setVisible(false);
this.add(panel);
}
public void mouseClicked(MouseEvent me){}
public void mousePressed(MouseEvent me){}
public void mouseReleased(MouseEvent me){}
public void mouseExited(MouseEvent me) {}
public void mouseEntered(MouseEvent me)
{
Random r=new Random();
if(me.getSource() == no)
{
for(int i=0;i<30;i++) {
int x =r.nextInt(300)*2;
int y=r.nextInt(200)+20;
no.setBounds(x, y, 120, 50);
}
}
else{}
}
public void actionPerformed(ActionEvent ae)
{
String Command = ae.getActionCommand();
if (Command.equals(yes.getText())){
yesText.setVisible(true);
yes.setVisible(false);
no.setVisible(false);
again.setVisible(true);
exit.setVisible(true);
}
else if(Command.equals(no.getText()))
{
noText.setVisible(true);
yes.setVisible(false);
no.setVisible(false);
again.setVisible(true);
exit.setVisible(true);
}
else if(Command.equals(again.getText()))
{
again.setVisible(false);
exit.setVisible(false);
yesText.setVisible(false);
noText.setVisible(false);
textLable.setVisible(true);
yes.setVisible(true);
no.setVisible(true);
no.setBounds(420, 190, 120, 50);
}
else if(Command.equals(exit.getText())) {
System.exit(0);
}
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER) {
again.setVisible(true);
exit.setVisible(true);
yesText.setVisible(false);
textLable.setVisible(true);
yes.setVisible(false);
no.setVisible(false);
noText.setVisible(true);
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {
}
}
part 2
import java.lang.*;
public class Start{
public static void main(String[]args) {
Game g =new Game();
g.setVisible(true);
}
}
No comments