import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CellularAutomata2DRotationApplet extends Applet implements ActionListener
{
	private int width = 7;

	private long[] pattern0;
	private long[] pattern1;
	private long[] pattern2;
	private long[] pattern3;
	private long[] pattern4;
	private long[] pattern5;
	private long[] pattern6;
	private long[] pattern7;

	private Choice choicePatternWidth = new Choice();

	public void init()
	{
		this.randomisePattern();
		this.createControls();
	}

	public void randomisePattern()
	{
		pattern0 = new long[width];
		pattern1 = new long[width];
		pattern2 = new long[width];
		pattern3 = new long[width];
		pattern4 = new long[width];
		pattern5 = new long[width];
		pattern6 = new long[width];
		pattern7 = new long[width];

		for(int i = 0; i < width; i++)
			pattern0[i] = (int)(Math.random() * (1L << width));

		/*pattern1[0]  = (pattern0[0] & 4) >> 2;
		pattern1[0] |= (pattern0[1] & 4) >> 1;
		pattern1[0] |= (pattern0[2] & 4);

		pattern1[1]  = (pattern0[0] & 2) >> 1;
		pattern1[1] |= (pattern0[1] & 2);
		pattern1[1] |= (pattern0[2] & 2) << 1;

		pattern1[2]  = (pattern0[0] & 1);
		pattern1[2] |= (pattern0[1] & 1) << 1;
		pattern1[2] |= (pattern0[2] & 1) << 2;*/

		// Pattern 1.
		int bitCompliment = 1 << width;
		int maxShift      = width - 1;
		for(int i = 0; i < width; i++)
		{
			pattern1[i]     = 0;
			bitCompliment >>= 1;
			for(int j = 0; j < width; j++)
			{
				int shift = maxShift - i - j;

				if(shift > 0)
					pattern1[i] |= (pattern0[j] & bitCompliment) >> shift;
				else
					pattern1[i] |= (pattern0[j] & bitCompliment) << -shift;
			}
		}

		// Pattern 2.
		maxShift = width - 1;
		for(int i = 0; i < this.width; i++)
		{
			int shift = maxShift;
			for(int j = 0; j < this.width; j++)
			{
				int oppositeIndex = this.width - i - 1;
				if(shift > 0)
					pattern2[i] |= (pattern0[oppositeIndex] & (1L << j)) << shift;
				else
					pattern2[i] |= (pattern0[oppositeIndex] & (1L << j)) >> -shift;

				shift -= 2;
			}
		}

		// Pattern 3.
		/*pattern3[0]  = (pattern0[0] & 1) << 2;
		pattern3[0] |= (pattern0[1] & 1) << 1;
		pattern3[0] |= (pattern0[2] & 1);

		pattern3[1]  = (pattern0[0] & 2) << 1;
		pattern3[1] |= (pattern0[1] & 2);
		pattern3[1] |= (pattern0[2] & 2) >> 1;

		pattern3[2]  = (pattern0[0] & 4);
		pattern3[2] |= (pattern0[1] & 4) >> 1;
		pattern3[2] |= (pattern0[2] & 4) >> 2;*/

		bitCompliment = 1;
		maxShift      = this.width - 1;
		for(int i = 0; i < width; i++)
		{
			pattern3[i] = 0;
			for(int j = 0; j < this.width; j++)
			{
				int shift = maxShift - i - j;

				if(shift > 0)
					pattern3[i] |= (pattern0[j] & bitCompliment) << shift;
				else
					pattern3[i] |= (pattern0[j] & bitCompliment) >> -shift;
			}
			bitCompliment <<= 1;
		}

		// Patterns 4-7.
		for(int i = 0; i < this.width; i++)
		{
			pattern4[i] = pattern0[this.width - i - 1];
			pattern5[i] = pattern1[this.width - i - 1];
			pattern6[i] = pattern2[this.width - i - 1];
			pattern7[i] = pattern3[this.width - i - 1];
		}
	}

	public void paint(Graphics g)
	{
		int squareWidth = 8;
		int squareHeight = 8;

		// Draw patterns.

			// Pattern 0.
		g.setColor(Color.RED);
		for(int i = 0; i < width; i++)
		{
			for(int j = 0; j < width; j++)
			{
				if((this.pattern0[j] & (1 << i)) != 0)
				{
					g.fillRect(squareWidth * i, j * squareHeight, squareWidth, squareHeight);
				}
				else
				{
					g.drawRect(squareWidth * i, j * squareHeight, squareWidth, squareHeight);
				}
			}
		}

			// Pattern 1.		
		g.setColor(Color.GREEN);
		for(int i = 0; i < width; i++)
		{
			for(int j = 0; j < width; j++)
			{
				if((this.pattern1[j] & (1 << i)) != 0)
				{
					g.fillRect(squareWidth * (i + width + 1), j * squareHeight, squareWidth, squareHeight);
				}
				else
				{
					g.drawRect(squareWidth * (i + width + 1), j * squareHeight, squareWidth, squareHeight);
				}
			}
		}

			// Pattern 2.
		g.setColor(Color.YELLOW);
		for(int i = 0; i < width; i++)
		{
			for(int j = 0; j < width; j++)
			{
				if((this.pattern2[j] & (1 << i)) != 0)
				{
					g.fillRect(squareWidth * (i + (width * 2) + 2), j * squareHeight, squareWidth, squareHeight);
				}
				else
				{
					g.drawRect(squareWidth * (i + (width * 2) + 2), j * squareHeight, squareWidth, squareHeight);
				}
			}
		}

			// Pattern 3.
		g.setColor(Color.BLUE);
		for(int i = 0; i < width; i++)
		{
			for(int j = 0; j < width; j++)
			{
				if((this.pattern3[j] & (1 << i)) != 0)
				{
					g.fillRect(squareWidth * (i + (width * 3) + 3), j * squareHeight, squareWidth, squareHeight);
				}
				else
				{
					g.drawRect(squareWidth * (i + (width * 3) + 3), j * squareHeight, squareWidth, squareHeight);
				}
			}
		}

			// Pattern 4.
		g.setColor(new Color(192, 0, 0));
		for(int i = 0; i < width; i++)
		{
			for(int j = 0; j < width; j++)
			{
				if((this.pattern4[j] & (1 << i)) != 0)
				{
					g.fillRect(squareWidth * i, squareHeight * (j + width + 1), squareWidth, squareHeight);
				}
				else
				{
					g.drawRect(squareWidth * i, squareHeight * (j + width + 1), squareWidth, squareHeight);
				}
			}
		}

			// Pattern 5.
		g.setColor(new Color(0, 192, 0));
		for(int i = 0; i < width; i++)
		{
			for(int j = 0; j < width; j++)
			{
				if((this.pattern5[j] & (1 << i)) != 0)
				{
					g.fillRect(squareWidth * (i + width + 1), squareHeight * (j + width + 1), squareWidth, squareHeight);
				}
				else
				{
					g.drawRect(squareWidth * (i + width + 1), squareHeight * (j + width + 1), squareWidth, squareHeight);
				}
			}
		}

		// Pattern 6.
		g.setColor(new Color(255, 160, 0));
		for(int i = 0; i < width; i++)
		{
			for(int j = 0; j < width; j++)
			{
				if((this.pattern6[j] & (1 << i)) != 0)
				{
					g.fillRect(squareWidth * (i + (width * 2) + 2), squareHeight * (j + width + 1), squareWidth, squareHeight);
				}
				else
				{
					g.drawRect(squareWidth * (i + (width * 2) + 2), squareHeight * (j + width + 1), squareWidth, squareHeight);
				}
			}
		}

		// Pattern 7.
		g.setColor(new Color(0, 0, 128));
		for(int i = 0; i < width; i++)
		{
			for(int j = 0; j < width; j++)
			{
				if((this.pattern7[j] & (1 << i)) != 0)
				{
					g.fillRect(squareWidth * (i + (width * 3) + 3), squareHeight * (j + width + 1), squareWidth, squareHeight);
				}
				else
				{
					g.drawRect(squareWidth * (i + (width * 3) + 3), squareHeight * (j + width + 1), squareWidth, squareHeight);
				}
			}
		}
	}

	public void actionPerformed(ActionEvent e)
	{
		this.width = this.choicePatternWidth.getSelectedIndex() + 2;
		this.randomisePattern();
		this.repaint();
	}

	private void createControls()
	{
		this.setLayout(new BorderLayout());
		Panel p = new Panel();
		for(int i = 2; i < 12; i++)
			this.choicePatternWidth.add("" + i + " x " + i);
		this.choicePatternWidth.select(5);
		p.add(this.choicePatternWidth);
		Button b = new Button("Regenerage Pattern");
		b.addActionListener(this);
		p.add(b);
		this.add(p, BorderLayout.PAGE_END);
	}
}
