Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need help with Java.awt.Color

  1. #1
    Member
    Join Date
    Dec 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with Java.awt.Color

    I am having problem with the JRadio Buttons to change the font color in my code. What needs to be done? Also if I were to connect this GUI to another GUI and save font colors, how would I go about that?

    package SystemandDesign.RISK;
     
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.Color;
    import java.awt.event.ItemListener;
    import java.awt.event.ItemEvent;
    import javax.swing.JTextField;
    import javax.swing.JRadioButton;
    import javax.swing.JFrame;
    import javax.swing.ButtonGroup;
     
    public class CharacterCreation extends JFrame{
     
      private Font redFont;
      private Font blueFont;
      private Font greenFont;
      private Font yellowFont;
      private Font grayFont;
      private Font blackFont;
      private JRadioButton three;
      private JRadioButton four;
      private JRadioButton five;
      private JRadioButton six;
      private JRadioButton red;
      private JRadioButton blue;
      private JRadioButton green;
      private JRadioButton yellow;
      private JRadioButton gray;
      private JRadioButton black;
      private JTextField textField;
      private ButtonGroup army;
      private ButtonGroup numPlayers;
     
      public CharacterCreation(){
     
        super("Character Creation");
        setLayout(new FlowLayout());
     
        textField = new JTextField("Your Army Color", 25);
        add(textField);
     
        //To remain true to the Game of Risk, instead of having individual name input for the game. 
        //I am allowing which army color the player wants to be.
        //When the game is done being written out, I hope to have the font of individual player text, to be the color
        //of their army.
     
        //In the future when the model is done, the character screen will prevent players from having the same color army.
        redFont = new Font("Serif", Font.PLAIN, 14);
        blueFont = new Font("Serif", Font.PLAIN,14);
        greenFont = new Font("Serif", Font.PLAIN,14);
        yellowFont = new Font("Serif", Font.PLAIN,14);
        grayFont = new Font("Serif",  Font.PLAIN,14);
        blackFont = new Font("Serif",  Font.PLAIN,14);
        textField.setFont(redFont);
     
        red = new JRadioButton("Red Army", true);
        blue = new JRadioButton("Blue Army", false);
        green = new JRadioButton("Green Army", false);
        yellow = new JRadioButton("Yellow Army", false);
        gray = new JRadioButton("Gray Army", false);
        black = new JRadioButton("Black Army", false);
        add(red);
        add(blue);
        add(green);
        add(yellow);
        add(gray);
        add(black);
     
        army = new ButtonGroup();
        army.add(red);
        army.add(blue);
        army.add(green);
        army.add(yellow);
        army.add(gray);
        army.add(black);
     
        red.addItemListener(new RadioButtonHandler(redFont));
        blue.addItemListener(new RadioButtonHandler(blueFont));
        green.addItemListener(new RadioButtonHandler(greenFont));
        yellow.addItemListener(new RadioButtonHandler(yellowFont));
        gray.addItemListener(new RadioButtonHandler(grayFont));
        black.addItemListener(new RadioButtonHandler(blackFont));
        //End of the Army selection RadioButtons.
     
        //Now for the Player selection RadioButtons.
     
        three = new JRadioButton("3 players", true);
        four = new JRadioButton("4 players", false);
        five = new JRadioButton("5 players", false);
        six = new JRadioButton("6 players", false);
        add(three);
        add(four);
        add(five);
        add(six);
     
        numPlayers = new ButtonGroup();
        numPlayers.add(three);
        numPlayers.add(four);
        numPlayers.add(five);
        numPlayers.add(six);
     
     
     
      }
     
      private class RadioButtonHandler implements ItemListener{
     
        private Font font;
     
        public RadioButtonHandler(Font f){
          font = f;
        }
     
        public void itemStateChanged(ItemEvent event){
     
          textField.setFont(font);
     
          if(event.equals(red)){
            textField.setBackGround(Color.RED);
          }
          else if(event.equals(blue)){
            textField.setBackground(Color.BLUE);
          }
          else if(event.equals(green)){
            textField.setBackground(Color.GREEN);
          }
          else if(event.equals(yellow)){
            textField.setBackground(Color.YELLOW);
          }
          else if(event.equals(gray)){
            textField.setBackground(Color.GRAY);
          }
          else if(event.equals(black)){
            textField.setBackground(Color.BLACK);
          }
     
        }
     
      }
     
    }

  2. #2
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Need help with Java.awt.Color

    Check out setForeground()

Similar Threads

  1. Replies: 6
    Last Post: September 15th, 2013, 07:18 PM
  2. Why does the Color class have two variables for each color?
    By Melawe in forum Java Theory & Questions
    Replies: 5
    Last Post: May 10th, 2012, 04:21 PM
  3. text color changer based on words/ word classification by color
    By knoxy5467 in forum Java Theory & Questions
    Replies: 25
    Last Post: June 15th, 2011, 07:52 AM
  4. Change of color for selected text in AWT
    By venkyInd in forum AWT / Java Swing
    Replies: 2
    Last Post: April 9th, 2010, 03:51 AM
  5. AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
    By nasi in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 25th, 2010, 10:37 PM

Tags for this Thread