import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Colour_Box extends JPanel implements Runnable
{
    Color color = nextColor();
    static final Color[] colors = {Color.gray, Color.red, Color.yellow, Color.green, Color.cyan, Color.blue };

    static Color nextColor()
    {
	return colors[ (int) (Math.random()*colors.length) ];
    }

    public void paintComponent(Graphics g)
    {
	super.paintComponent(g);
	g.setColor(color);
	Dimension s = getSize();
	g.fillRect(0,0, s.width, s.height);
    }

    public void run()
    {
	while ( true ) {
	    color = nextColor();
	    repaint();
	    try {
		Thread.sleep((int)(10000*Math.random()));
	    } catch (InterruptedException e ){}
	}
    }
}//end of Colour_Box


public class Colour_Grid extends JFrame
{
    static final int SIZE=5;
    
    Colour_Grid()
    {
	Container cp = getContentPane();
	addWindowListener( new windowHandler() );

	cp.setLayout(new GridLayout ( SIZE, SIZE ) );

	for ( int i=0; i< SIZE * SIZE; i++ ) {
	    Colour_Box box = new Colour_Box();
	    cp.add(box);
	    new Thread(box).start();
	}
    }
    
    

    class windowHandler extends WindowAdapter
    {
	public void windowClosing( WindowEvent e )
	{
	    System.exit(0);
	}
    }

    public static void main (String []args)
    {
	Colour_Grid boxes = new Colour_Grid();
	boxes.setSize(500, 500);
	boxes.setVisible( true );
    }

} //end of Colour_Grid

