Results 1 to 5 of 5
  1. #1
    JavaClass is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Angry Need Help Trying to set an array for multiple RGB values

    I'm trying to define a color by inputting multiple Red, Green, and Blue values from three arrays - although I can't seem to do so. I understand that this type of code may not be possible in the manner that i've written it... how can i accomplish this?

    Here's the code:

    Java Code:
    import java.awt.Color;
    
    public class colorSetter {
    public static Color acolor;
      
        
        public static void main(String[] args){
           
            
          int[] redvaluesColor = {41,42,43,44,45,46,47,48,49,50};
          int[] greenvaluesColor = {71,72,73,74,75,76,77,78,79,80};
          int[] bluevaluesColor = {11,12,13,14,15,16,17,18,19,20};
          acolor = new Color(redvaluesColor, greenvaluesColor, bluevaluesColor);
    
    
    }
    }
    I'm new to Java so any help would be much appreciate - thanks!
    Last edited by JavaClass; 02-28-2013 at 02:32 AM.

  2. #2
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default Re: Need Help Trying to set an array for multiple RGB values

    every RGB code has 3 colors right?

    Java Code:
    int aColor[][] = { {1, 2, 3}, {4,5,6}, {7,8,9}, {10,11,12} };
    //You can call this by:
    for (int i = 0; i < 2; i++){
       for (int x = 0; x < 3; x++){
            System.out.print(aColor[i][x] + "\t");
       }
        System.out.println();
    }
    -LearningTOprogram

  3. #3
    JavaClass is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default Re: Need Help Trying to set an array for multiple RGB values

    Quote Originally Posted by LearningTOprogram View Post
    every RGB code has 3 colors right?

    Java Code:
    int aColor[][] = { {1, 2, 3}, {4,5,6}, {7,8,9}, {10,11,12} };
    //You can call this by:
    for (int i = 0; i < 2; i++){
       for (int x = 0; x < 3; x++){
            System.out.print(aColor[i][x] + "\t");
       }
        System.out.println();
    }
    -LearningTOprogram
    Yes, and I'm trying to get it so that it has all of those combinations of integers for each R,G, and B. That code would require me to list every single combination out wouldn't it? Is there any way I could get it so it would check all of the different combinations as aforementioned?

    Thanks!

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: Need Help Trying to set an array for multiple RGB values

    I am not certain what you want to do but an RGB color can be created by calling java.awt.Color as follows:

    Java Code:
    import java.awt.Color;
    ...
    Color aColor = new Color(20,20,20);
    ...
    //You could then store the color in a list of colors:
    ...
    List<Color> myColors = new ArrayList<Color>();
    myColors.add(aColor);
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default Re: Need Help Trying to set an array for multiple RGB values

    Hi JavaClass, welcome to the forums.

    The code written in #1 wouldn't work anyway as you are atttempting to construct a new color using arrays as the parameters.
    See the link below for information on basic arrays.
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Just to demonstrate a way to simplify your code:

    Java Code:
    import java.awt.Color; 
    
    public class colorSetter 
    {    
        public static void main(String[] args)
        {
             Color[] colours = new color[10];
    
             for(int i = 0; i < colours.length(); i++)
                  colours[i] = new color(i+41, i+71, i+11);
        }
    }
    The above code will generate a fixed array of ten colours, each containing the colours from your initial code.
    I should point out this is not the only way of doing this. For example, as Jim has already shown, Lists can be used as an alternative to fixed size arrays.

    Regards.
    Last edited by Ronin; 03-01-2013 at 01:36 AM. Reason: Code indentation

Similar Threads

  1. Return multiple values from array lookup?
    By tbhawk in forum New To Java
    Replies: 2
    Last Post: 07-31-2011, 03:56 PM
  2. Replies: 14
    Last Post: 06-29-2011, 03:49 PM
  3. Multiple values of different types in an object
    By scheffetz in forum New To Java
    Replies: 2
    Last Post: 03-23-2011, 08:34 PM
  4. Replies: 14
    Last Post: 01-22-2011, 02:36 PM
  5. [SOLVED] Multiple return values
    By Manfizy in forum New To Java
    Replies: 17
    Last Post: 05-25-2009, 12:07 PM

Posting Permissions

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