//mein Chaos-Applet, J.G. 18. April 1997 
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Event;
import java.util.Date;
import java.util.Random;
import java.lang.Math;
import java.awt.*;
import java.util.*;


public class Chaos46 extends Applet {

	StringBuffer buffer;
	int a;
	int ax;
	int xx;
	Vector meineKreise;

	Date dat;
	int n;
	int n1;
	Random r;
	Kreis k;
	Wuerfel w;

	int zaehler;
	int xk,yk;

	int z,max;
			
	public void init() {
		a = size().height-1;
		ax=size().height-1+40;                         		
		dat = new Date();
		r = new Random(dat.getTime());
		xx=300;
		n1=0;

		meineKreise = new Vector();
		zaehler = 0;
    	Font f = new Font("Helvetica", Font.PLAIN, 12);
    	setFont(f);
		
		buffer = new StringBuffer();
				
		if (getParameter("Maximum").equals("1")) max=1;
		if (getParameter("Maximum").equals("10")) max=10;
		if (getParameter("Maximum").equals("50")) max=50;
		if (getParameter("Maximum").equals("100")) max=100;
		if (getParameter("Maximum").equals("1000")) max=1000;
		  
	 }

	public void start() {
	}

	//-------------------------------------------------
	public boolean mouseDown(Event event, int x, int y) {
		
		//RESET
		if ((x<35) && (y<35)) {init();repaint();return true;}
		//Startpunkt
		if ((zaehler==0) && ((x<300) | (y>100))) {n=0;xk=x; yk=y;zaehler=1;repaint();
			k = new Kreis();  
			k.x=x;
			k.y=y;
			xk=x;
			yk=y;
			meineKreise.addElement(k);						
		
		 return true;}
		 
		for (int z=0; z<max; z++) {
			
		 { 	n =  r.nextInt( ) % 6; 
			n = Math.abs(n);
			n = n +1;}  

		if ((n==1) | (n==2)) {
		  	k = new Kreis();
		  	k.x=xk/2;
		  	k.y=(a+yk)/2;
			xk=xk/2;
		  	yk=(a+yk)/2;
		  	meineKreise.addElement(k);}

		if ((n==3) | (n==4)) {
		  	k = new Kreis();
		  	k.x=(ax+xk)/2;
		  	k.y=(a+yk)/2;
			xk=(ax+xk)/2;
		  	yk=(a+yk)/2;
		  	meineKreise.addElement(k);}

		 if ((n==5) | (n==6)) {
		  	k = new Kreis();
		  	k.x=(ax+2*xk)/4;
		  	k.y=yk/2;
			xk=(ax+2*xk)/4;
		  	yk=yk/2;
		  	meineKreise.addElement(k);}

		zaehler = zaehler + 1;		
	
		  }

		repaint();

		return true;
		}
	//-------------------------------------------------
	public void paint(Graphics g) {

		//Applet-Rechteck zeichnen	
		g.setColor(Color.white);
		g.fillRect(0, 0, size().width - 1, size().height - 1);
		//NEU zeichnen
		g.setColor(Color.green);
		g.drawRect(0, 0, 35, 35);
		g.drawRect(1, 1, 33, 33);
		g.drawString("NEU",5,23);
		//Wuerfel zeichnen
		g.setColor(Color.blue);
		g.drawRect(xx, 0,100 - 1, 100 - 1);
		g.drawRect(xx+1, 1,100 - 3, 100 - 3);
		//Dreieck zeichnen	
		g.setColor(Color.red);		
		g.drawLine(0,a,ax,a);
		g.drawLine(0,a-1,ax,a-1);
		g.drawLine(ax,a,ax/2,0);
		g.drawLine(ax-1,a,ax/2-1,0);
		g.drawLine(ax/2,0,0,a);
		g.drawLine(ax/2+1,0,1,a);
		g.setColor(Color.black);

		int nKreise;
		nKreise=meineKreise.size();

		for (int i=0; i<nKreise; i++) {
			k = (Kreis)meineKreise.elementAt(i);
			if (i==nKreise-1) g.fillOval(k.x-4,k.y-4,8,8);
			if (i==nKreise-2) g.drawOval(k.x-4,k.y-4,8,8);
			if (i<nKreise-2) g.fillRect(k.x-1,k.y-1,2,2);
		}

		//String zeichnen
		buffer = new StringBuffer(zaehler-1 + " . Wurf: " + n);
		if (zaehler==0) g.drawString("W"+ "\u00e4" +"hle einen Startpunkt im Dreieck !", 50, 200);
		if (zaehler==1) {
			g.drawString("Hier w" + "\u00fc" + "rfeln !", 310, 55);
			w = new Wuerfel();
			w.initialize(1,0,0,0,0,0,0,0,1);
			w.draw(g);			
			}		
		if (zaehler>1) g.drawString(buffer.toString(), 320, 125);

		if ((n==1) | (n==2)) {
			k = (Kreis)meineKreise.elementAt(nKreise-2);
			g.drawLine(k.x,k.y,0,a);
			}
		if ((n==3) | (n==4)) {
			k = (Kreis)meineKreise.elementAt(nKreise-2);
			g.drawLine(k.x,k.y,ax,a);
			}
		if ((n==5) | (n==6)) {
			k = (Kreis)meineKreise.elementAt(nKreise-2);
			g.drawLine(k.x,k.y,ax/2,0);
			}
			
	//es werden die Wuerfelaugen gezeichnet
		if (n==1) {
			w = new Wuerfel();
			w.initialize(0,0,0,0,1,0,0,0,0);
			w.draw(g);
			}
	// bei 2 und 2 hintereinander soll die Anordnung der Augen verschieden sein				
		if (n==2) {
			if (n1==2) {
				w = new Wuerfel();
				w.initialize(1,0,0,0,0,0,0,0,1);
				w.draw(g);
				} else {
				w = new Wuerfel();
				w.initialize(0,0,1,0,0,0,1,0,0);
				w.draw(g);
				}
			}

		if (n==3) {
			if (n1==3) {
				w = new Wuerfel();
				w.initialize(1,0,0,0,1,0,0,0,1);
				w.draw(g);
				} else {
				w = new Wuerfel();
				w.initialize(0,0,1,0,1,0,1,0,0);
				w.draw(g);}
				}

		if (n==4) {
				w = new Wuerfel();
				w.initialize(1,0,1,0,0,0,1,0,1);
				w.draw(g);
				}
		
		if (n==5) {
				w = new Wuerfel();
				w.initialize(1,0,1,0,1,0,1,0,1);
				w.draw(g);
				}
		
		if (n==6) {
			if (n1==6) {
				w = new Wuerfel();
				w.initialize(1,1,1,0,0,0,1,1,1);
				w.draw(g);
				} else {
				w = new Wuerfel();
				w.initialize(1,0,1,1,0,1,1,0,1);
				w.draw(g);;
			}
				}
			n1=n;
		}
	}

