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: Trying to output color....HELP PLEASE!

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trying to output color....HELP PLEASE!

    Problem: I am trying to output different attributes of balls - golf, soccer, baseball.
    I am trying the output to look like the following:

    color = red and hardness = median and size = big

    but the output is as follows no matter what I do:

    color = java.awt.Color[r=0,g=0,b=0] hardness = median and size = big

    Can anyone point me in the right direction?


    Code:

    import java.awt.Color;
    import java.lang.*;

    public class Ball
    {
    private Color color;
    private String hardness;
    private String size;

    Ball(Color thecolor, String hardness, String size)
    {
    this.color = thecolor;
    this.hardness = hardness;
    this.size = size;
    }

    void displayInfor()
    {
    System.out.println(" color = " + color.toString()+ " and " + " hardness = " + hardness + " and size = " + size);
    System.out.println(color);
    }



    //method used to set the new color*/
    public void setColor(Color newcolor)
    {
    color = newcolor;
    }

    //method used to set the new hardness*/
    public void setHardness(String newHardness)
    {
    hardness = newHardness;
    }

    //method used to set the new size*/
    public void setSize(String newSize)
    {
    size = newSize;
    }

    }

    class driver
    {
    public static void main(String args[])
    {

    Ball golf, baseball, soccer;
    golf = new Ball(Color.WHITE, "hard", "small");
    baseball = new Ball(Color.RED, "soft", "median");
    soccer = new Ball(Color.BLACK, "median", "big");

    //print out the default value*/
    System.out.println("The golf ball ");
    golf.displayInfor();

    System.out.println("The baseball ");
    baseball.displayInfor();

    System.out.println("The soccer ball ");
    soccer.displayInfor();

    //now change the attribute values and print again. Since there are no requirements on how to change the value, I'll just use an example to show how it work. And of course you can make this part fancier by using a for/while loop to change the attribute values randomly...*/
    golf.setColor(Color.RED);
    golf.setHardness("soft");
    golf.setSize("big");
    System.out.println("The golf ball is now ");
    golf.displayInfor();

    baseball .setColor(Color.GREEN);
    baseball .setHardness("median");
    baseball .setSize("median");
    System.out.println("The baseball is now ");
    baseball.displayInfor();

    soccer .setColor(Color.YELLOW);
    soccer .setHardness("hard");
    soccer .setSize("small");
    System.out.println("The soccer ball is now ");
    soccer.displayInfor();

    }
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Trying to output color....HELP PLEASE!

    The color class is used when you are doing custom painting in a GUI. If all you want to do is display the text red or black or white to the screen then just use a String.

Similar Threads

  1. select a color from a png file
    By strider in forum Java Theory & Questions
    Replies: 2
    Last Post: December 2nd, 2010, 06:20 AM
  2. JTable Row Color
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: October 8th, 2010, 04:59 PM
  3. Color Problem
    By aussiemcgr in forum Java Theory & Questions
    Replies: 5
    Last Post: July 12th, 2010, 04:53 PM
  4. Change font color and size
    By javanovice in forum AWT / Java Swing
    Replies: 2
    Last Post: April 20th, 2010, 10:57 AM
  5. How prograssbarColor with differentThe color indicates
    By NARs in forum AWT / Java Swing
    Replies: 0
    Last Post: October 18th, 2009, 11:04 PM