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

public class lab20070614 extends JPanel implements Runnable
{
	Thread controllore;
	int MAX_RECT = 50;
	Rettangolo[] rettangoli;
	int PAUSA = 1000;
	public lab20070614()
	{
		rettangoli = new Rettangolo[MAX_RECT];
		for (int j = 0; j < rettangoli.length; j++)
		{
			Color t = new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255));
			int dim = (int)(400/MAX_RECT);
			rettangoli[j] = new Rettangolo(j*dim, 0, dim, 400, t);
		}
		addMouseMotionListener(new MouseMotionAdapter(){ public void mouseMoved(MouseEvent e){
			PAUSA = e.getX();
			}});
		controllore = new Thread(this);
		controllore.start();
	}
	public void paint(Graphics g)
	{
		super.paintComponent(g);
		for (int j = 0; j < rettangoli.length; j++)
			rettangoli[j].paint(g);
	}
	public void run()
	{
		while(controllore != null)
		{
			Color t = rettangoli[rettangoli.length-1].background;
			for (int j = rettangoli.length-2; j >= 0; j--)
				rettangoli[j+1].background = rettangoli[j].background;
			rettangoli[0].background = t;
			
			repaint();
			try { controllore.sleep(PAUSA); } catch(Exception e){}
		}
	}
	public static void main(String[] args){ 
		JFrame f = new JFrame(); 
		f.setSize(400,400);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.getContentPane().add(new lab20070614());
		f.setVisible(true);
	}
}

class Rettangolo
{
	int x,y,w,h;
	Color background;
	public Rettangolo(int x, int y, int w, int h, Color c)
	{
		this.x = x;
		this.y = y;
		this.w = w;
		this.h = h;
		this.background = c;
	}
	public void paint(Graphics g)
	{
		g.setColor(background);
		g.fillRect(x,y,w,h);
	}
}
