hey all,
Well im going absolutely insane! I have been sitting infront of this thing all day today just to get myself some decent practise with
java after not having programmed for a couple of weeks....so i thought i would create a simple naughts and crosses type of game!?! well i say simple!?!
I am trying to do just a two player thing at the moment, whereby first person who clicks...the square turns black...second person chooses a square, turns red, then back to 1st player and black....etc etc! but for some reason, this thing only does the red square, and then does not go onto the black one, it's so frustrating i have tried so many diff ways! If any genious out there that can help, by all means please please do!
Here is the code i have created:
public class theGame extends JApplet {
final int width = 375;
final int height = 375;
boolean valid = false, valid2 = false, valid3 = false, valid4 = false;
boolean valid5 = false, valid6 = false, valid7 = false, valid8 = false, valid9 = false;
JTextField textFieldOne;
JTextArea textAreaOne;
JButton b1, b2, b3, b4, b5, b6, b7, b8, b9;
public void init() {
String name = JOptionPane.showInputDialog(null,
"What's your name??",
"Hello, what's your name??",
JOptionPane.QUESTION_MESSAGE);
Container c = getContentPane();
c.setLayout(null);
c.setBackground(Color.white);
textFieldOne = new JTextField();
textFieldOne.setText("Hello " +name+ ", Welcome to Red & Black )");
textFieldOne.setEditable(false);
textAreaOne = new JTextArea(5,5);
textAreaOne.setText("Heres How To Play: Simply click on the squares below" +
"\nin order to set it to your colour. Make a horizontal," +
"\nvertical, or even a diagonal line of your colour to win!!");
textAreaOne.setEditable(false);
b1 = new JButton("?");
b1.addActionListener(new ButtonHandler(this, 'A'));
b2 = new JButton("?");
b2.addActionListener(new ButtonHandler(this, 'B'));
b3 = new JButton("?");
b3.addActionListener(new ButtonHandler(this, 'C'));
b4 = new JButton("?");
b4.addActionListener(new ButtonHandler(this, 'D'));
b5 = new JButton("?");
b5.addActionListener(new ButtonHandler(this, 'E'));
b6 = new JButton("?");
b6.addActionListener(new ButtonHandler(this, 'F'));
b7 = new JButton("?");
b7.addActionListener(new ButtonHandler(this, 'G'));
b8 = new JButton("?");
b8.addActionListener(new ButtonHandler(this, 'H'));
b9 = new JButton("?");
b9.addActionListener(new ButtonHandler(this, 'I'));
SquarePanel graphicsPanel = new SquarePanel(this);
//FOR THE TEXT FIELD POSITIONING
Insets insets = c.getInsets();
Dimension size = textFieldOne.getPreferredSize();
textFieldOne.setBounds(70 + insets.left, 25 + insets.top,
size.width, size.height);
Insets insets0 = c.getInsets();
Dimension size2 = textAreaOne.getPreferredSize();
textAreaOne.setBounds(35 + insets0.left, 50 + insets0.top,
size2.width, size2.height);
//FOR THE BUTTONS POSITIONING PLACED ON THE BOARD
Insets insets2 = c.getInsets();
b1.setBounds(87 + insets2.left, 137 + insets2.top,
50, 50);
Insets insets3 = c.getInsets();
b2.setBounds(162 + insets3.left, 137 + insets3.top,
50, 50);
Insets insets4 = c.getInsets();
b3.setBounds(237 + insets4.left, 137 + insets4.top,
50, 50);
Insets insets5 = c.getInsets();
b4.setBounds(87 + insets5.left, 214 + insets5.top,
50, 50);
Insets insets6 = c.getInsets();
b5.setBounds(162 + insets6.left, 214 + insets6.top,
50, 50);
Insets insets7 = c.getInsets();
b6.setBounds(237 + insets7.left, 214 + insets7.top,
50, 50);
Insets insets8 = c.getInsets();
b7.setBounds(87 + insets8.left, 292 + insets8.top,
50, 50);
Insets insets9 = c.getInsets();
b8.setBounds(162 + insets9.left, 292 + insets9.top,
50, 50);
Insets insets10 = c.getInsets();
b9.setBounds(237 + insets10.left, 292 + insets10.top,
50, 50);
//FOR THE ACTUAL GRAPHICS PANEL
Insets insets11 = c.getInsets();
graphicsPanel.setBounds(insets11.left, 50 + insets11.top,
375, 375);
c.add(graphicsPanel);
c.add(b1);
c.add(b2);
c.add(b3);
c.add(b4);
c.add(b5);
c.add(b6);
c.add(b7);
c.add(b8);
c.add(b9);
c.add(textFieldOne);
c.add(textAreaOne);
repaint();
}
class SquarePanel extends JPanel {
int counter = 1;
theGame theApplet;
SquarePanel(theGame app)
{
theApplet = app;
}
public void paint (Graphics g) {
theApplet.setSize(375, 375);
super.paintComponents(g);
g.drawLine(75, 150, width - 75, 150);
g.drawLine(75, height - 150, width - 75, height - 150);
g.drawLine(150, 75, 150, height - 75);
g.drawLine(width - 150, 75, width - 150, height - 75);
for (int x = counter; x <= 9; x++){
if ((counter == 1) || (counter == 3) || (counter == 5) || (counter == 7) || (counter == 9)){
g.setColor(Color.red);
if (theApplet.valid) {
g.fillRect(87,87,50,50);
} else if (theApplet.valid2) {
g.fillRect(162,87,50,50);
} else if (theApplet.valid3) {
g.fillRect(237,87,50,50);
} else if (theApplet.valid4) {
g.fillRect(87,164,50,50);
} else if (theApplet.valid5) {
g.fillRect(162,164,50,50);
} else if (theApplet.valid6) {
g.fillRect(237,164,50,50);
} else if (theApplet.valid7) {
g.fillRect(87,241,50,50);
} else if (theApplet.valid8) {
g.fillRect(162,241,50,50);
} else if (theApplet.valid9) {
g.fillRect(237,241,50,50);
}
//repaint();
//counter++;
}
else if ((counter == 2) || (counter == 4) || (counter == 6) || (counter == 8)){
g.setColor(Color.black);
if (theApplet.valid) {
g.fillRect(87,87,50,50);
} else if (theApplet.valid2) {
g.fillRect(162,87,50,50);
} else if (theApplet.valid3) {
g.fillRect(237,87,50,50);
} else if (theApplet.valid4) {
g.fillRect(87,164,50,50);
} else if (theApplet.valid5) {
g.fillRect(162,164,50,50);
} else if (theApplet.valid6) {
g.fillRect(237,164,50,50);
} else if (theApplet.valid7) {
g.fillRect(87,241,50,50);
} else if (theApplet.valid8) {
g.fillRect(162,241,50,50);
} else if (theApplet.valid9) {
g.fillRect(237,241,50,50);
}
//repaint();
//counter++;
}
repaint();
}
}
}
class ButtonHandler implements ActionListener {
theGame theApplet;
char theButton;
public ButtonHandler(theGame theApp, char but) {
theApplet = theApp;
theButton = but;
}
public void actionPerformed(ActionEvent e) {
if (theButton == 'A') {
valid = true;
}
if (theButton == 'B') {
valid2 = true;
}
if (theButton == 'C') {
valid3 = true;
}
if (theButton == 'D') {
valid4 = true;
}
if (theButton == 'E') {
valid5 = true;
}
if (theButton == 'F') {
valid6 = true;
}
if (theButton == 'G') {
valid7 = true;
}
if (theButton == 'H') {
valid8 = true;
}
if (theButton == 'I') {
valid9 = true;
}
theApplet.repaint();
}
}
}
I have simply been mucking around with the paint method for about 2 hours now, and cannot fix this problem! Any help would be brilliant, thank you!