Thread: Color objects
View Single Post
  #2 (permalink)  
Old 04-01-2008, 12:07 AM
Java Tip Java Tip is offline
Moderator
 
Join Date: Nov 2007
Posts: 1,637
Java Tip will become famous soon enoughJava Tip will become famous soon enough
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).

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); } }
Reply With Quote