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 3 of 3

Thread: GetColor Not working

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default GetColor Not working

    I am working on a fairly simple program where I have a colour change three times using the red green blue spectrum. The problem is that I keep getting a result of zero no matter what I do. Does anyone have any suggestions?

    import java.awt.*;
     
    class RectangleMain
    {
      public static void main (String[] args)
      {
        ColouredRectangle blocky = new ColouredRectangle(50, 100, 20, 40, Color.red);
        System.out.println(blocky.getColour());
        blocky.mixColour(Color.blue);
        System.out.println(blocky.getColour());
        blocky.mixColour(Color.black);
        System.out.println(blocky.getColour());
      }
    }

    import java.awt.*;
     
    public class ColouredRectangle extends Rectangle
    {
      ColouredRectangle (int x, int y, int width, int height, Color colour)
      {
        super (x, y, width, height);
      }
     
      private Color colour;
     
      private int rVal;
      private int gVal;
      private int bVal;
     
      public void setColour(int r, int g, int b)
      {
        colour = new Color(r,g,b);
        rVal = r;
        gVal = g;
        bVal = b;
      }
     
      public String getColour()
      {
        return ("blocky's colour is java.awt.Color[Red = " + rVal + ", Green = " + gVal + ", Blue = " + bVal + "]");
      }
     
      public void mixColour(Color addColour)
      {
        int newRed = (addColour.getRed() + rVal) % 256;
        int newGreen = (addColour.getGreen() + gVal) % 256;
        int newBlue = (addColour.getBlue() + bVal) % 256;
        colour = new Color(newRed, newGreen, newBlue); 
      }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: GetColor Not working

    What do you mean you keep getting zero? Post a sample run or whatever it is you're seeing that shows the zero result.

  3. #3
    Junior Member
    Join Date
    Oct 2014
    Posts
    12
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: GetColor Not working

    Just glancing at the code. You never call setColor, so the values of rVal, bVal and gVal aren't ever changed by anything. Rethink your design...

Similar Threads

  1. Replies: 7
    Last Post: April 25th, 2013, 01:12 PM
  2. Writing a getColor() method
    By KristopherP in forum Java Theory & Questions
    Replies: 14
    Last Post: April 23rd, 2013, 06:33 PM
  3. graphics fillRect and getColor
    By prashantgolu in forum AWT / Java Swing
    Replies: 2
    Last Post: July 25th, 2011, 07:30 AM
  4. Replies: 4
    Last Post: January 26th, 2009, 11:03 PM