Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. #1
    HELPME2 is offline Member
    Join Date
    Jan 2013
    Posts
    13
    Rep Power
    0

    Default 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!

    Java 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(); 
        } 
    }
    Last edited by HELPME2; 01-16-2013 at 11:23 PM.

  2. #2
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Change font color

    Please wrap your code in code tags so we can easily find something in it...

  3. #3
    HELPME2 is offline Member
    Join Date
    Jan 2013
    Posts
    13
    Rep Power
    0

    Default Re: Change font color

    Like that. my bad Im new

  4. #4
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Change font color

    you are near, it is actually CODE not QUOTE tags

  5. #5
    HELPME2 is offline Member
    Join Date
    Jan 2013
    Posts
    13
    Rep Power
    0

    Default Re: Change font color

    I think I got it now

  6. #6
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Change font color

    There is a method setForeground(Color color), did you try it?

  7. #7
    HELPME2 is offline Member
    Join Date
    Jan 2013
    Posts
    13
    Rep Power
    0

    Default Re: Change font color

    Yes, it says ...

    The method setForeground(java.awt.Color) is undefined for the type NewTicTacToe

  8. #8
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default 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.

  9. #9
    HELPME2 is offline Member
    Join Date
    Jan 2013
    Posts
    13
    Rep Power
    0

    Default Re: Change font color

    Where should I try that code to change the color of the X and O

  10. #10
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default 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.

  11. #11
    HELPME2 is offline Member
    Join Date
    Jan 2013
    Posts
    13
    Rep Power
    0

    Default Re: Change font color

    I tried this. It compiles but it doesnt do anything

    Java 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));
                 }

  12. #12
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Change font color

    It prints letter correctly?

  13. #13
    HELPME2 is offline Member
    Join Date
    Jan 2013
    Posts
    13
    Rep Power
    0

    Default Re: Change font color

    Yea its prints everyone the same.

    button[i].setForeground(Color.YELLOW);

    makes no difference

  14. #14
    HELPME2 is offline Member
    Join Date
    Jan 2013
    Posts
    13
    Rep Power
    0

    Default Re: Change font color

    *everything

  15. #15
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default 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
    Java Code:
    button[i].setText("<html><font color = red>X</font></html>");
    This way you will set both letter and color in one line..
    Last edited by milovan; 01-17-2013 at 12:48 AM.

  16. #16
    HELPME2 is offline Member
    Join Date
    Jan 2013
    Posts
    13
    Rep Power
    0

    Default 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));
    }

  17. #17
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Change font color

    read my edit in previous post

  18. #18
    HELPME2 is offline Member
    Join Date
    Jan 2013
    Posts
    13
    Rep Power
    0

    Default Re: Change font color

    Change font color-fontcolor.png That did something I think

  19. #19
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default 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:
    Java 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();
    }

  20. #20
    HELPME2 is offline Member
    Join Date
    Jan 2013
    Posts
    13
    Rep Power
    0

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 5
    Last Post: 05-09-2011, 06:28 AM
  2. Replies: 0
    Last Post: 08-18-2010, 10:31 AM
  3. font color
    By newbie123 in forum New To Java
    Replies: 2
    Last Post: 03-01-2010, 03:58 PM
  4. Replies: 5
    Last Post: 04-25-2009, 08:00 PM
  5. Replies: 0
    Last Post: 06-25-2008, 10:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •