// P1001.java:$B@5B?LLBN$N2sE>(B

package applet;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import Glib.*;

public class P1001 extends xApplet
	implements Runnable,ActionListener
{	JButton bt0,bt1;
	Thread th=null;
	Image Im;
	xGraphics3 G3,ImG3;
	int rad=200,bkc=2;
	xRPh ph=new xRPh20((double)rad);
	double rtx=15.0,rty=0.0,vpz=2000.0,drty=1.5;
	xPoint3D Lt=new xPoint3D(1.0,1.0,1.0);

	public void init()
	{	app_init(bkc);
		Container cp=getContentPane();
		cp.setBackground(getBackground());
		cp.setLayout(new FlowLayout(FlowLayout.LEFT));
		bt0=new JButton("Start");
		bt0.addActionListener(this);
		cp.add(bt0);
 		bt1=new JButton("Exit");
		bt1.addActionListener(this);
		cp.add(bt1);
		G3=new xGraphics3(getGraphics());
		G3.set0(CA,1);
	}

	public void actionPerformed(ActionEvent evt)
	{	if(evt.getSource()==bt0)
		{	if(th==null)
			{	th=new Thread(this);
				th.start();
			}
		}
		if(evt.getSource()==bt1)
			System.exit(0);
	}

	public void start(){}

	public void stop()
	{	if(th!=null)
			th=null;
	}

	public void run()
	{	Im=createImage(2*rad+1,2*rad+1);
		ImG3=new xGraphics3(Im.getGraphics());
		ImG3.set0(rad,rad,1);
		ImG3.setshdspectra(1,5);
		Thread th0=Thread.currentThread();
		while(th==th0)
		{	paint2();
			try
			{	th0.sleep(200);
			}
			catch(InterruptedException e){}
		}
	}

	public void paint2()
	{	xPoint3D[]vt=new xPoint3D[ph.NVt];
		xPoint3D[]ps=new xPoint3D[ph.NVt];
		xPoint3D nv,np;
		int[]n=new int[ph.Nvt];
		Point[][]fc_pvt=new Point[ph.NFc][ph.Nvt];
		int[]fc_color=new int[ph.NFc];
		ImG3.setpers(rtx,rty,vpz);
		int No=0,Face;
		xPoint3D lt=xMath.ev(Lt);
		for(int i=0;i<ph.NVt;i++)
		{	vt[i]=ImG3.getproj(ph.Vt[i]);
			ps[i]=ImG3.getpers(ph.Vt[i]);
		}
		for(int i=0;i<ph.NFc;i++)
		{	for(int j=0;j<ph.Nvt;j++)
				n[j]=ph.Ord[i][j];
			nv=xMath.nop(vt[n[0]],vt[n[1]],vt[n[2]]);
			np=xMath.nop(ps[n[0]],ps[n[1]],ps[n[2]]);
			if(np.z>0)
			{	int its=(int)(240*xMath.ip(lt,nv));
				fc_color[No]=ImG3.getspectrum(its);
				for(int j=0;j<ph.Nvt;j++)
					fc_pvt[No][j]=new Point(xMath.fint(ps[n[j]].x),
						xMath.fint(ps[n[j]].y));
				No++;
			}
		}
		ImG3.frectangle(-rad,rad,rad,-rad,240+bkc);
		Face=No;
		for(No=0;No<Face;No++)
			ImG3.fpolygon(fc_pvt[No],ph.Nvt,fc_color[No]);
		G3.putimage(Im,-rad,rad,this);
		rty+=drty;rty=rty-360.0*((int)rty%360);
	}
}

