Results 1 to 5 of 5
Thread: Color objects
- 03-31-2008, 10:50 PM #1
Member
- Join Date
- Mar 2008
- Posts
- 16
- Rep Power
- 0
Color objects
hey all,
am a bit stuck here. basically would like to return the color object so am I right in thinking this is just the rgb values? If not then what would the return type be in the method? i.e. in this case I have put int for the rgb values
Also, what would be the syntax of returning the final three integers, seem to only be able to put one i.e. r in this case and not r,g,b?
Java Code:package Chapter4; import java.awt.*; import java.util.Random; public class RandomColor { public int randomcolor(int r, int g, int b){ return r; } public static void main (String []args){ RandomColor pickcolor = new RandomColor(); Random generator = new Random(); int num1 = generator.nextInt(255); int num2 = generator.nextInt(255); int num3 = generator.nextInt(255); int randcolor = pickcolor.randomcolor(num1, num2, num3); System.out.println(randcolor); } }
-
Below is one solution as far as i understood your problem.
And if you want to return three integers, you can return them as an array in the simplest way (or you can create another object to hold all three integers).
Java Code:package Chapter4; import java.awt.*; import java.util.Random; public class RandomColor { int r, g, b; private RandomColor(int r, int g, int b) { this.r = r; this.g = g; this.b = b; } public static RandomColor createRandomColor(){ Random generator = new Random(); int r = generator.nextInt(255); int g = generator.nextInt(255); int b = generator.nextInt(255); RandomColor c = new RandomColor(r,g,b); return c; } public String toString() { return "R: "+r+" G: "+g+" B: "+b; } public static void main (String []args){ RandomColor pickcolor = RandomColor.createRandomColor(); System.out.println(pickColor); } }
- 03-31-2008, 11:42 PM #3
Member
- Join Date
- Mar 2008
- Posts
- 16
- Rep Power
- 0
Thanks for the reply there, but think my level of coding was already at its limit, now am a bit confused ; )
What does the this.r etc do?
-
With 'this' keyword, you are accessing to the class variables. If i do not write 'this' keyword there, i can not change the instance variable of the class. Check parameter names of that method! They are same with class variables' names. In this way, i told compiler which variable to access to eliminate confusion. If you dont want 'this' keyword there just rename method parameters to sth else and remove 'this' keyword.
- 04-01-2008, 12:41 AM #5
Member
- Join Date
- Mar 2008
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
Help with switch color
By Daniel in forum AWT / SwingReplies: 2Last Post: 09-18-2008, 07:54 AM -
Color a button in Gnome
By waka in forum New To JavaReplies: 0Last Post: 02-13-2008, 12:52 AM -
A bit of color!
By tim in forum Java 2DReplies: 8Last Post: 02-11-2008, 11:57 PM -
How to change TXT color Onclick
By dave700800 in forum New To JavaReplies: 1Last Post: 12-08-2007, 01:39 AM -
Color of the focued row in a JTable
By SteM in forum AWT / SwingReplies: 2Last Post: 11-20-2007, 06:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks