4 November 2011

How to get random color in Java ?

PROBLEM:

Java gives you narrow range of predefine from eyef**king pink to awesome magneta and wide range of colors which you need define by yourself.
However sometimes you need random color... and question is ... what to do?

SOLUTION:
just copy below method:

import java.awt.Color;
import java.util.Random;
 (...)

public static Color getRandomColor() {
  Random randomRGB = new Random();
  return new Color(  randomRGB.nextInt(255),   randomRGB.nextInt(255),   randomRGB.nextInt(255));
}

* static is not required

No comments:

Post a Comment