import javax.swing.*;
import java.awt.*;

public class Prog7 extends JFrame
{//displays moving rectangle on coffee window. step 6 four partitions
//multi threads of execution; 
	 static int size;
	
	public Prog7()
	{
		super("animation");
		size = 500;
		setSize(size, size);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel panel = new JPanel();
		panel.setLayout( new GridLayout(2, 2) );//2 row 2 column
		Test test0 = new Test();
		Test test1 = new Test();
		Test test2 = new Test();
		Test test3 = new Test();
		test0.setBackground(Color.black);
		test1.setBackground(Color.cyan);
		test2.setBackground(Color.red);
		test3.setBackground(Color.green);
		panel.add(test0);
		panel.add(test1);
		panel.add(test2);
		panel.add(test3);
		setContentPane(panel);	
		show();
		Temp one = new Temp(test0);
		Temp two = new Temp(test1);
		Temp three = new Temp(test2);
		Temp four = new Temp(test3);
		one.start();//how thread is envoked
		two.start();
		three.start();
		four.start();
	}
	
	public static void main(String[] asd)
	{		
		Prog7 obj = new Prog7();			
	}
}

class Temp extends Thread
{
	Test animation;
	
	public Temp( Test animation)
	{
		this.animation = animation;
	}
	
	public void run()
	{
		animation.move();
	}
}
	

class Test extends JPanel
{
	int x = 0;
	
	public void move()
	{
		while( x < Prog7.size/2)
		{
			x++;
			repaint();//calls paintComponent
			try
			{
				Thread.sleep(50);
			}catch(InterruptedException e)
			{}
		}
	}
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);//required to erase previous image
		int side = 20;
		Graphics2D g2 = (Graphics2D)g;
		g2.setColor(Color.yellow);
		g2.fillRect(x, x, side, side);
		//(x,x) is coordinate of upper left-hand corner 
	}
}