//==========================================
class Kreis {

	int x;
	int y;
	static final int Radius=2;

	void draw(Graphics g) {
	g.fillOval(this.x-Radius,this.y-Radius,2*Radius,2*Radius);
	}
}
//==========================================
class Wuerfel {

	//int x;
	int b;
	int w1,w2,w3,w4,w5,w6,w7,w8,w9;
	static final int Radius=10;
	
	void draw(Graphics g) {
	b=25;
	//x=300;
	if (this.w1==1) g.fillOval(300+1*b-Radius,1*b-Radius,2*Radius,2*Radius); 
	if (this.w2==1) g.fillOval(300+2*b-Radius,1*b-Radius,2*Radius,2*Radius);
	if (this.w3==1) g.fillOval(300+3*b-Radius,1*b-Radius,2*Radius,2*Radius);
	if (this.w4==1) g.fillOval(300+1*b-Radius,2*b-Radius,2*Radius,2*Radius);
	if (this.w5==1) g.fillOval(300+2*b-Radius,2*b-Radius,2*Radius,2*Radius);
	if (this.w6==1) g.fillOval(300+3*b-Radius,2*b-Radius,2*Radius,2*Radius);
	if (this.w7==1) g.fillOval(300+1*b-Radius,3*b-Radius,2*Radius,2*Radius);
	if (this.w8==1) g.fillOval(300+2*b-Radius,3*b-Radius,2*Radius,2*Radius);
	if (this.w9==1) g.fillOval(300+3*b-Radius,3*b-Radius,2*Radius,2*Radius);
	}
	
	void initialize(int w1,int w2,int w3,int w4,int w5,int w6,int w7,int w8,int w9) {
	this.w1 = w1;
	this.w2 = w2;
	this.w3 = w3;
	this.w4 = w4;
	this.w5 = w5;
	this.w6 = w6;
	this.w7 = w7;
	this.w8 = w8;
	this.w9 = w9;
	}
}