Hi,
I don't even know if this is the right place to post this but i will, because I need help. I'm trying to finish this basic game. but I am having troubles. If anyone wants to help me out it would be great. I will admit this might take a long time, i am new to java and i suck at it(yes i will also admit that). by anyones standards this program probably really sucks, but its the best that i can do.
my objectives:
- when a button is pressed down, switch it to the opposite button ( on goes to off, off goes to on) but at the same time, switch the buttons that are left, right, up and down from it.
Before:
http://i15.photobucket.com/albums/a385/t_dimock/Picture3-2.png
After:
http://i15.photobucket.com/albums/a385/t_dimock/Picture2-9.png
graphics
http://i15.photobucket.com/albums/a385/t_dimock/On.png
http://i15.photobucket.com/albums/a385/t_dimock/Off.png
Anyone have any ideas?
- import java.awt.*;
-
import javax.swing.*;
-
import java.awt.event.*;
-
import java.util.*;
-
-
-
/**
-
* Class l0gixGame - write a description of the class here
-
*
-
* @author (your name)
-
* @version (a version number)
-
*/
-
public class logixGame2 extends JApplet {
-
JButton btn;
-
private static final ImageIcon image[]={
-
new ImageIcon("On.png"),
-
new ImageIcon("Off.png")};
-
ImageIcon on = new ImageIcon(getImage(getCodeBase(),
-
"On.png"));
-
-
ImageIcon off = new ImageIcon(getImage(getCodeBase(),
-
"Off.png"));
-
-
public void init(){
-
-
/** creates the control panel, sets the size,locaiton and the background
-
* colour
-
*/
-
JPanel controlPanel = new JPanel();
-
controlPanel.setSize(new Dimension(400,275));
-
controlPanel.setLocation(10,100);
-
controlPanel.setBackground(Color.gray);
-
-
/** creates the background panel, sets the size,locaiton and the
-
* background color
-
*/
-
JPanel backGround = new JPanel();
-
backGround.setSize(new Dimension(400,400));
-
backGround.setLocation(0,0);
-
backGround.setBackground(Color.gray);
-
-
/** creates the play panel, sets the size,locaiton and the
-
* background color
-
*/
-
JPanel playPanel = new JPanel();
-
playPanel.setSize(new Dimension(400,275));
-
playPanel.setLocation(500,200);
-
playPanel.setBackground(Color.orange);
-
-
/** creates the layout for the play panel, and the control panel
-
* creates the content pane, and the three panels to the content pane
-
* and sets the dimensions for the content pane
-
*/
-
controlPanel.setLayout(new GridLayout(8,12));
-
playPanel.setLayout(new GridLayout(8,12));
-
Container cp = getContentPane();
-
cp.setSize(new Dimension(5000,5000));
-
cp.add(controlPanel);
-
cp.add(playPanel);
-
cp.add(backGround);
-
-
/**initialize the onbutton and the offbutton with the related graphics*/
-
ImageIcon on = new ImageIcon(getImage(getCodeBase(),
-
"On.png"));
-
-
ImageIcon off = new ImageIcon(getImage(getCodeBase(),
-
"Off.png"));
-
-
ArrayList<String> control = new ArrayList<String>();
-
ArrayList<String> play = new ArrayList<String>();
-
-
/** Randimization of control panel and play panel*/
-
/**1=off 2=on*/
-
int row,column,i,x,y;
-
for( column = 0; column <= 8; column = column+1)
-
{
-
for (row = 0; row <= 14; row = row+1)
-
{
-
i = 1;
-
y = row * 50;
-
x = column * 50;
-
if ( (column % 2) == (row % 2) )
-
{
-
-
Random r = new Random();
-
int n = r.nextInt();
-
if(n % 2 == 0){
-
JLabel btn2 = new JLabel(off);
-
controlPanel.add(btn2);
-
control.add("1");
-
JButton btn = new JButton(on);
-
btn.setBackground(Color.orange);
-
playPanel.add(btn);
-
btn.addActionListener(new bob());
-
play.add("2");
-
-
-
-
}
-
else{
-
JLabel btn2 = new JLabel(on);
-
controlPanel.add(btn2);
-
control.add("2");
-
JButton btn = new JButton(off);
-
btn.setBackground(Color.orange);
-
playPanel.add(btn);
-
btn.addActionListener(new bob());
-
play.add("1");
-
}
-
-
}
-
else
-
{
-
Random r = new Random();
-
int n = r.nextInt();
-
if(n % 3 == 0){
-
JLabel btn2 = new JLabel(on);
-
controlPanel.add(btn2);
-
control.add("2");
-
JButton btn = new JButton(on);
-
btn.setBackground(Color.orange);
-
playPanel.add(btn);
-
btn.addActionListener(new bob());
-
play.add("2");
-
-
}
-
-
}
-
-
-
}
-
}
-
-
String controlString =""+ control;
-
String playString =""+ play;
-
-
if(controlString.compareTo(playString)==0){
-
JLabel winner = new JLabel("Winner!");
-
backGround.add(winner);
-
}
-
}
-
public class bob implements ActionListener{
-
public void actionPerformed (ActionEvent event){
-
if(btn.getIcon().equals(on))
-
{
-
btn.setIcon(off);
-
}
-
else
-
{
-
btn.setIcon(on);
-
-
}
-
}
-
}
-
-
}