import java.awt.Color;
import java.util.ArrayList;
import java.util.Random;
class ColorList {
public ArrayList <java.awt.Color> colList = new ArrayList <java.awt.Color> ();
ColorList(){
Random rng = new Random(); // rand num generator
// loop 256 times through
for (int i=0; i < 256 ; i++) {
//get random integers between 0 and 255 for r,g,b
int red = rng.nextInt(256); // creates red
int green = rng.nextInt(256); // creates green
int blue = rng.nextInt(256); // creates blue
//Color is the class of c, c is made of 3 components, r,g,b
Color c = new Color(red,green,blue); // creates a color
colList.add(c);
}
}
public static void main(String[] args) {
ColorList colors = new ColorList();
Color aColor = colors.colList.get(12);
System.out.println(aColor);
}// constructor
}// ColorList
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ColorStrips {
// "psvm" - our static "launcher" function (starting point for execution)
public static void main(String[] args) {
ColorList cols = new ColorList(); // generate colors, to use for populating strips below
JFrame frame = new JFrame(); // our outer window
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Makes 256 rows of panels in one column
frame.setLayout(new GridLayout(256,1));
JPanel p;
Color c;
// 256 color strips cx v
for (int i=0; i < 256 ; i++) {
p = new JPanel();
c = cols.colList.get(i);
p.setBackground(c); // color our panel
p.setPreferredSize(new Dimension(100,4)); // set the dimensions of our panel
frame.add(p); // pop the panel into the frame
}
// all done!!!
frame.pack(); // arrange all the panels
frame.setVisible(true); // show to the world :)
}// main()
}// ColorStrips