get Random Color Code - Java 2D Graphics

Java examples for 2D Graphics:Color

Description

get Random Color Code

Demo Code


//package com.java2s;
import java.util.Random;

public class Main {
    public static void main(String[] argv) throws Exception {
        System.out.println(getRandColorCode());
    }/*from   w  ww.  java  2  s  . c om*/

    public static String getRandColorCode() {
        String r, g, b;
        Random random = new Random();
        r = Integer.toHexString(random.nextInt(256)).toUpperCase();
        g = Integer.toHexString(random.nextInt(256)).toUpperCase();
        b = Integer.toHexString(random.nextInt(256)).toUpperCase();

        r = r.length() == 1 ? "0" + r : r;
        g = g.length() == 1 ? "0" + g : g;
        b = b.length() == 1 ? "0" + b : b;

        return r + g + b;
    }
}

Related Tutorials