
//package matrices;

import java.awt.*;
import java.lang.*;
import java.util.*;

public class meshes extends BufferedApplet {

    public void init() {
	super.init();
    }

    int M = 15;
    int N = 15;
    MatrixStack T = new MatrixStack();
    sphereMesh sphere = new sphereMesh(M,N);
    sphereMesh temp = new sphereMesh(M,N);
    int w = 0;
    int h = 0;
    int frame = 0;
    int counter = 0;
    
    void drawQuad (double[] a, double[] b,
		   double[] c, double[] d,
		   Graphics g) {
	g.drawLine(x(a[0]), y(a[1]), x(b[0]), y(b[1]));
	g.drawLine(x(b[0]), y(b[1]), x(c[0]), y(c[1]));
	g.drawLine(x(c[0]), y(c[1]), x(a[0]), y(a[1]));

	g.drawLine(x(a[0]), y(a[1]), x(c[0]), y(c[1]));
	g.drawLine(x(c[0]), y(c[1]), x(d[0]), y(d[1]));
	g.drawLine(x(d[0]), y(d[1]), x(a[0]), y(a[1]));

    }

    void copy (MatrixStack t) {
	for (int i = 0; i < M; i++)
	    for (int j = 0; j < N; j++)
		t.get().transform(sphere.mesh[i][j],
				  temp.mesh[i][j]);
    }

    void draw (Graphics g, MatrixStack t) {
        for (int i = 0 ; i < M-1 ; i++)
            for (int j = 0 ; j < N-1 ; j++)
                {
                    drawQuad(temp.mesh[i  ][j  ],
                             temp.mesh[i+1][j  ],
                             temp.mesh[i+1][j+1],
                             temp.mesh[i  ][j+1],g);
                }
    }
    
    int x (double t) { return w/2 + (int)(t*w/4); }
    int y (double t) { return h/2 - (int)(t*w/4); }


    public void render(Graphics g) {

	w = bounds().width;
	h = bounds().height;

        g.setColor(Color.white);
	g.fillRect(0,0,w,h);

	g.setColor(Color.black);
	g.drawString
	    ("I'm not very pretty right now... but maybe after Game 2.",
	     20,20);
	g.setColor(Color.blue);
	g.drawString("Go Yankees!",40,40);

	T = new MatrixStack();
	T.get().Identity();
	g.setColor(Color.blue);

	T.push();
	   T.get().Scale(.5,.5,.5);
	   T.get().translate(.5,.5,.5);
	   T.get().zRotate(0.5+.01*frame);
	   T.get().yRotate(0.5+.01*frame);
	   T.get().xRotate(0.5+.01*frame);
	   T.get().translate((Math.sin(frame*0.05) / 1.5),
			     (Math.cos(frame*0.05) / 1.5),
			     (Math.sin(frame*0.05) / 1.5));
	   T.get().Scale(1.0 + (Math.sin(frame*0.01) / 2),
			 1.0 + (Math.cos(frame*0.01) / 2),
			 1.0 + (Math.sin(frame*0.01) / 2));
	   copy(T);
	   draw(g,T);
	      T.push();
	      g.setColor(Color.red);
	      T.get().Scale(.5,.5,.5);
	      T.get().translate((Math.cos(frame*0.1) / 1.5),
				(Math.cos(frame*0.1) / 1.5),
				(Math.sin(frame*0.1) / 1.5));
	      copy(T);
	      draw(g,T);
	         T.push();
		 g.setColor(Color.green);
		 T.get().Scale(.5,.5,.5);
		 T.get().translate((Math.sin(frame*0.2) / 1.5),
				   (Math.cos(frame*0.2) / 1.5),
				   (Math.sin(frame*0.2) / 1.5));
		 copy(T);
		 draw(g,T);
		 T.pop();
	      T.pop();
	T.pop();

	g.setColor(Color.red);
	T.push();
	   T.get().Scale(.75,.75,.75);
	   T.get().translate(-.5,-.5,-.5);
	   T.get().zRotate(0.5+.01*frame);
	   T.get().yRotate(0.5+.01*frame);
	   T.get().xRotate(0.5+.01*frame);
	   T.get().translate(-(Math.sin(frame*0.05) / 1.5),
			     -(Math.cos(frame*0.05) / 1.5),
			     -(Math.sin(frame*0.05) / 1.5));
	   T.get().Scale(1.0 - (Math.sin(frame*0.01) / 2),
			 1.0 - (Math.cos(frame*0.01) / 2),
			 1.0 - (Math.sin(frame*0.01) / 2));
	   copy(T);
	   draw(g,T);
	      T.push();
	      g.setColor(Color.green);
	      T.get().Scale(.5,.5,.5);
	      T.get().translate((Math.sin(frame*0.1) / 1.5),
				(Math.cos(frame*0.1) / 1.5),
				(Math.sin(frame*0.1) / 1.5));
	      copy(T);
	      draw(g,T);
	         T.push();
		 g.setColor(Color.blue);
		 T.get().Scale(.5,.5,.5);
		 T.get().translate((Math.cos(frame*0.2) / 1.5),
				   (Math.sin(frame*0.2) / 1.5),
				   (Math.cos(frame*0.2) / 1.5));
		 copy(T);
		 draw(g,T);
		 T.pop();
	      T.pop();
	T.pop();

    frame++;
    }

    

}

