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

public class scorricolori extends JFrame
{
	JButton btnS, btnD;
	JPanel[] pannelli;
	int maxColori = 10;
	JSlider slider;
	JPanel pannelloColori;
	
	public scorricolori()
	{
		setTitle("scorri colori");
		setSize(400,100);
		
		pannelloColori = new JPanel();
		
		pannelloColori.setLayout(new GridLayout(1,maxColori+2));
		btnS = new JButton("<<");
		btnS.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				Color primoColore = pannelli[0].getBackground();
				for (int j = 0; j < maxColori-1; j++) 
					pannelli[j].setBackground(pannelli[j+1].getBackground());
				pannelli[maxColori-1].setBackground(primoColore);
			}});
		
		btnD = new JButton(">>");
		btnD.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				Color ultimoColore = pannelli[maxColori-1].getBackground();
				for (int j = maxColori-1; j > 0; j--) 
					pannelli[j].setBackground(pannelli[j-1].getBackground());
				pannelli[0].setBackground(ultimoColore);
			}});
		
		pannelloColori.add(btnS);
		pannelli = new JPanel[maxColori];
		for (int j = 0; j < maxColori; j++)
		{
			pannelli[j] = new JPanel();
			pannelli[j].setBackground(new Color((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255)));
			pannelloColori.add(pannelli[j]);
		}
		pannelloColori.add(btnD);
		
		setLayout(new BorderLayout());
		add(pannelloColori, BorderLayout.CENTER);
		
		slider = new JSlider(0,15,maxColori);
		slider.addChangeListener(new ChangeListener(){
			public void stateChanged(ChangeEvent e){
				maxColori = slider.getValue();
				pannelloColori = new JPanel();
				impostaPannello();
				add(pannelloColori, BorderLayout.CENTER);
				for (int j = 0; j < maxColori; j++)
					pannelli[j].setBackground(pannelli[j].getBackground());
			}});
		
		add(slider, BorderLayout.SOUTH);
		
		setVisible(true);
	}
	
	public void impostaPannello()
	{
		pannelloColori.setLayout(new GridLayout(1,maxColori+2));
		pannelloColori.add(btnS);
		pannelli = new JPanel[maxColori];
		for (int j = 0; j < maxColori; j++)
		{
			pannelli[j] = new JPanel();
			pannelli[j].setBackground(new Color((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255)));
			pannelloColori.add(pannelli[j]);
		}
		pannelloColori.add(btnD);
	}
	
	public static void main(String[] a)
	{
		scorricolori f = new scorricolori();
	}
}

