Adam Cruge
Broken In
I have written a Java code.The following is my code...
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
public class Game extends Frame{
public Panel x;
Panel pi,pi1,pi2,pi3,pi4;
public Game(){
pi=new Panel();
add(pi);
pi1= new Panel(new GridLayout(4,0));
pi2= new Panel();
pi3= new Panel(new GridLayout(4,4));
pi4= new Panel();
Label l3[][]=new Label[4][4];
x=pi1;
JButton b1=new JButton(" Play ");
JButton b2=new JButton(" Objective ");
JButton b3=new JButton(" Instruction ");
JButton b4=new JButton(" Exit ");
pi.add(pi1);
pi1.add(b1);
pi1.add(b2);
pi1.add(b3);
pi1.add(b4);
b1.addActionListener(new MyAction(this));
b2.addActionListener(new MyAction(this));
b3.addActionListener(new MyAction(this));
b4.addActionListener(new MyAction(this));
Label l1= new Label("This is a puzzle game.You have to arrange the numbers in proper order...");
pi2.add(l1);
JButton b5= new JButton(" Back ");
pi2.add(b5);
b5.addActionListener(new MyAction(this));
Label l2= new Label("You should use four arrow kyes to move the numbers...");
pi4.add(l2);
pi4.add(b5);
Panel p[][]= new Panel[4][4];
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
p[j]=new Panel();
pi3.add(p[j]);
p[j].addKeyListener(new MyKeyAdapter(this));
}
}
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
l3[j]=new Label(""+i);
}
}
p[0][0].add(l3[0][0]);
p[0][1].add(l3[0][3]);
p[0][2].add(l3[3][2]);
p[0][3].add(l3[1][2]);
p[1][0].add(l3[1][3]);
p[1][1].add(l3[2][1]);
p[1][2].add(l3[0][1]);
p[1][3].add(l3[2][2]);
p[2][0].add(l3[3][1]);
p[2][1].add(l3[0][2]);
p[2][2].add(l3[1][1]);
p[2][3].add(l3[3][0]);
p[3][0].add(l3[2][3]);
p[3][1].add(l3[2][0]);
p[3][2].add(l3[1][0]);
addWindowListener(new MyWindowAdapter());
}
public static void main(String args[])
{
Game g = new Game();
g.setSize(new Dimension(300, 200));
g.setTitle("A Puzzle Game");
g.setLayout(new FlowLayout(FlowLayout.CENTER));
g.setVisible(true);
}
}
class MyWindowAdapter extends WindowAdapter{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
class MyAction implements ActionListener{
Game g;
public MyAction(Game g)
{
this.g=g;
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
if(str.equals("Objective"))
{
g.pi.remove(g.x);
g.pi.add(g.pi2);
g.pi.x=g.pi2;
}
if(str.equals("Instruction"))
{
g.pi.remove(g.x);
g.pi.add(g.pi4);
g.pi.x=g.pi4;
}
if(str.equals("Exit"))
{
System.exit(0);
}
}
}
class MyKeyAdapter extends KeyAdapter{
Game g;
public MyKeyAdapter(Game g)
{
this.g=g;
}
public void keyTyped(KeyEvent ke)
{
}
}
This code is perfect when actionPerformed function in MyAction class is kept empty...
But I have to write something to obtain the desired result...I want to remove the Panels pi2,pi4 etc when buttons "Objective" or "Instructions" are pressed...
Please help ...
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
public class Game extends Frame{
public Panel x;
Panel pi,pi1,pi2,pi3,pi4;
public Game(){
pi=new Panel();
add(pi);
pi1= new Panel(new GridLayout(4,0));
pi2= new Panel();
pi3= new Panel(new GridLayout(4,4));
pi4= new Panel();
Label l3[][]=new Label[4][4];
x=pi1;
JButton b1=new JButton(" Play ");
JButton b2=new JButton(" Objective ");
JButton b3=new JButton(" Instruction ");
JButton b4=new JButton(" Exit ");
pi.add(pi1);
pi1.add(b1);
pi1.add(b2);
pi1.add(b3);
pi1.add(b4);
b1.addActionListener(new MyAction(this));
b2.addActionListener(new MyAction(this));
b3.addActionListener(new MyAction(this));
b4.addActionListener(new MyAction(this));
Label l1= new Label("This is a puzzle game.You have to arrange the numbers in proper order...");
pi2.add(l1);
JButton b5= new JButton(" Back ");
pi2.add(b5);
b5.addActionListener(new MyAction(this));
Label l2= new Label("You should use four arrow kyes to move the numbers...");
pi4.add(l2);
pi4.add(b5);
Panel p[][]= new Panel[4][4];
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
p[j]=new Panel();
pi3.add(p[j]);
p[j].addKeyListener(new MyKeyAdapter(this));
}
}
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
l3[j]=new Label(""+i);
}
}
p[0][0].add(l3[0][0]);
p[0][1].add(l3[0][3]);
p[0][2].add(l3[3][2]);
p[0][3].add(l3[1][2]);
p[1][0].add(l3[1][3]);
p[1][1].add(l3[2][1]);
p[1][2].add(l3[0][1]);
p[1][3].add(l3[2][2]);
p[2][0].add(l3[3][1]);
p[2][1].add(l3[0][2]);
p[2][2].add(l3[1][1]);
p[2][3].add(l3[3][0]);
p[3][0].add(l3[2][3]);
p[3][1].add(l3[2][0]);
p[3][2].add(l3[1][0]);
addWindowListener(new MyWindowAdapter());
}
public static void main(String args[])
{
Game g = new Game();
g.setSize(new Dimension(300, 200));
g.setTitle("A Puzzle Game");
g.setLayout(new FlowLayout(FlowLayout.CENTER));
g.setVisible(true);
}
}
class MyWindowAdapter extends WindowAdapter{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
class MyAction implements ActionListener{
Game g;
public MyAction(Game g)
{
this.g=g;
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
if(str.equals("Objective"))
{
g.pi.remove(g.x);
g.pi.add(g.pi2);
g.pi.x=g.pi2;
}
if(str.equals("Instruction"))
{
g.pi.remove(g.x);
g.pi.add(g.pi4);
g.pi.x=g.pi4;
}
if(str.equals("Exit"))
{
System.exit(0);
}
}
}
class MyKeyAdapter extends KeyAdapter{
Game g;
public MyKeyAdapter(Game g)
{
this.g=g;
}
public void keyTyped(KeyEvent ke)
{
}
}
This code is perfect when actionPerformed function in MyAction class is kept empty...
But I have to write something to obtain the desired result...I want to remove the Panels pi2,pi4 etc when buttons "Objective" or "Instructions" are pressed...
Please help ...