Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Louisa..ivy

macrumors newbie
Original poster
Jun 27, 2009
6
0
Hi i am a student and i have this code(its a little long) and i want to make it so the buttons are a different colour and then a different colour again once they have been pressed. also I want to make the font a different colour then just black and so the X's are a different colour to the O's any suggestions will be very helpful:



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import javax.swing.JButton;


public class TicTacToe extends JPanel implements ActionListener
{



//Class constants

private static final int WINDOW_WIDTH = 400;
private static final int WINDOW_HEIGHT = 400;
private static final int TEXT_WIDTH = 10;

//Design
private static final GridLayout Layout_Style = new GridLayout(3,3);


//Instance variables
private JFrame window = new JFrame("TicTacToe");
private JButton Square1 = new JButton("");
private JButton Square2 = new JButton("");
private JButton Square3 = new JButton("");
private JButton Square4 = new JButton("");
private JButton Square5 = new JButton("");
private JButton Square6 = new JButton("");
private JButton Square7 = new JButton("");
private JButton Square8 = new JButton("");
private JButton Square9 = new JButton("");


String mark = "X";
boolean win = false;
Color blue = new Color(0,0,0);
Color red = new Color(0,0,0);
//Constructor

public TicTacToe()
{

window.setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// exits the application one the game has been won
window.setLocationRelativeTo(null);//Centers the window

//Add to window the internet helped me to see what this should look like
window.getContentPane().setLayout(Layout_Style);
window.getContentPane().add(Square1);
window.getContentPane().add(Square2);
window.getContentPane().add(Square3);
window.getContentPane().add(Square4);
window.getContentPane().add(Square5);
window.getContentPane().add(Square6);
window.getContentPane().add(Square7);
window.getContentPane().add(Square8);
window.getContentPane().add(Square9);
//Add listener
Square1.addActionListener(this);
Square2.addActionListener(this);
Square3.addActionListener(this);
Square4.addActionListener(this);
Square5.addActionListener(this);
Square6.addActionListener(this);
Square7.addActionListener(this);
Square8.addActionListener(this);
Square9.addActionListener(this);

window.setVisible(true);
}
//ActionPerformed
public void actionPerformed(ActionEvent e)
{
// counts one click of the mouse and then searches for which button has been pressed
int count=1;

do
{

if (e.getSource()==Square1)
//If user marks Square 1 then it will set a mark or it will continue on searching until a Square is marked
{
Square1.setText(mark);
Square1.setEnabled(false);
}
else if (e.getSource()==Square2)
{
Square2.setText(mark);
Square2.setEnabled(false);
}
else if (e.getSource()==Square3)
{
Square3.setText(mark);
Square3.setEnabled(false);
}
else if (e.getSource()==Square4)
{
Square4.setText(mark);
Square4.setEnabled(false);
}
else if (e.getSource()==Square5)
{
Square5.setText(mark);
Square5.setEnabled(false);
}
else if (e.getSource()==Square6)
{
Square6.setText(mark);
Square6.setEnabled(false);
}
else if (e.getSource()==Square7)
{
Square7.setText(mark);
Square7.setEnabled(false);
}
else if (e.getSource()==Square8)
{
Square8.setText(mark);
Square8.setEnabled(false);
}
else if (e.getSource()==Square9)
{
Square9.setText(mark);
Square9.setEnabled(false);
}

//Checks to see if 3 corresponding X's or O's Match-up
if (Square1.getText().equals(Square2.getText()) && Square2.getText().equals
(Square3.getText()) && Square1.getText().equals("")==false)
{
Square1.setBackground(blue);
Square2.setBackground(blue);
Square3.setBackground(blue);
win=true;
}
else if (Square4.getText().equals(Square5.getText()) && Square5.getText().equals
(Square6.getText())&& Square4.getText().equals("")==false)
{
Square4.setBackground(blue);
Square5.setBackground(blue);
Square6.setBackground(blue);
win=true;
}
else if (Square7.getText().equals(Square8.getText()) && Square8.getText().equals
(Square9.getText())&& Square7.getText().equals("")==false)
{
Square7.setBackground(blue);
Square8.setBackground(blue);
Square9.setBackground(blue);
win=true;
}
else if (Square1.getText().equals(Square4.getText()) && Square4.getText().equals
(Square7.getText())&& Square1.getText().equals("")==false)
{
Square1.setBackground(blue);
Square4.setBackground(blue);
Square7.setBackground(blue);
win=true;
}
else if (Square2.getText().equals(Square5.getText()) && Square5.getText().equals
(Square8.getText())&& Square2.getText().equals("")==false)
{
Square2.setBackground(blue);
Square5.setBackground(blue);
Square8.setBackground(blue);
win=true;
}
else if (Square3.getText().equals(Square6.getText()) && Square6.getText().equals
(Square9.getText())&& Square3.getText().equals("")==false)
{
Square3.setBackground(blue);
Square6.setBackground(blue);
Square9.setBackground(blue);
win=true;
}
else if (Square1.getText().equals(Square5.getText()) && Square5.getText().equals
(Square9.getText())&& Square1.getText().equals("")==false)
{
Square1.setBackground(blue);
Square5.setBackground(blue);
Square9.setBackground(blue);
win=true;
}
else if (Square3.getText().equals(Square5.getText()) && Square5.getText().equals
(Square7.getText())&& Square3.getText().equals("")==false)
{
Square3.setBackground(blue);
Square5.setBackground(blue);
Square7.setBackground(blue);
win=true;
}

if (count==9 && win== false)
{
JOptionPane.showMessageDialog(null, mark + "Too bad its a tie");//at the moment this isn't working but i cannot see why
System.exit(1);
win=false;
}

System.out.println("Button Pressed");
if (count!=9 && win==true)
{
JOptionPane.showMessageDialog(null, mark + " You Win!!!");
System.exit(1);
}
if (mark.equals("X"))
{
mark="O";
}
else
{
mark="X";
}

}

while(win=false);

}
public static void main(String[] args)

{
TicTacToe gui = new TicTacToe();



}
}

Thanks
 
update

Ive been told that this code:
button.setBackground(Color.YELLOW)
will change the colour of the button which i added to this part of the code
Square1.setBackground(Color.YELLOW);
Square2.setBackground(Color.YELLOW);
Square3.setBackground(Color.YELLOW);
win=true;
}

But nothing seems to have changed- is there anything else that i might need to add to make this work?
 
You should focus first on making the code work, and only then worry about the cosmetic aspects like font and background color.

First, you can't have a do/while loop in actionPerformed. If you're taking a programming class, you should ask your instructor about this. If you're not taking a programming class, you should refer to a Swing or AWT tutorial that covers the basics of event-loops and action listeners.

Second, you have a lot of unnecessarily duplicated code. Think a little about the source that's in the event, and instead of comparing it to 9 different JButtons, use the value as a JButton to set its mark and enabled state.

Third, button background colors are a function of the Look and Feel. The default Mac OS X LaF ignores background colors on push-buttons. If you want something colored, you'll have to do something else. You could add a colored Border to the buttons, or you could use a different button variant that supports background colors. In a way, it won't matter much, because as soon as a win is detected your program exits. Any coloring will only be visible for a fraction of a second. Again, you should solve that problem before working on the cosmetics.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.