The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2002



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


Random Color

Posted by Matt Gerrans on January 29, 2002 at 1:51 AM

> Hello i need a basic way tocreat random colors. I am making a program that displays random shapes and I need those shapes to have random colors. PLease help

// In your class:
static Random random = new Random();

// ... Later, in a method:
Color randomColor = Color.getHSBColor( random.nextFloat(), 1.0F, 1.0F );

You actually get a lot of junky colors when they are too random, so it creates a more aesthetically pleasing effect when they are less random. For instance, you can create an array of colors by incrementing the first parameter of getHSBColor() from 0 to 1.0 (increment size being the inverse of the array size, of course), then use that more limited "spectrum" for a more visually pleasing effect.


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us