-
Change font color
I want to make the Xs and Os in different colors in my Tic Tac Toe game. This is my code. Help me please!
Code:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.Random;
import javax.swing.*;
import java.awt.Color.*;
import java.awt.Font;
import java.awt.Graphics2D;
public class NewTicTacToe implements ActionListener {
private JFrame window = new JFrame("Tic-Tac-Toe");
private JButton button[] = new JButton[10];
private String letter = "";
private boolean winner = false;
public int turn = 1;
public NewTicTacToe(){
/*Create Window*/
window.setPreferredSize(new Dimension(400,400));
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));
/*Add Buttons To The Window*/
for(int i = 1; i<=9; i++){
button[i] = new JButton();
window.add(button[i]);
button[i].addActionListener(this);
button[i].setBackground(new Color(0, 40, 75));
}
/*Make The Window Visible*/
window.setVisible(true);
window.pack();
}
public void actionPerformed(ActionEvent a) {
if (turn == 1 || turn==3 || turn==5 || turn==7 || turn== 9)
{
for(int i = 1; i<= 9; i++){
if(a.getSource() == button[i]){
button[i].setText("X");
button[i].setEnabled(false);
button[i].setFont(new Font("Sans Serif", Font.PLAIN, 75));
}
}
}
else
{
for(int i = 1; i<= 9; i++){
if(a.getSource() == button[i]){
button[i].setText("O");
button[i].setEnabled(false);
button[i].setFont(new Font("Sans Serif", Font.PLAIN, 75));
}
} }
turn++;
if(turn % 2 == 0)
letter = "X";
else
letter = "O";
//Horizontal win 1
if(button[1].getText()==button[2].getText() && button[2].getText() == button[3].getText() && button[3].getText()!="" )
{
winner=true;
button[1].setBackground(new Color(0, 50, 75));
button[2].setBackground(new Color(0, 50, 75));
button[3].setBackground(new Color(0, 50, 75));
JOptionPane.showMessageDialog(null, letter+ " wins!");
System.exit(0);
}
//Horizontal win 2
else if(button[4].getText()==button[5].getText() && button[5].getText() == button[6].getText() && button[4].getText()!="" )
{
winner=true;
button[4].setBackground(new Color(0, 50, 75));
button[5].setBackground(new Color(0, 50, 75));
button[6].setBackground(new Color(0, 50, 75));
JOptionPane.showMessageDialog(null, letter+ " wins!");
System.exit(0);
}
//Horizontal win 3
else if(button[7].getText()==button[8].getText() && button[8].getText() == button[9].getText() && button[7].getText()!="" )
{
winner=true;
button[7].setBackground(new Color(0, 50, 75));
button[8].setBackground(new Color(0, 50, 75));
button[9].setBackground(new Color(0, 50, 75));
JOptionPane.showMessageDialog(null, letter+ " wins!");
System.exit(0);
}
//Vertical win 1
else if(button[1].getText()==button[4].getText() && button[4].getText() == button[7].getText() && button[1].getText()!="" )
{
winner=true;
button[1].setBackground(new Color(0, 50, 75));
button[4].setBackground(new Color(0, 50, 75));
button[7].setBackground(new Color(0, 50, 75));
JOptionPane.showMessageDialog(null, letter+ " wins!");
System.exit(0);
}
//Vertical win 2
else if(button[2].getText()==button[5].getText() && button[5].getText() == button[8].getText() && button[2].getText()!="" )
{
winner=true;
button[2].setBackground(new Color(0, 50, 75));
button[5].setBackground(new Color(0, 50, 75));
button[8].setBackground(new Color(0, 50, 75));
JOptionPane.showMessageDialog(null, letter+ " wins!");
System.exit(0);
}
//Vertical win 3
else if(button[3].getText()==button[6].getText() && button[6].getText() == button[9].getText() && button[3].getText()!="" )
{
winner=true;
button[3].setBackground(new Color(0, 50, 75));
button[6].setBackground(new Color(0, 50, 75));
button[9].setBackground(new Color(0, 50, 75));
JOptionPane.showMessageDialog(null, letter+ " wins!");
System.exit(0);
}
//Diagonal win 1
else if(button[1].getText()==button[5].getText() && button[5].getText() == button[9].getText() && button[1].getText()!="" )
{
winner=true;
button[1].setBackground(new Color(0, 50, 75));
button[5].setBackground(new Color(0, 50, 75));
button[9].setBackground(new Color(0, 50, 75));
JOptionPane.showMessageDialog(null, letter+ " wins!");
System.exit(0);
}
//Diagonal win 2
else if(button[3].getText()==button[5].getText() && button[5].getText() == button[7].getText() && button[3].getText()!="" )
{
winner=true;
button[3].setBackground(new Color(0, 50, 75));
button[5].setBackground(new Color(0, 50, 75));
button[7].setBackground(new Color(0, 50, 75));
JOptionPane.showMessageDialog(null, letter+ " wins!");
System.exit(0);
}
else if(turn==10 && winner==false)
JOptionPane.showMessageDialog(null, "Match is a draw");
}
public static void main(String[] args){
new NewTicTacToe();
}
}
-
Re: Change font color
Please wrap your code in code tags so we can easily find something in it...
-
Re: Change font color
-
Re: Change font color
you are near, it is actually CODE not QUOTE tags :(happy):
-
Re: Change font color
-
Re: Change font color
There is a method setForeground(Color color), did you try it?
-
Re: Change font color
Yes, it says ...
The method setForeground(java.awt.Color) is undefined for the type NewTicTacToe
-
Re: Change font color
Where did you try it? Hope you will finish this little program and you will be happy with it, but I also hope that you will discover there are much better ways to build tic tac toe than this approach. Never the less, your current design has one advantage and it is that you choose in one place whether to set X or O on the button. In your actionPerformed method. Now you only have to decide what color to use for button's text and to change button's foreground to that color along with text.
-
Re: Change font color
Where should I try that code to change the color of the X and O
-
Re: Change font color
I pointed you to your actionPerformed() method. When decide what letter to set as a button's caption, you just have to set button's foreground you choose for that letter.
-
Re: Change font color
I tried this. It compiles but it doesnt do anything
Code:
if (turn == 1 || turn==3 || turn==5 || turn==7 || turn== 9)
{
for(int i = 1; i<= 9; i++){
if(a.getSource() == button[i]){
button[i].setText("X");
button[i].setForeground(Color.YELLOW);
button[i].setEnabled(false);
button[i].setFont(new Font("Sans Serif", Font.PLAIN, 75));
}
-
Re: Change font color
It prints letter correctly?
-
Re: Change font color
Yea its prints everyone the same.
button[i].setForeground(Color.YELLOW);
makes no difference
-
Re: Change font color
-
Re: Change font color
Ok try to put setForeground after setEnabled (because latest will change buttons text color to L&F default disabled color)
If L&F lets you do it. If not try HTML code like
Code:
button[i].setText("<html><font color = red>X</font></html>");
This way you will set both letter and color in one line..
-
Re: Change font color
Tried this. Still doesnt work
if (turn == 1 || turn==3 || turn==5 || turn==7 || turn== 9)
{
for(int i = 1; i<= 9; i++){
if(a.getSource() == button[i]){
button[i].setText("X");
button[i].setEnabled(false);
button[i].setForeground(Color.YELLOW);
button[i].setFont(new Font("Sans Serif", Font.PLAIN, 75));
}
-
Re: Change font color
read my edit in previous post
-
1 Attachment(s)
Re: Change font color
Attachment 4371 That did something I think
-
Re: Change font color
No, this is not the solution :-)
I am not sure I can see clearly what makes it so hard to make this work.
Just one more try:
Code:
if (turn == 1 || turn==3 || turn==5 || turn==7 || turn== 9)
{
for(int i = 1; i<= 9; i++){
if(a.getSource() == button[i]){
//button[i].setText("X");
button[i].setEnabled(false);
//button[i].setForeground(Color.YELLOW);
button[i].setFont(new Font("Sans Serif", Font.PLAIN, 75));
button[i].setText("<html><font color = red>X</font></html>");
break();
}
-
Re: Change font color