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

public class lez20070327_4 extends JFrame
{
	public lez20070327_4()
	{
		setTitle("prova");
		setSize(400, 400);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLayout(new GridLayout(4,4));
		for (int k = 0; k < 16; k++)
			add(new Pannello());
		setVisible(true);
	}
	public static void main(String[] a)
	{
		lez20070327_4 f = new lez20070327_4();
	}
}
class Pannello extends JPanel
{
	double i = 0;
	int vx = 1, vy = 1;
	double x = -1, y = -1;
	double step = 0.1;
	Color miocolore;
	Image img;
	Pannello()
	{
		img = Toolkit.getDefaultToolkit().getImage("freekevin.jpg");
		step = Math.random();
		miocolore = new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255));
	}
	public void paint(Graphics g)
	{
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D)g;
		if (x == -1 && y == -1)
		{
			x = Math.random()*(getWidth()-20);
			y = Math.random()*(getHeight()-20);		
		}
		g.setColor(miocolore);
		g.drawImage(img, 1, 1, getWidth()-2,getHeight()-2, null);
		g.fillOval((int)x, (int)y, 20,20);
		x = x + step*vx;
		y = y + step*vy;
		if ( (x > getWidth()-20) || (x < 0) ) vx = vx * (-1);
		if ( (y > getHeight()-20) || (y < 0) ) vy = vy * (-1);
		repaint();
	}
}