Generate Unique Colors

Hi Guys, I have a set of keywords that I'd like to assign a unique color for each keyword. The code must be able to check whether a color is already assigned previously. Ideas would be highly appreciated. Louie
[232 byte] By [_louiebagza] at [2007-11-27 1:18:00]
# 1

import java.awt.*;

import java.util.List;

import java.util.*;

import javax.swing.*;

public class KeyColors {

public static void main(String[] args) {

String s = "abcdefgh";

String[] keys = s.split("(?<=[\\w\\s])");

JPanel panel = new JPanel();

ColorKeys colorKeys = new ColorKeys();

for(int j = 0; j < keys.length; j++) {

Color color = colorKeys.getKeyColor();

panel.add(getLabel(keys[j], color));

}

JOptionPane.showMessageDialog(null, panel, "",

JOptionPane.PLAIN_MESSAGE);

}

private static JLabel getLabel(String s, Color color) {

JLabel label = new JLabel(s, JLabel.CENTER);

label.setForeground(color);

label.setPreferredSize(new Dimension(40,40));

label.setBorder(BorderFactory.createEtchedBorder());

return label;

}

}

class ColorKeys {

Random seed = new Random();

List colors = new ArrayList();

int inc = 51; // 216 unique colors

public Color getKeyColor() {

while(true) {

Color color = getColor();

if(!colors.contains(color)) {

colors.add(color);

return color;

}

}

}

private Color getColor() {

int[] n = new int[3];

for(int j = 0; j < 3; j++) {

n[j] = seed.nextInt(6);

}

return new Color(n[0]*inc, n[1]*inc, n[2]*inc);

}

}

Message was edited by:

crwood

crwooda at 2007-7-11 23:53:45 > top of Java-index,Security,Cryptography...
# 2
Thanks for the code and the prompt reply. But what I really wanted to do is something like this... http://www.geocities.com/louiebagz/files/keywords.docLouie
_louiebagza at 2007-7-11 23:53:45 > top of Java-index,Security,Cryptography...
# 3
Sorry. I tried your .doc file in my WordPerfect and only see a large rectangle containing a vertical column of four small squares in the upper right.
crwooda at 2007-7-11 23:53:45 > top of Java-index,Security,Cryptography...
# 4
Hi, I have uploaded an image file -> http://www.geocities.com/louiebagz/files/keyword.jpg and as well as another doc file -> http://www.geocities.com/louiebagz/files/keyword.doc. Thanks for your kind attention. Louie
_louiebagza at 2007-7-11 23:53:45 > top of Java-index,Security,Cryptography...