import java.awt.*;
import java.applet.*;




public class lego2 extends Applet
{
        protected String Top = new String();
        protected String NextMessage = new String();
        protected String NoroomMessage = new String();
        double xtheta,ytheta,ztheta,yth;
        double ang = 90;
        int startx,starty;
        int n,nv,nth,topnum,sideno;
        Color bground = new Color(100,100,220);
        Button newbrick = new Button("New brick");

        Font Danbury = new Font(Top,20,20);
        int rot,grun,blau;//colours of newbrick and variables used for highlighting squares for drop
        int maxht = 9;
        int brlength,brwidth;
        int brxlen,brzlen;
        int basex=5;int basez=5;int basendx = 13;int basendz =13;
        int hx = basex;int hz = basez;
        int newbx = 17;int newbz=22;int newby=2;
        int nbhx = newbx;int nbhy = newby;int nbhz = newbz;
        Image sbuff1 = null; //for flicker free graphics
        Graphics offscng = null; //for flicker free graphics

        boolean newbrickon = false;
        boolean noroom = false;
        boolean rotated = false;
        int nx,ny,nz,hy,px,py,pz,temp,nx1,nz1,bx1,bx2,bz1,bz2;
        int barry[][][] = new int[40][12][40]; //barry[x][y][z] 0,0,0 is the front left bottom corner of the base at startup

        protected List Colourlist = new List(3,false);
        protected List Blengthlist = new List(3,false);
        protected List Bwidthlist = new List(3,false);


        brick2 br[][][] = new brick2[40][12][40];
        brick2 ts[][][] = new brick2[40][12][40];

        public void init()
        {
                bx1=-20-(basex*5);bx2=-20+((18-basex)*5);
                bz1=-20-(basez*5);bz2=-20+((18-basez)*5);


                barry[0][0][0]=0;
                br[0][0][0] = new brick2(23,70,28,bx1,30,bz2,bx1,30,bz1,bx1,30,bz1,bx1,30,bz2,bx2,30,bz2,bx2,30,bz1,bx2,30,bz1,bx2,30,bz2,0,0,100,1);
                ts[0][0][0] = new brick2(23,70,28,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,1);

          for(nz=newbz;nz<newbz+9;nz++)
                for (nx=newbx;nx<newbx+9;nx++)
                {
                        br[nx][newby][nz] = new brick2(255,0,0,-20+((nx-basex)*5),23-(newby*7),-15+((nz-basez)*5),-20+((nx-basex)*5),23-(newby*7),-20+((nz-basez)*5),-20+((nx-basex)*5),30-(newby*7),-20+((nz-basez)*5),-20+((nx-basex)*5),30-(newby*7),-15+((nz-basez)*5),-15+((nx-basex)*5),23-(newby*7),-15+((nz-basez)*5),-15+((nx-basex)*5),23-(newby*7),-20+((nz-basez)*5),-15+((nx-basex)*5),30-(newby*7),-20+((nz-basez)*5),-15+((nx-basex)*5),30-(newby*7),-15+((nz-basez)*5),0,0,100,1);
                        ts[nx][newby][nz] = new brick2(255,0,0,-19+((nx-basex)*5),21-(newby*7),-16+((nz-basez)*5),-19+((nx-basex)*5),21-(newby*7),-19+((nz-basez)*5),-19+((nx-basex)*5),23-(newby*7),-19+((nz-basez)*5),-19+((nx-basex)*5),23-(newby*7),-16+((nz-basez)*5),-16+((nx-basex)*5),21-(newby*7),-16+((nz-basez)*5),-16+((nx-basex)*5),21-(newby*7),-19+((nz-basez)*5),-16+((nx-basex)*5),23-(newby*7),-19+((nz-basez)*5),-16+((nx-basex)*5),23-(newby*7),-16+((nz-basez)*5),0,0,100,1);
                }




           for(nz=0;nz<(basendz-basez);nz++)
                for (nx=0;nx<(basendx-basex);nx++)
                {
                        br[nx+basex][0][nz+basez] = new brick2(0,255,0,-20+(nx*5),23-(ny*7),-15+(nz*5),-20+(nx*5),23-(ny*7),-20+(nz*5),-20+(nx*5),30-(ny*7),-20+(nz*5),-20+(nx*5),30-(ny*7),-15+(nz*5),-15+(nx*5),23-(ny*7),-15+(nz*5),-15+(nx*5),23-(ny*7),-20+(nz*5),-15+(nx*5),30-(ny*7),-20+(nz*5),-15+(nx*5),30-(ny*7),-15+(nz*5),0,0,100,1);
                        ts[nx+basex][0][nz+basez] = new brick2(0,255,0,-19+(nx*5),21-(ny*7),-16+(nz*5),-19+(nx*5),21-(ny*7),-19+(nz*5),-19+(nx*5),23-(ny*7),-19+(nz*5),-19+(nx*5),23-(ny*7),-16+(nz*5),-16+(nx*5),21-(ny*7),-16+(nz*5),-16+(nx*5),21-(ny*7),-19+(nz*5),-16+(nx*5),23-(ny*7),-19+(nz*5),-16+(nx*5),23-(ny*7),-16+(nz*5),0,0,100,1);
                }


            barry[basex][0][basez]=16;
                barry[basendx-1][0][basez]=15;
                barry[basex][0][basendz-1]=13;
                barry[basendx-1][0][basendz-1]=14;

                for (nx=basex+1;nx<basendx-1;nx++)
                {barry[nx][0][basez]=12;
                 barry[nx][0][basendz-1]=11;}
                for (nz=basez+1;nz<basendz-1;nz++)
                {barry[basex][0][nz]=9;
                 barry[basendx-1][0][nz]=10;}
                for(nz=basez+1;nz<basendz-1;nz++)
                for (nx=basex+1;nx<basendx-1;nx++)
                  barry[nx][0][nz]=25;

                resize(500,320);
                setBackground(bground);
                add(newbrick);
                Colourlist.addItem("Red");Blengthlist.addItem("1");
                Colourlist.addItem("White");Blengthlist.addItem("2");
                Colourlist.addItem("Yellow");Blengthlist.addItem("3");
                Colourlist.addItem("Green");Blengthlist.addItem("4");
                Colourlist.addItem("Blue");Blengthlist.addItem("8");
                Colourlist.addItem("Black");
                Bwidthlist.addItem("1");Bwidthlist.addItem("2");
                add(Colourlist);
                add(Blengthlist);
                add(Bwidthlist);
                Colourlist.select(0);
                Blengthlist.select(0);
                Bwidthlist.select(0);
                NoroomMessage = "Can't put it there";
                NextMessage =" Brick Length                    Brick Width";
        }

        public final void update(Graphics SBg)
        {
                sbuff1 = createImage(500,320);
                offscng = sbuff1.getGraphics();
                offscng.setColor(getBackground());
                offscng.fillRect(0,0,500,320);
                paint(offscng);
                SBg.drawImage(sbuff1,0,0,null);

        }

        public  void paint(Graphics g)
        {
                br[0][0][0].toppoly(xtheta,ytheta);
                if (br[0][0][0].BF[2]<1){g.setColor(br[0][0][0].tric[2]);g.fillPolygon(br[0][0][0].brickside[2]);}

                for(nz1=0;nz1<30;nz1++)
                  for(nx1=0;nx1<30;nx1++)
                        for(ny=0;ny<maxht;ny++)

                        {  switch((int)((ytheta)/45))
                                { case 0:nz=29-nz1;nx=nx1;break;
                                  case 1:nz=29-nx1;nx=nz1;break;
                                  case 2:nz=nx1;nx=nz1;break;
                                  case 3:nx=nx1;nz=nz1;break;
                                  case 4:nx=29-nx1;nz=nz1;break;
                                  case 5:nz=nx1;nx=29-nz1;break;
                                  case 6:nz=29-nx1;nx=29-nz1;break;
                                  case 7:nx=29-nx1;nz=29-nz1;break;

                                 }

                                switch(barry[nx][ny][nz])
                                 {
                                        case 0 : break;
                                        case 1 :  br[nx][ny][nz].leftpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[0]<1){g.setColor(br[nx][ny][nz].tric[0]);g.fillPolygon(br[nx][ny][nz].brickside[0]);} break;
                                        case 2 : br[nx][ny][nz].rightpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[1]<1){g.setColor(br[nx][ny][nz].tric[1]);g.fillPolygon(br[nx][ny][nz].brickside[1]);} break;
                                        case 3 :  br[nx][ny][nz].backpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[3]<1){g.setColor(br[nx][ny][nz].tric[3]);g.fillPolygon(br[nx][ny][nz].brickside[3]);} break;
                                        case 4 : br[nx][ny][nz].frontpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[4]<1){g.setColor(br[nx][ny][nz].tric[4]);g.fillPolygon(br[nx][ny][nz].brickside[4]);} break;
                                        case 5 :  br[nx][ny][nz].leftpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[0]<1){g.setColor(br[nx][ny][nz].tric[0]);g.fillPolygon(br[nx][ny][nz].brickside[0]);}
                                                          br[nx][ny][nz].backpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[3]<1){g.setColor(br[nx][ny][nz].tric[3]);g.fillPolygon(br[nx][ny][nz].brickside[3]);} break;
                                        case 6 :  br[nx][ny][nz].backpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[3]<1){g.setColor(br[nx][ny][nz].tric[3]);g.fillPolygon(br[nx][ny][nz].brickside[3]);}
                                                         br[nx][ny][nz].rightpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[1]<1){g.setColor(br[nx][ny][nz].tric[1]);g.fillPolygon(br[nx][ny][nz].brickside[1]);} break;
                                        case 7 : br[nx][ny][nz].rightpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[1]<1){g.setColor(br[nx][ny][nz].tric[1]);g.fillPolygon(br[nx][ny][nz].brickside[1]);}
                                                         br[nx][ny][nz].frontpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[4]<1){g.setColor(br[nx][ny][nz].tric[4]);g.fillPolygon(br[nx][ny][nz].brickside[4]);} break;
                                        case 8 : br[nx][ny][nz].frontpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[4]<1){g.setColor(br[nx][ny][nz].tric[4]);g.fillPolygon(br[nx][ny][nz].brickside[4]);}
                                                          br[nx][ny][nz].leftpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[0]<1){g.setColor(br[nx][ny][nz].tric[0]);g.fillPolygon(br[nx][ny][nz].brickside[0]);} break;
                                        case 9 :  br[nx][ny][nz].leftpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[0]<1){g.setColor(br[nx][ny][nz].tric[0]);g.fillPolygon(br[nx][ny][nz].brickside[0]);}
                                                           br[nx][ny][nz].toppoly(xtheta,ytheta);if (br[nx][ny][nz].BF[2]<1){g.setColor(br[nx][ny][nz].tric[2]);g.fillPolygon(br[nx][ny][nz].brickside[2]);}
                                                           ts[nx][ny][nz].xrot(xtheta,ytheta);ts[nx][ny][nz].plane();ts[nx][ny][nz].projection();for(sideno =0;sideno<5;sideno++){if(ts[nx][ny][nz].BF[sideno]<1){g.setColor(ts[nx][ny][nz].tric[sideno]);g.fillPolygon(ts[nx][ny][nz].brickside[sideno]);}}break;
                                        case 10 :br[nx][ny][nz].rightpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[1]<1){g.setColor(br[nx][ny][nz].tric[1]);g.fillPolygon(br[nx][ny][nz].brickside[1]);}
                                                           br[nx][ny][nz].toppoly(xtheta,ytheta);if (br[nx][ny][nz].BF[2]<1){g.setColor(br[nx][ny][nz].tric[2]);g.fillPolygon(br[nx][ny][nz].brickside[2]);}
                                                           ts[nx][ny][nz].xrot(xtheta,ytheta);ts[nx][ny][nz].plane();ts[nx][ny][nz].projection();for(sideno =0;sideno<5;sideno++){if(ts[nx][ny][nz].BF[sideno]<1){g.setColor(ts[nx][ny][nz].tric[sideno]);g.fillPolygon(ts[nx][ny][nz].brickside[sideno]);}}break;
                                        case 11 : br[nx][ny][nz].backpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[3]<1){g.setColor(br[nx][ny][nz].tric[3]);g.fillPolygon(br[nx][ny][nz].brickside[3]);}
                                                           br[nx][ny][nz].toppoly(xtheta,ytheta);if (br[nx][ny][nz].BF[2]<1){g.setColor(br[nx][ny][nz].tric[2]);g.fillPolygon(br[nx][ny][nz].brickside[2]);}
                                                           ts[nx][ny][nz].xrot(xtheta,ytheta);ts[nx][ny][nz].plane();ts[nx][ny][nz].projection();for(sideno =0;sideno<5;sideno++){if(ts[nx][ny][nz].BF[sideno]<1){g.setColor(ts[nx][ny][nz].tric[sideno]);g.fillPolygon(ts[nx][ny][nz].brickside[sideno]);}}break;
                                        case 12 :br[nx][ny][nz].frontpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[4]<1){g.setColor(br[nx][ny][nz].tric[4]);g.fillPolygon(br[nx][ny][nz].brickside[4]);}
                                                           br[nx][ny][nz].toppoly(xtheta,ytheta);if (br[nx][ny][nz].BF[2]<1){g.setColor(br[nx][ny][nz].tric[2]);g.fillPolygon(br[nx][ny][nz].brickside[2]);}
                                                           ts[nx][ny][nz].xrot(xtheta,ytheta);ts[nx][ny][nz].plane();ts[nx][ny][nz].projection();for(sideno =0;sideno<5;sideno++){if(ts[nx][ny][nz].BF[sideno]<1){g.setColor(ts[nx][ny][nz].tric[sideno]);g.fillPolygon(ts[nx][ny][nz].brickside[sideno]);}}break;
                                        case 13 : br[nx][ny][nz].leftpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[0]<1){g.setColor(br[nx][ny][nz].tric[0]);g.fillPolygon(br[nx][ny][nz].brickside[0]);}
                                                          br[nx][ny][nz].backpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[3]<1){g.setColor(br[nx][ny][nz].tric[3]);g.fillPolygon(br[nx][ny][nz].brickside[3]);}
                                                           br[nx][ny][nz].toppoly(xtheta,ytheta);if (br[nx][ny][nz].BF[2]<1){g.setColor(br[nx][ny][nz].tric[2]);g.fillPolygon(br[nx][ny][nz].brickside[2]);}
                                                           ts[nx][ny][nz].xrot(xtheta,ytheta);ts[nx][ny][nz].plane();ts[nx][ny][nz].projection();for(sideno =0;sideno<5;sideno++){if(ts[nx][ny][nz].BF[sideno]<1){g.setColor(ts[nx][ny][nz].tric[sideno]);g.fillPolygon(ts[nx][ny][nz].brickside[sideno]);}}break;
                                        case 14 : br[nx][ny][nz].backpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[3]<1){g.setColor(br[nx][ny][nz].tric[3]);g.fillPolygon(br[nx][ny][nz].brickside[3]);}
                                                         br[nx][ny][nz].rightpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[1]<1){g.setColor(br[nx][ny][nz].tric[1]);g.fillPolygon(br[nx][ny][nz].brickside[1]);}
                                                           br[nx][ny][nz].toppoly(xtheta,ytheta);if (br[nx][ny][nz].BF[2]<1){g.setColor(br[nx][ny][nz].tric[2]);g.fillPolygon(br[nx][ny][nz].brickside[2]);}
                                                           ts[nx][ny][nz].xrot(xtheta,ytheta);ts[nx][ny][nz].plane();ts[nx][ny][nz].projection();for(sideno =0;sideno<5;sideno++){if(ts[nx][ny][nz].BF[sideno]<1){g.setColor(ts[nx][ny][nz].tric[sideno]);g.fillPolygon(ts[nx][ny][nz].brickside[sideno]);}}break;
                                        case 15 :br[nx][ny][nz].rightpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[1]<1){g.setColor(br[nx][ny][nz].tric[1]);g.fillPolygon(br[nx][ny][nz].brickside[1]);}
                                                         br[nx][ny][nz].frontpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[4]<1){g.setColor(br[nx][ny][nz].tric[4]);g.fillPolygon(br[nx][ny][nz].brickside[4]);}
                                                           br[nx][ny][nz].toppoly(xtheta,ytheta);if (br[nx][ny][nz].BF[2]<1){g.setColor(br[nx][ny][nz].tric[2]);g.fillPolygon(br[nx][ny][nz].brickside[2]);}
                                                           ts[nx][ny][nz].xrot(xtheta,ytheta);ts[nx][ny][nz].plane();ts[nx][ny][nz].projection();for(sideno =0;sideno<5;sideno++){if(ts[nx][ny][nz].BF[sideno]<1){g.setColor(ts[nx][ny][nz].tric[sideno]);g.fillPolygon(ts[nx][ny][nz].brickside[sideno]);}}break;
                                        case 16 :br[nx][ny][nz].frontpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[4]<1){g.setColor(br[nx][ny][nz].tric[4]);g.fillPolygon(br[nx][ny][nz].brickside[4]);}
                                                          br[nx][ny][nz].leftpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[0]<1){g.setColor(br[nx][ny][nz].tric[0]);g.fillPolygon(br[nx][ny][nz].brickside[0]);}
                                                           br[nx][ny][nz].toppoly(xtheta,ytheta);if (br[nx][ny][nz].BF[2]<1){g.setColor(br[nx][ny][nz].tric[2]);g.fillPolygon(br[nx][ny][nz].brickside[2]);}
                                                           ts[nx][ny][nz].xrot(xtheta,ytheta);ts[nx][ny][nz].plane();ts[nx][ny][nz].projection();for(sideno =0;sideno<5;sideno++){if(ts[nx][ny][nz].BF[sideno]<1){g.setColor(ts[nx][ny][nz].tric[sideno]);g.fillPolygon(ts[nx][ny][nz].brickside[sideno]);}}break;
                                        case 17 :br[nx][ny][nz].frontpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[4]<1){g.setColor(br[nx][ny][nz].tric[4]);g.fillPolygon(br[nx][ny][nz].brickside[4]);}
                                                          br[nx][ny][nz].backpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[3]<1){g.setColor(br[nx][ny][nz].tric[3]);g.fillPolygon(br[nx][ny][nz].brickside[3]);}
                                                           br[nx][ny][nz].toppoly(xtheta,ytheta);if (br[nx][ny][nz].BF[2]<1){g.setColor(br[nx][ny][nz].tric[2]);g.fillPolygon(br[nx][ny][nz].brickside[2]);}
                                                           ts[nx][ny][nz].xrot(xtheta,ytheta);ts[nx][ny][nz].plane();ts[nx][ny][nz].projection();for(sideno =0;sideno<5;sideno++){if(ts[nx][ny][nz].BF[sideno]<1){g.setColor(ts[nx][ny][nz].tric[sideno]);g.fillPolygon(ts[nx][ny][nz].brickside[sideno]);}}break;
                                        case 18 : br[nx][ny][nz].leftpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[0]<1){g.setColor(br[nx][ny][nz].tric[0]);g.fillPolygon(br[nx][ny][nz].brickside[0]);}
                                                         br[nx][ny][nz].rightpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[1]<1){g.setColor(br[nx][ny][nz].tric[1]);g.fillPolygon(br[nx][ny][nz].brickside[1]);}
                                                         br[nx][ny][nz].toppoly(xtheta,ytheta);if (br[nx][ny][nz].BF[2]<1){g.setColor(br[nx][ny][nz].tric[2]);g.fillPolygon(br[nx][ny][nz].brickside[2]);}
                                                           ts[nx][ny][nz].xrot(xtheta,ytheta);ts[nx][ny][nz].plane();ts[nx][ny][nz].projection();for(sideno =0;sideno<5;sideno++){if(ts[nx][ny][nz].BF[sideno]<1){g.setColor(ts[nx][ny][nz].tric[sideno]);g.fillPolygon(ts[nx][ny][nz].brickside[sideno]);}}break;
                                        case 20 :br[nx][ny][nz].leftpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[0]<1){g.setColor(br[nx][ny][nz].tric[0]);g.fillPolygon(br[nx][ny][nz].brickside[0]);}
                                                         br[nx][ny][nz].rightpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[1]<1){g.setColor(br[nx][ny][nz].tric[1]);g.fillPolygon(br[nx][ny][nz].brickside[1]);}
                                                         br[nx][ny][nz].backpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[3]<1){g.setColor(br[nx][ny][nz].tric[3]);g.fillPolygon(br[nx][ny][nz].brickside[3]);}
                                                         br[nx][ny][nz].toppoly(xtheta,ytheta);if (br[nx][ny][nz].BF[2]<1){g.setColor(br[nx][ny][nz].tric[2]);g.fillPolygon(br[nx][ny][nz].brickside[2]);}
                                                           ts[nx][ny][nz].xrot(xtheta,ytheta);ts[nx][ny][nz].plane();ts[nx][ny][nz].projection();for(sideno =0;sideno<5;sideno++){if(ts[nx][ny][nz].BF[sideno]<1){g.setColor(ts[nx][ny][nz].tric[sideno]);g.fillPolygon(ts[nx][ny][nz].brickside[sideno]);}}break;
                                        case 21 :br[nx][ny][nz].frontpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[4]<1){g.setColor(br[nx][ny][nz].tric[4]);g.fillPolygon(br[nx][ny][nz].brickside[4]);}
                                                         br[nx][ny][nz].rightpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[1]<1){g.setColor(br[nx][ny][nz].tric[1]);g.fillPolygon(br[nx][ny][nz].brickside[1]);}
                                                         br[nx][ny][nz].backpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[3]<1){g.setColor(br[nx][ny][nz].tric[3]);g.fillPolygon(br[nx][ny][nz].brickside[3]);}
                                                         br[nx][ny][nz].toppoly(xtheta,ytheta);if (br[nx][ny][nz].BF[2]<1){g.setColor(br[nx][ny][nz].tric[2]);g.fillPolygon(br[nx][ny][nz].brickside[2]);}
                                                           ts[nx][ny][nz].xrot(xtheta,ytheta);ts[nx][ny][nz].plane();ts[nx][ny][nz].projection();for(sideno =0;sideno<5;sideno++){if(ts[nx][ny][nz].BF[sideno]<1){g.setColor(ts[nx][ny][nz].tric[sideno]);g.fillPolygon(ts[nx][ny][nz].brickside[sideno]);}}break;
                                        case 22 :br[nx][ny][nz].frontpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[4]<1){g.setColor(br[nx][ny][nz].tric[4]);g.fillPolygon(br[nx][ny][nz].brickside[4]);}
                                                          br[nx][ny][nz].leftpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[0]<1){g.setColor(br[nx][ny][nz].tric[0]);g.fillPolygon(br[nx][ny][nz].brickside[0]);}
                                                         br[nx][ny][nz].rightpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[1]<1){g.setColor(br[nx][ny][nz].tric[1]);g.fillPolygon(br[nx][ny][nz].brickside[1]);}
                                                         br[nx][ny][nz].toppoly(xtheta,ytheta);if (br[nx][ny][nz].BF[2]<1){g.setColor(br[nx][ny][nz].tric[2]);g.fillPolygon(br[nx][ny][nz].brickside[2]);}
                                                           ts[nx][ny][nz].xrot(xtheta,ytheta);ts[nx][ny][nz].plane();ts[nx][ny][nz].projection();for(sideno =0;sideno<5;sideno++){if(ts[nx][ny][nz].BF[sideno]<1){g.setColor(ts[nx][ny][nz].tric[sideno]);g.fillPolygon(ts[nx][ny][nz].brickside[sideno]);}}break;
                                        case 23 :br[nx][ny][nz].frontpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[4]<1){g.setColor(br[nx][ny][nz].tric[4]);g.fillPolygon(br[nx][ny][nz].brickside[4]);}
                                                          br[nx][ny][nz].leftpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[0]<1){g.setColor(br[nx][ny][nz].tric[0]);g.fillPolygon(br[nx][ny][nz].brickside[0]);}
                                                         br[nx][ny][nz].backpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[3]<1){g.setColor(br[nx][ny][nz].tric[3]);g.fillPolygon(br[nx][ny][nz].brickside[3]);}
                                                         br[nx][ny][nz].toppoly(xtheta,ytheta);if (br[nx][ny][nz].BF[2]<1){g.setColor(br[nx][ny][nz].tric[2]);g.fillPolygon(br[nx][ny][nz].brickside[2]);}
                                                           ts[nx][ny][nz].xrot(xtheta,ytheta);ts[nx][ny][nz].plane();ts[nx][ny][nz].projection();for(sideno =0;sideno<5;sideno++){if(ts[nx][ny][nz].BF[sideno]<1){g.setColor(ts[nx][ny][nz].tric[sideno]);g.fillPolygon(ts[nx][ny][nz].brickside[sideno]);}}break;
                                        case 24 :br[nx][ny][nz].frontpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[4]<1){g.setColor(br[nx][ny][nz].tric[4]);g.fillPolygon(br[nx][ny][nz].brickside[4]);}
                                                          br[nx][ny][nz].leftpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[0]<1){g.setColor(br[nx][ny][nz].tric[0]);g.fillPolygon(br[nx][ny][nz].brickside[0]);}
                                                         br[nx][ny][nz].rightpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[1]<1){g.setColor(br[nx][ny][nz].tric[1]);g.fillPolygon(br[nx][ny][nz].brickside[1]);}
                                                         br[nx][ny][nz].backpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[3]<1){g.setColor(br[nx][ny][nz].tric[3]);g.fillPolygon(br[nx][ny][nz].brickside[3]);}
                                                         br[nx][ny][nz].toppoly(xtheta,ytheta);if (br[nx][ny][nz].BF[2]<1){g.setColor(br[nx][ny][nz].tric[2]);g.fillPolygon(br[nx][ny][nz].brickside[2]);}
                                                           ts[nx][ny][nz].xrot(xtheta,ytheta);ts[nx][ny][nz].plane();ts[nx][ny][nz].projection();for(sideno =0;sideno<5;sideno++){if(ts[nx][ny][nz].BF[sideno]<1){g.setColor(ts[nx][ny][nz].tric[sideno]);g.fillPolygon(ts[nx][ny][nz].brickside[sideno]);}}break;

                                        case 25 :  br[nx][ny][nz].toppoly(xtheta,ytheta);if (br[nx][ny][nz].BF[2]<1){g.setColor(br[nx][ny][nz].tric[2]);g.fillPolygon(br[nx][ny][nz].brickside[2]);}
                                                           ts[nx][ny][nz].xrot(xtheta,ytheta);ts[nx][ny][nz].plane();ts[nx][ny][nz].projection();for(sideno =0;sideno<5;sideno++){if(ts[nx][ny][nz].BF[sideno]<1){g.setColor(ts[nx][ny][nz].tric[sideno]);g.fillPolygon(ts[nx][ny][nz].brickside[sideno]);}}break;


                                        case 26 :br[nx][ny][nz].frontpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[4]<1){g.setColor(br[nx][ny][nz].tric[4]);g.fillPolygon(br[nx][ny][nz].brickside[4]);}
                                                          br[nx][ny][nz].backpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[3]<1){g.setColor(br[nx][ny][nz].tric[3]);g.fillPolygon(br[nx][ny][nz].brickside[3]);}
                                                          ts[nx][ny][nz].xrot(xtheta,ytheta);ts[nx][ny][nz].plane();ts[nx][ny][nz].projection();for(sideno =0;sideno<5;sideno++){if(ts[nx][ny][nz].BF[sideno]<1){g.setColor(ts[nx][ny][nz].tric[sideno]);g.fillPolygon(ts[nx][ny][nz].brickside[sideno]);}}break;
                                        case 27 : br[nx][ny][nz].leftpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[0]<1){g.setColor(br[nx][ny][nz].tric[0]);g.fillPolygon(br[nx][ny][nz].brickside[0]);}
                                                         br[nx][ny][nz].rightpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[1]<1){g.setColor(br[nx][ny][nz].tric[1]);g.fillPolygon(br[nx][ny][nz].brickside[1]);}
                                                           ts[nx][ny][nz].xrot(xtheta,ytheta);ts[nx][ny][nz].plane();ts[nx][ny][nz].projection();for(sideno =0;sideno<5;sideno++){if(ts[nx][ny][nz].BF[sideno]<1){g.setColor(ts[nx][ny][nz].tric[sideno]);g.fillPolygon(ts[nx][ny][nz].brickside[sideno]);}}break;
                                        case 28 :br[nx][ny][nz].frontpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[4]<1){g.setColor(br[nx][ny][nz].tric[4]);g.fillPolygon(br[nx][ny][nz].brickside[4]);}
                                                          br[nx][ny][nz].leftpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[0]<1){g.setColor(br[nx][ny][nz].tric[0]);g.fillPolygon(br[nx][ny][nz].brickside[0]);}
                                                         br[nx][ny][nz].rightpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[1]<1){g.setColor(br[nx][ny][nz].tric[1]);g.fillPolygon(br[nx][ny][nz].brickside[1]);}
                                                         br[nx][ny][nz].backpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[3]<1){g.setColor(br[nx][ny][nz].tric[3]);g.fillPolygon(br[nx][ny][nz].brickside[3]);}
                                                          ts[nx][ny][nz].xrot(xtheta,ytheta);ts[nx][ny][nz].plane();ts[nx][ny][nz].projection();for(sideno =0;sideno<5;sideno++){if(ts[nx][ny][nz].BF[sideno]<1){g.setColor(ts[nx][ny][nz].tric[sideno]);g.fillPolygon(ts[nx][ny][nz].brickside[sideno]);}}break;
                                        case 29 :br[nx][ny][nz].leftpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[0]<1){g.setColor(br[nx][ny][nz].tric[0]);g.fillPolygon(br[nx][ny][nz].brickside[0]);}
                                                         br[nx][ny][nz].rightpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[1]<1){g.setColor(br[nx][ny][nz].tric[1]);g.fillPolygon(br[nx][ny][nz].brickside[1]);}
                                                         br[nx][ny][nz].backpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[3]<1){g.setColor(br[nx][ny][nz].tric[3]);g.fillPolygon(br[nx][ny][nz].brickside[3]);}
                                                           ts[nx][ny][nz].xrot(xtheta,ytheta);ts[nx][ny][nz].plane();ts[nx][ny][nz].projection();for(sideno =0;sideno<5;sideno++){if(ts[nx][ny][nz].BF[sideno]<1){g.setColor(ts[nx][ny][nz].tric[sideno]);g.fillPolygon(ts[nx][ny][nz].brickside[sideno]);}}break;
                                        case 30 :br[nx][ny][nz].frontpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[4]<1){g.setColor(br[nx][ny][nz].tric[4]);g.fillPolygon(br[nx][ny][nz].brickside[4]);}
                                                         br[nx][ny][nz].rightpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[1]<1){g.setColor(br[nx][ny][nz].tric[1]);g.fillPolygon(br[nx][ny][nz].brickside[1]);}
                                                         br[nx][ny][nz].backpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[3]<1){g.setColor(br[nx][ny][nz].tric[3]);g.fillPolygon(br[nx][ny][nz].brickside[3]);}
                                                         ts[nx][ny][nz].xrot(xtheta,ytheta);ts[nx][ny][nz].plane();ts[nx][ny][nz].projection();for(sideno =0;sideno<5;sideno++){if(ts[nx][ny][nz].BF[sideno]<1){g.setColor(ts[nx][ny][nz].tric[sideno]);g.fillPolygon(ts[nx][ny][nz].brickside[sideno]);}}break;
                                        case 31 :br[nx][ny][nz].frontpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[4]<1){g.setColor(br[nx][ny][nz].tric[4]);g.fillPolygon(br[nx][ny][nz].brickside[4]);}
                                                          br[nx][ny][nz].leftpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[0]<1){g.setColor(br[nx][ny][nz].tric[0]);g.fillPolygon(br[nx][ny][nz].brickside[0]);}
                                                         br[nx][ny][nz].rightpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[1]<1){g.setColor(br[nx][ny][nz].tric[1]);g.fillPolygon(br[nx][ny][nz].brickside[1]);}
                                                           ts[nx][ny][nz].xrot(xtheta,ytheta);ts[nx][ny][nz].plane();ts[nx][ny][nz].projection();for(sideno =0;sideno<5;sideno++){if(ts[nx][ny][nz].BF[sideno]<1){g.setColor(ts[nx][ny][nz].tric[sideno]);g.fillPolygon(ts[nx][ny][nz].brickside[sideno]);}}break;
                                        case 32 :br[nx][ny][nz].frontpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[4]<1){g.setColor(br[nx][ny][nz].tric[4]);g.fillPolygon(br[nx][ny][nz].brickside[4]);}
                                                          br[nx][ny][nz].leftpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[0]<1){g.setColor(br[nx][ny][nz].tric[0]);g.fillPolygon(br[nx][ny][nz].brickside[0]);}
                                                         br[nx][ny][nz].backpoly(xtheta,ytheta);if (br[nx][ny][nz].BF[3]<1){g.setColor(br[nx][ny][nz].tric[3]);g.fillPolygon(br[nx][ny][nz].brickside[3]);}
                                                           ts[nx][ny][nz].xrot(xtheta,ytheta);ts[nx][ny][nz].plane();ts[nx][ny][nz].projection();for(sideno =0;sideno<5;sideno++){if(ts[nx][ny][nz].BF[sideno]<1){g.setColor(ts[nx][ny][nz].tric[sideno]);g.fillPolygon(ts[nx][ny][nz].brickside[sideno]);}}break;


                                }
                        }


                g.setColor(Color.black);
                g.drawString(NextMessage,247,67); // y coordinate changed from 62 to 65 by J.A.W.



                if (newbrickon)
                        {
                         g.setColor(Color.magenta);
                         g.fillPolygon(ts[hx][hy][hz].brickside[2]);
                         g.fillPolygon(ts[nbhx][nbhy][nbhz].brickside[2]);



                         g.setColor(Color.black);
                         g.setFont(Danbury);
                         if(noroom) g.drawString(NoroomMessage,10,68);

                        }


        }

        public boolean mouseDown(Event evt, int x, int y)
        {
           for(ny=0;ny<maxht;ny++)
                for (nx=0;nx<25;nx++)
                   for (nz=0;nz<22;nz++)
                        {if(barry[nx][ny][nz]>8)
                        if(ts[nx][ny][nz].brickside[2].inside(x,y)){ hx=nx;hy=ny;hz=nz;};}

                for(nz=newbz;nz<newbz+9;nz++)
                for (nx=newbx;nx<newbx+9;nx++)
                   {if(barry[nx][newby][nz]>8)
                        if(ts[nx][newby][nz].brickside[2].inside(x,y)){ nbhx=nx;nbhy=newby;nbhz=nz;};}

         noroom = false;
         repaint();
         return true;
        }

        public boolean keyDown(Event evt,int key)
        {
          //if((char)key == 'z' )
          //     ztheta = 2;

          //else if((char)key == 'a')
        //       ztheta = -2;

          if(key == Event.UP )
                  {if(xtheta+4<91)
                  xtheta += 4;}

          else if(key == Event.DOWN)
                  {if(xtheta-4>-1)
                  xtheta += -4;}

          else if(key == Event.RIGHT )
                {ytheta += 4;
                 if(ytheta>359)ytheta-=360;}
          else if(key == Event.LEFT)
                {ytheta += -4;
                 if(ytheta<0)ytheta+=360;}

          else if((char)key == ' ')     //rotate brick2
                  turnbrick();

          else if((char)key == 'p')      //     drop new brick2 on to model
                 dropbrick();

          // else if((char)key == 't')   //     drop new brick2 on to model
          // {
        //       for(ny=0;ny<8;ny++)
        //        for (nx=0;nx<25;nx++)
         //        for (nz=0;nz<25;nz++)
        //              {if(barry[nx][ny][nz]>0)
        //                br[nx][ny][nz].tz[8]+=1;
         //               ts[nx][ny][nz].tz[8]+=1;}

        //       }


         repaint();
         return true;
        }

        public boolean action(Event evt,Object arg)
        {
        if (evt.target instanceof Button)

                {String text = (String)arg;
                        if(text.equals("New brick"))
                            if(newbrickon) return true;
                                else
                                {
                                newbrickon = true;
                                brlength= Integer.parseInt(Blengthlist.getSelectedItem());
                                brwidth= Integer.parseInt(Bwidthlist.getSelectedItem());
                                switch(Colourlist.getSelectedIndex()+1)
                                        {
                                                case 1: rot=255;grun=0;blau=0;break;
                                                case 2: rot=255;grun=255;blau=255;break;
                                                case 3: rot=255;grun=255;blau=0;break;
                                                case 4: rot=0;grun=255;blau=0;break;
                                                case 5: rot=0;grun=0;blau=255;break;
                                                case 6: rot=20;grun=20;blau=20;break;
                                        }

                                        for(nz=newbz;nz<newbz+9;nz++)
                                                for (nx=newbx;nx<newbx+9;nx++)
                                        {
                                         br[nx][newby][nz].rd=rot;ts[nx][newby][nz].rd=rot;
                                         br[nx][newby][nz].gn=grun;ts[nx][newby][nz].gn=grun;
                                         br[nx][newby][nz].bl=blau;ts[nx][newby][nz].bl=blau;
                                         }

                                         brxlen=brlength;brzlen=brwidth;
                                         switch(brwidth)
                                         {
                                         case 1 :       if(brlength==1){barry[newbx][newby][newbz]=24;break;}
                                                                barry[newbx][newby][newbz]=23;barry[newbx+brlength-1][newby][newbz]=21;
                                                                for (nx=1;nx<brlength-1;nx++)
                                                                   barry[newbx+nx][newby][newbz]=17;
                                                                break;

                                         case 2 :       if(brlength==1){barry[newbx][newby][newbz]=22;barry[newbx][newby][newbz+1]=20;break;}
                                                                barry[newbx][newby][newbz]=16;barry[newbx][newby][newbz+1]=13;
                                                                barry[newbx+brlength-1][newby][newbz]=15;barry[newbx+brlength-1][newby][newbz+1]=14;
                                                                for (nx=1;nx<brlength-1;nx++)
                                                                        {barry[newbx+nx][newby][newbz]=12;
                                                                         barry[newbx+nx][newby][newbz+1]=11;}
                                                                break;
                                         }

                                xtheta=40;ytheta=40;

                                repaint();
                                }
                 }
         return true;
        }





        public boolean keyUp(Event e,int ky)
          {
                 //ztheta = 0;xtheta = 0;ytheta = 0;
                  return true;
          }

        public boolean mouseUp(Event evt, int x, int y)
           {//{xtheta =0;ytheta =0;
                return true;}


        public void dropbrick()
           {if(newbrickon)
            {

                //find coords of bottom left of newbrick on model
                  px=hx-(nbhx-newbx);py=hy+1;pz=hz-(nbhz-newbz);
                //check barry for space
                         if (hy>maxht-2)noroom=true;

                         for (nx=0;nx<brxlen;nx++)
                           for(nz=0;nz<brzlen;nz++)
                                if(barry[px+nx][py][pz+nz]>0)noroom=true;

                  if(noroom==false)
                  {

                        newbrickon=false;

                          for (nx=0;nx<brxlen;nx++)
                           for(nz=0;nz<brzlen;nz++)
                                {barry[px+nx][py][pz+nz]=barry[newbx+nx][nbhy][newbz+nz];
                                 br[px+nx][py][pz+nz] = new brick2(rot,grun,blau,-20+((px+(nx-basex))*5),23-(py*7),-15+((pz+(nz-basez))*5),-20+((px+(nx-basex))*5),23-(py*7),-20+((pz+(nz-basez))*5),-20+((px+(nx-basex))*5),30-(py*7),-20+((pz+(nz-basez))*5),-20+((px+(nx-basex))*5),30-(py*7),-15+((pz+(nz-basez))*5),-15+((px+(nx-basex))*5),23-(py*7),-15+((pz+(nz-basez))*5),-15+((px+(nx-basex))*5),23-(py*7),-20+((pz+(nz-basez))*5),-15+((px+(nx-basex))*5),30-(py*7),-20+((pz+(nz-basez))*5),-15+((px+(nx-basex))*5),30-(py*7),-15+((pz+(nz-basez))*5),0,0,100,1);                                                                  // x         y   z                    x         y   z                       x         y    z                    x      y    z                   x         y    z                  x         y   z                    x        y   z
                                 ts[px+nx][py][pz+nz] = new brick2(rot,grun,blau,-19+((px+(nx-basex))*5),21-(py*7),-16+((pz+(nz-basez))*5),-19+((px+(nx-basex))*5),21-(py*7),-19+((pz+(nz-basez))*5),-19+((px+(nx-basex))*5),23-(py*7),-19+((pz+(nz-basez))*5),-19+((px+(nx-basex))*5),23-(py*7),-16+((pz+(nz-basez))*5),-16+((px+(nx-basex))*5),21-(py*7),-16+((pz+(nz-basez))*5),-16+((px+(nx-basex))*5),21-(py*7),-19+((pz+(nz-basez))*5),-16+((px+(nx-basex))*5),23-(py*7),-19+((pz+(nz-basez))*5),-16+((px+(nx-basex))*5),23-(py*7),-16+((pz+(nz-basez))*5),0,0,100,1);}
                          topoff();
                          for (nz=0;nz<brzlen;nz++)
                                leftoff(px+brxlen,py,pz+nz);
                          for (nz=0;nz<brzlen;nz++)
                                rightoff(px-1,py,pz+nz);
                          for (nx=0;nx<brxlen;nx++)
                                backoff(px+nx,py,pz-1);
                          for (nx=0;nx<brxlen;nx++)
                                frontoff(px+nx,py,pz+brzlen);

                          //backoff();
                          //clear newbrick
                        for(nz=newbz;nz<newbz+9;nz++)
                                for (nx=newbx;nx<newbx+9;nx++)
                                   barry[nx][newby][nz]=0;
                        nbhx=newbx;nbhz=newbz;
                        hy=hy+1;
                   }



                  }

                }
        public void turnbrick()
                {if(newbrickon)
                        {
                           if(rotated)
                           {
                                   rotated=false;
                                   temp = nbhx-newbx;nbhx=newbx+(nbhz-newbz);nbhz=newbz+temp;
                                   brxlen=brlength;brzlen=brwidth;
                                   for(nz=newbz;nz<newbz+9;nz++)
                                        for (nx=newbx;nx<newbx+9;nx++)
                                          barry[nx][newby][nz]=0;
                                   switch(brwidth)
                                         {
                                         case 1 :       if(brlength==1){barry[newbx][newby][newbz]=24;break;}
                                                                barry[newbx][newby][newbz]=23;barry[newbx+brlength-1][newby][newbz]=21;
                                                                for (nx=1;nx<brlength-1;nx++)
                                                                   barry[newbx+nx][newby][newbz]=17;
                                                                break;

                                         case 2 :       if(brlength==1){barry[newbx][newby][newbz]=22;barry[newbx][newby][newbz+1]=20;break;}
                                                                barry[newbx][newby][newbz]=16;barry[newbx][newby][newbz+1]=13;
                                                                barry[newbx+brlength-1][newby][newbz]=15;barry[newbx+brlength-1][newby][newbz+1]=14;
                                                                for (nx=1;nx<brlength-1;nx++)
                                                                        {barry[newbx+nx][newby][newbz]=12;
                                                                         barry[newbx+nx][newby][newbz+1]=11;}
                                                                break;
                                         }
                            }
                                 else
                            {
                                   rotated=true;
                                   temp = nbhx-newbx;nbhx=newbx+(nbhz-newbz);nbhz=newbz+temp;
                                        brxlen=brwidth;brzlen=brlength;
                                   for(nz=newbz;nz<newbz+9;nz++)
                                        for (nx=newbx;nx<newbx+9;nx++)
                                          barry[nx][newby][nz]=0;

                                        switch(brwidth)
                                         {
                                         case 1 :       if(brlength==1){barry[newbx][newby][newbz]=24;break;}
                                                                barry[newbx][newby][newbz]=22;barry[newbx][newby][newbz+brlength-1]=20;
                                                                for (nz=1;nz<brlength-1;nz++)
                                                                   barry[newbx][newby][newbz+nz]=18;
                                                                break;

                                         case 2 :       if(brlength==1){barry[newbx][newby][newbz]=23;barry[newbx+1][newby][newbz]=21;break;}
                                                                barry[newbx][newby][newbz]=16;barry[newbx+1][newby][newbz]=15;
                                                                barry[newbx][newby][newbz+brlength-1]=13;barry[newbx+1][newby][newbz+brlength-1]=14;
                                                                for (nz=1;nz<brlength-1;nz++)
                                                                        {barry[newbx][newby][newbz+nz]=9;
                                                                         barry[newbx+1][newby][newbz+nz]=10;}
                                                                break;
                                         }
                                 }

                        }

                }

  public void topoff()
  {
        for (nx=0;nx<brxlen;nx++)
                for(nz=0;nz<brzlen;nz++)
                        {
                         switch (barry[px+nx][py-1][pz+nz])
                         {
                           case 0 :topnum = 0;break;
                           case 9 :topnum = 1;break;
                           case 10:topnum = 2;break;
                           case 11:topnum = 3;break;
                           case 12:topnum = 4;break;
                           case 13:topnum = 5;break;
                           case 14:topnum = 6;break;
                           case 15:topnum = 7;break;
                           case 16:topnum = 8;break;
                           case 17:topnum = 26;break;
                           case 18:topnum = 27;break;
                           case 20:topnum = 29;break;
                           case 21:topnum = 30;break;
                           case 22:topnum = 31;break;
                           case 23:topnum = 32;break;
                           case 24:topnum = 28;break;
                           case 25:topnum = 0;break;

                         }
                         barry[px+nx][py-1][pz+nz]=topnum;

                        }


  }

 public void leftoff(int bx,int by,int bz)
  {
         switch (barry[bx][by][bz])
                         {
                           case 0 :topnum = 0;break;
                           case 1 :topnum = 0;break;
                           case 5 :topnum = 3;break;
                           case 8 :topnum = 4;break;
                           case 9 :topnum = 25;break;
                           case 13:topnum = 11;break;
                           case 16:topnum = 12;break;
                           case 18:topnum = 10;break;
                           case 20:topnum = 14;break;
                           case 22:topnum = 15;break;
                           case 23:topnum = 17;break;
                           case 24:topnum = 21;break;
                           case 27:topnum = 2;break;
                           case 28:topnum = 30;break;
                           case 29:topnum = 6;break;
                           case 31:topnum = 7;break;
                           case 32:topnum = 26;break;

                         }
                         barry[bx][by][bz]=topnum;
        }

  public void rightoff(int bx,int by,int bz)
  {
         switch (barry[bx][by][bz])
                         {
                           case 0 :topnum = 0;break;
                           case 2 :topnum = 0;break;
                           case 6 :topnum = 3;break;
                           case 7 :topnum = 4;break;
                           case 10 :topnum = 25;break;
                           case 14:topnum = 11;break;
                           case 15:topnum = 12;break;
                           case 18:topnum = 9;break;
                           case 20:topnum = 13;break;
                           case 22:topnum = 16;break;
                           case 21:topnum = 17;break;
                           case 24:topnum = 23;break;
                           case 27:topnum = 1;break;
                           case 28:topnum = 32;break;
                           case 29:topnum = 5;break;
                           case 31:topnum = 8;break;
                           case 30:topnum = 26;break;

                         }
                         barry[bx][by][bz]=topnum;
        }

public void frontoff(int bx,int by,int bz)
  {
         switch (barry[bx][by][bz])
                         {
                           case 0 :topnum = 0;break;
                           case 4 :topnum = 0;break;
                           case 7 :topnum = 2;break;
                           case 8 :topnum = 1;break;
                           case 12:topnum = 25;break;
                           case 15:topnum = 10;break;
                           case 16:topnum = 9;break;
                           case 17:topnum = 11;break;
                           case 21:topnum = 14;break;
                           case 22:topnum = 18;break;
                           case 23:topnum = 13;break;
                           case 24:topnum = 20;break;
                           case 26:topnum = 3;break;
                           case 28:topnum = 29;break;
                           case 30:topnum = 6;break;
                           case 31:topnum = 27;break;
                           case 32:topnum = 5;break;

                         }
                         barry[bx][by][bz]=topnum;
        }

public void backoff(int bx,int by,int bz)
  {
         switch (barry[bx][by][bz])
                         {
                           case 0 :topnum = 0;break;
                           case 3 :topnum = 0;break;
                           case 5 :topnum = 1;break;
                           case 6 :topnum = 2;break;
                           case 11:topnum = 25;break;
                           case 13:topnum = 9;break;
                           case 14:topnum = 10;break;
                           case 17:topnum = 12;break;
                           case 20:topnum = 18;break;
                           case 21:topnum = 15;break;
                           case 23:topnum = 16;break;
                           case 24:topnum = 22;break;
                           case 26:topnum = 4;break;
                           case 28:topnum = 31;break;
                           case 29:topnum = 27;break;
                           case 30:topnum = 7;break;
                           case 32:topnum = 8;break;

                         }
                         barry[bx][by][bz]=topnum;
        }

}





// following line commented out by J.A.W.
// causes complilation error - why is it there?

// import java.awt.*;


class brick2
{
        int n,nx,ny,nz;
        double xt,yt,zt;
        public float litex=10000,litey=-10000,litez=0;//lightsource coords
        public float  tx[] = new float[18];
        public float  ty[] = new float[18];
        public float  tz[] = new float[18];
        protected int xvxendL[] = new int[4];
        protected int yvxendL[] = new int[4];
        protected int xvxendR[] = new int[4];
        protected int yvxendR[] = new int[4];
        protected int xvxtop[] = new int[4];
        protected int yvxtop[] = new int[4];
        protected int xvxbottom[] = new int[4];
        protected int yvxbottom[] = new int[4];
        protected int xvxfront[] = new int[4];
        protected int yvxfront[] = new int[4];
        protected int xvxback[] = new int[4];
        protected int yvxback[] = new int[4];
        public int blen;
        public int orientation;
        protected float Pax[] = new float[6];
        protected float Pay[] = new float[6];
        protected float Paz[] = new float[6];
        protected float Pbx[] = new float[6];
        protected float Pby[] = new float[6];
        protected float Pbz[] = new float[6];

        protected double PVx[] = new double[6];
        protected double PVy[] = new double[6];
        protected double PVz[] = new double[6];
        protected double PVM[] = new double[6];
        protected double LVM[] = new double[6];
        protected float  vx[] = new float[6];//used for plane constant calc
        protected float  vy[] = new float[6];
        protected float  vz[] = new float[6];

        protected float PVxNorm[] = new float[6];
        protected float PVyNorm[] = new float[6];
        protected float PVzNorm[] = new float[6];

        protected float NV[] = new float[6];
        protected float LV[] = new float[6];
        protected float planeconst[] = new float[6];
        protected float BF[] = new float[6];
        protected float LF[] = new float[6];
        protected int Override[] = new int[6];
        double pi = 3.14159265;
        protected int rd,gn,bl,tricR,tricG,tricB,redconst,greenconst,blueconst;
        Polygon pgon = new Polygon();
        Polygon brickside[]     = new Polygon[6];
        Polygon topsquares[] = new Polygon[8];
        Color tric[] = new Color[6];


        brick2 (){}

        brick2(int rd1,int gn1,int bl1,float ax1,float ay1,float az1,float bx1,float by1,float bz1,float cx1,float cy1,float cz1,float dx1,float dy1,float dz1,float ax2,float ay2,float az2,float bx2,float by2,float bz2,float cx2,float cy2,float cz2,float dx2,float dy2,float dz2, float ox1, float oy1,float oz1,int bricklength)
        {
        rd=rd1;gn=gn1;bl=bl1;
        tx[9+0]=ax1;ty[9+0]=ay1;tz[9+0]=az1;tx[9+1]=bx1;ty[9+1]=by1;tz[9+1]=bz1;tx[9+2]=cx1;ty[9+2]=cy1;tz[9+2]=cz1;tx[9+3]=dx1;ty[9+3]=dy1;tz[9+3]=dz1;tx[9+8]=ox1;ty[9+8]=oy1;tz[9+8]=oz1;
        tx[9+4]=ax2;ty[9+4]=ay2;tz[9+4]=az2;tx[9+5]=bx2;ty[9+5]=by2;tz[9+5]=bz2;tx[9+6]=cx2;ty[9+6]=cy2;tz[9+6]=cz2;tx[9+7]=dx2;ty[9+7]=dy2;tz[9+7]=dz2;
        tx[0]=ax1;ty[0]=ay1;tz[0]=az1;tx[1]=bx1;ty[1]=by1;tz[1]=bz1;tx[2]=cx1;ty[2]=cy1;tz[2]=cz1;tx[3]=dx1;ty[3]=dy1;tz[3]=dz1;tx[8]=ox1;ty[8]=oy1;tz[8]=oz1;
        tx[4]=ax2;ty[4]=ay2;tz[4]=az2;tx[5]=bx2;ty[5]=by2;tz[5]=bz2;tx[6]=cx2;ty[6]=cy2;tz[6]=cz2;tx[7]=dx2;ty[7]=dy2;tz[7]=dz2;
        blen = bricklength;

        }



void projection()
        {
        xvxendL[0] = (int)(((tx[0]+tx[8])*340/(tz[0]+tz[8]))+160); xvxendL[1]= (int)(((tx[1]+tx[8])*340/(tz[1]+tz[8]))+160); xvxendL[2] = (int)(((tx[2]+tx[8])*340/(tz[2]+tz[8]))+160);xvxendL[3] = (int)(((tx[3]+tx[8])*340/(tz[3]+tz[8]))+160);
        xvxendR[0] = (int)(((tx[4]+tx[8])*340/(tz[4]+tz[8]))+160); xvxendR[1]= (int)(((tx[5]+tx[8])*340/(tz[5]+tz[8]))+160); xvxendR[2] = (int)(((tx[6]+tx[8])*340/(tz[6]+tz[8]))+160);xvxendR[3] = (int)(((tx[7]+tx[8])*340/(tz[7]+tz[8]))+160);
        yvxendL[0] = (int)(((ty[0]+ty[8])*340/(tz[0]+tz[8]))+160); yvxendL[1]= (int)(((ty[1]+ty[8])*340/(tz[1]+tz[8]))+160); yvxendL[2] = (int)(((ty[2]+ty[8])*340/(tz[2]+tz[8]))+160);yvxendL[3] = (int)(((ty[3]+ty[8])*340/(tz[3]+tz[8]))+160);
        yvxendR[0] = (int)(((ty[4]+ty[8])*340/(tz[4]+tz[8]))+160); yvxendR[1]= (int)(((ty[5]+ty[8])*340/(tz[5]+tz[8]))+160); yvxendR[2] = (int)(((ty[6]+ty[8])*340/(tz[6]+tz[8]))+160);yvxendR[3] = (int)(((ty[7]+ty[8])*340/(tz[7]+tz[8]))+160);
        xvxtop[0] = xvxback[0] = xvxendL[0];xvxtop[1] = xvxfront[0] = xvxendL[1];xvxtop[2] = xvxfront[1] = xvxendR[1];xvxtop[3] = xvxback[3] = xvxendR[0];
        yvxtop[0] = yvxback[0] = yvxendL[0];yvxtop[1] = yvxfront[0] = yvxendL[1];yvxtop[2] = yvxfront[1] = yvxendR[1];yvxtop[3] = yvxback[3] = yvxendR[0];
        xvxfront[3] = xvxendL[2];xvxback[1] = xvxendL[3];xvxback[2] = xvxendR[3];xvxfront[2] = xvxendR[2];
        yvxfront[3] = yvxendL[2];yvxback[1] = yvxendL[3];yvxback[2] = yvxendR[3];yvxfront[2] = yvxendR[2];


        brickside[0] = new Polygon(xvxendL,yvxendL,4);
        brickside[1] = new Polygon(xvxendR,yvxendR,4);
        brickside[2] = new Polygon(xvxtop,yvxtop,4);
        brickside[3] = new Polygon(xvxback,yvxback,4);
        brickside[4] = new Polygon(xvxfront,yvxfront,4);
        }

void xrot(double xtheta,double ytheta)
        {
        yt =ytheta*(pi/180);
        double ct = Math.cos(yt);
        double st = Math.sin(yt);
        float Nax = (float)(tx[9+0] * ct + (tz[9+0] * st));
        float  Naz = (float)(-(tx[9+0] * st) + (tz[9+0] * ct));
        float Nbx = (float)(tx[9+1] * ct + (tz[9+1] * st));
        float Nbz = (float)(-(tx[9+1] * st) + (tz[9+1] * ct));
        float Ncx = (float)(tx[9+2] * ct + (tz[9+2] * st));
        float Ncz = (float)(-(tx[9+2] * st)+ (tz[9+2] * ct));
        float Ndx = (float)(tx[9+3] * ct + (tz[9+3] * st));
        float Ndz = (float)(-(tx[9+3] * st)+ (tz[9+3] * ct));
        float Nax2 = (float)(tx[9+4] * ct + (tz[9+4] * st));
        float Naz2 = (float)(-(tx[9+4] * st) + (tz[9+4] * ct));
        float Nbx2 = (float)(tx[9+5] * ct + (tz[9+5] * st));
        float Nbz2 = (float)(-(tx[9+5] * st) + (tz[9+5] * ct));
        float Ncx2 = (float)(tx[9+6] * ct + (tz[9+6] * st));
        float Ncz2 = (float)(-(tx[9+6] * st)+ (tz[9+6] * ct));
        float Ndx2 = (float)(tx[9+7] * ct + (tz[9+7] * st));
        float Ndz2 = (float)(-(tx[9+7] * st)+ (tz[9+7] * ct));

        tx[0] = Nax; tz[0] = Naz;
        tx[1] = Nbx; tz[1] = Nbz;
        tx[2] = Ncx; tz[2] = Ncz;
        tx[3] = Ndx; tz[3] = Ndz;
        tx[4] = Nax2; tz[4] = Naz2;
        tx[5] = Nbx2; tz[5] = Nbz2;
        tx[6] = Ncx2; tz[6] = Ncz2;
        tx[7] = Ndx2; tz[7] = Ndz2;


        xt = xtheta*(pi/180);
        ct = Math.cos(xt);
        st = Math.sin(xt);
        float Nay = (float)(ty[9+0] * ct - (tz[0] * st));
         Naz = (float)(ty[9+0] * st + (tz[0] * ct));
        float Nby = (float)(ty[9+1] * ct - (tz[1] * st));
         Nbz = (float)(ty[9+1] * st + (tz[1] * ct));
        float Ncy = (float)(ty[9+2] * ct - (tz[2] * st));
         Ncz = (float)(ty[9+2] * st + (tz[2] * ct));
        float Ndy = (float)(ty[9+3] * ct - (tz[3] * st));
         Ndz = (float)(ty[9+3] * st + (tz[3] * ct));
        float Nay2 = (float)(ty[9+4] * ct - (tz[4] * st));
         Naz2 = (float)(ty[9+4] * st + (tz[4] * ct));
        float Nby2 = (float)(ty[9+5] * ct - (tz[5] * st));
         Nbz2 = (float)(ty[9+5] * st + (tz[5] * ct));
        float Ncy2 = (float)(ty[9+6] * ct - (tz[6] * st));
         Ncz2 = (float)(ty[9+6] * st + (tz[6] * ct));
        float Ndy2 = (float)(ty[9+7] * ct - (tz[7] * st));
         Ndz2 = (float)(ty[9+7] * st + (tz[7] * ct));



        ty[0] = Nay; tz[0] = Naz;
        ty[1] = Nby; tz[1] = Nbz;
        ty[2] = Ncy; tz[2] = Ncz;
        ty[3] = Ndy; tz[3] = Ndz;
        ty[4] = Nay2; tz[4] = Naz2;
        ty[5] = Nby2; tz[5] = Nbz2;
        ty[6] = Ncy2; tz[6] = Ncz2;
        ty[7] = Ndy2; tz[7] = Ndz2;



        }




 void plane ()
        {
         //compute plane vector PVx,PVy,PVz with a magnitude PVM
         Pax[0] = tx[1] - tx[0];Pbx[0] = tx[2] - tx[1];
         Pay[0] = ty[1] - ty[0];Pby[0] = ty[2] - ty[1];
         Paz[0] = tz[1] - tz[0];Pbz[0] = tz[2] - tz[1];//endL
         Pax[1] = tx[5] - tx[4];Pbx[1] = tx[5] - tx[6];
         Pay[1] = ty[5] - ty[4];Pby[1] = ty[5] - ty[6];
         Paz[1] = tz[5] - tz[4];Pbz[1] = tz[5] - tz[6];//endR
         Pax[2] = tx[5] - tx[1];Pbx[2] = tx[1] - tx[0];
         Pay[2] = ty[5] - ty[1];Pby[2] = ty[1] - ty[0];
         Paz[2] = tz[5] - tz[1];Pbz[2] = tz[1] - tz[0];//top
         Pax[3] = tx[3] - tx[0];Pbx[3] = tx[7] - tx[3];
         Pay[3] = ty[3] - ty[0];Pby[3] = ty[7] - ty[3];
         Paz[3] = tz[3] - tz[0];Pbz[3] = tz[7] - tz[3];//back
         Pax[4] = tx[2] - tx[1];Pbx[4] = tx[1] - tx[5];
         Pay[4] = ty[2] - ty[1];Pby[4] = ty[1] - ty[5];
         Paz[4] = tz[2] - tz[1];Pbz[4] = tz[1] - tz[5];//front

         vx[0]=tx[0]; vy[0]=ty[0]; vz[0]=tz[0];
         vx[1]=tx[4]; vy[1]=ty[4]; vz[1]=tz[4];
         vx[2]=tx[0]; vy[2]=ty[0]; vz[2]=tz[0];
         vx[3]=tx[0]; vy[3]=ty[0]; vz[3]=tz[0];
         vx[4]=tx[1]; vy[4]=ty[1]; vz[4]=tz[1];
         float CAMx = -tx[8];
         float CAMy = ty[8];
         float CAMz = -tz[8];
         if(rd<31)redconst=rd;else redconst =90;
         if(gn<31)greenconst=gn;else greenconst =90;
         if(bl<31)blueconst=bl;else blueconst = 90;

         for (n=0;n<5;n++)
         {
                PVx[n] = (double)(Pay[n]*Pbz[n])-(Paz[n]*Pby[n]);
                PVy[n] = (double)(Paz[n]*Pbx[n])-(Pax[n]*Pbz[n]);
                PVz[n] = (double)(Pax[n]*Pby[n])-(Pay[n]*Pbx[n]);
                PVM[n] = Math.sqrt(Math.pow(PVx[n],2)+Math.pow(PVy[n],2)+Math.pow(PVz[n],2));
                //normalize it
                PVxNorm[n] = (float)(PVx[n]/PVM[n]);
                PVyNorm[n] = (float)(PVy[n]/PVM[n]);
                PVzNorm[n] = (float)(PVz[n]/PVM[n]);
                //compute plane constant
                planeconst[n] = -((PVxNorm[n]*vx[n])+(PVyNorm[n]*vy[n])+(PVzNorm[n]*vz[n]));
                NV[n] = (PVxNorm[n] * CAMx)+(PVyNorm[n] * CAMy)+(PVzNorm[n] * CAMz);
                BF[n] = planeconst[n] + NV[n]+Override[n];
                //lightsource vector
                LV[n] = (PVxNorm[n] * litex)+(PVyNorm[n] * litey)+(PVzNorm[n] * litez);
                LVM[n] =  Math.sqrt(Math.pow((double)(litex),2)+Math.pow((double)(litey),2)+Math.pow((double)(litez),2));
                LF[n] = (((planeconst[n] - LV[n])/((float)(LVM[n])))+1)/2;//LF returns a value between 0(black) and 1(white).
                tricR = (int)(LF[n]*rd/2)+redconst;
                tricG = (int)(LF[n]*gn/2)+greenconst;
                tricB = (int)(LF[n]*bl/2)+blueconst;
                tric[n] = new Color(tricR,tricG,tricB);
         }
        }






        public boolean insidetop(int ix,int iy)
        {
         Polygon pgon = new Polygon(xvxtop,yvxtop,4);
         if (pgon.inside(ix,iy))
         {return true;}
         else{return false;}
        }


void xrotate(double yth)
        {
        yt =yth*(pi/180);
        double ct = Math.cos(yt);
        double st = Math.sin(yt);
        float Nax = (float)(tx[9+0] * ct + (tz[9+0] * st));
        float  Naz = (float)(-(tx[9+0] * st) + (tz[9+0] * ct));
        float Nbx = (float)(tx[9+1] * ct + (tz[9+1] * st));
        float Nbz = (float)(-(tx[9+1] * st) + (tz[9+1] * ct));
        float Ncx = (float)(tx[9+2] * ct + (tz[9+2] * st));
        float Ncz = (float)(-(tx[9+2] * st)+ (tz[9+2] * ct));
        float Ndx = (float)(tx[9+3] * ct + (tz[9+3] * st));
        float Ndz = (float)(-(tx[9+3] * st)+ (tz[9+3] * ct));
        float Nax2 = (float)(tx[9+4] * ct + (tz[9+4] * st));
        float Naz2 = (float)(-(tx[9+4] * st) + (tz[9+4] * ct));
        float Nbx2 = (float)(tx[9+5] * ct + (tz[9+5] * st));
        float Nbz2 = (float)(-(tx[9+5] * st) + (tz[9+5] * ct));
        float Ncx2 = (float)(tx[9+6] * ct + (tz[9+6] * st));
        float Ncz2 = (float)(-(tx[9+6] * st)+ (tz[9+6] * ct));
        float Ndx2 = (float)(tx[9+7] * ct + (tz[9+7] * st));
        float Ndz2 = (float)(-(tx[9+7] * st)+ (tz[9+7] * ct));

        tx[9+0] = Nbx; tz[9+0] = Nbz;
        tx[9+1] = Nbx2; tz[9+1] = Nbz2;
        tx[9+2] = Ncx2; tz[9+2] = Ncz2;
        tx[9+3] = Ncx; tz[9+3] = Ncz;
        tx[9+4] = Nax; tz[9+4] = Naz;
        tx[9+5] = Nax2; tz[9+5] = Naz2;
        tx[9+6] = Ndx2; tz[9+6] = Ndz2;
        tx[9+7] = Ndx; tz[9+7] = Ndz;
        orientation = orientation + 1;
        if (orientation>3) orientation = 0;
        }

 void move(float xm,float ym,float zm)
    {
         for (n=0;n<8;n++)
         {
          tx[9+n] += xm;
          ty[9+n] += ym;
          tz[9+n] += zm;

         }
         tx[8]=0;ty[8]=0;tz[8]=100;
         tx[9+8]=0;ty[9+8]=0;tz[9+8]=100;

        }


void toppoly(double xtheta,double ytheta)
        {
        yt =ytheta*(pi/180);
        double ct = Math.cos(yt);
        double st = Math.sin(yt);
        float Nax = (float)(tx[9+0] * ct + (tz[9+0] * st));
        float  Naz = (float)(-(tx[9+0] * st) + (tz[9+0] * ct));
        float Nbx = (float)(tx[9+1] * ct + (tz[9+1] * st));
        float Nbz = (float)(-(tx[9+1] * st) + (tz[9+1] * ct));
        float Nax2 = (float)(tx[9+4] * ct + (tz[9+4] * st));
        float Naz2 = (float)(-(tx[9+4] * st) + (tz[9+4] * ct));
        float Nbx2 = (float)(tx[9+5] * ct + (tz[9+5] * st));
        float Nbz2 = (float)(-(tx[9+5] * st) + (tz[9+5] * ct));
        tx[0] = Nax; tz[0] = Naz;
        tx[1] = Nbx; tz[1] = Nbz;
        tx[4] = Nax2; tz[4] = Naz2;
        tx[5] = Nbx2; tz[5] = Nbz2;
        xt = xtheta*(pi/180);
        ct = Math.cos(xt);
        st = Math.sin(xt);
        float Nay = (float)(ty[9+0] * ct - (tz[0] * st));
         Naz = (float)(ty[9+0] * st + (tz[0] * ct));
        float Nby = (float)(ty[9+1] * ct - (tz[1] * st));
         Nbz = (float)(ty[9+1] * st + (tz[1] * ct));
                float Nay2 = (float)(ty[9+4] * ct - (tz[4] * st));
         Naz2 = (float)(ty[9+4] * st + (tz[4] * ct));
        float Nby2 = (float)(ty[9+5] * ct - (tz[5] * st));
         Nbz2 = (float)(ty[9+5] * st + (tz[5] * ct));
         ty[0] = Nay; tz[0] = Naz;
         ty[1] = Nby; tz[1] = Nbz;
         ty[4] = Nay2; tz[4] = Naz2;
         ty[5] = Nby2; tz[5] = Nbz2;
         Pax[2] = tx[5] - tx[1];Pbx[2] = tx[1] - tx[0];
         Pay[2] = ty[5] - ty[1];Pby[2] = ty[1] - ty[0];
         Paz[2] = tz[5] - tz[1];Pbz[2] = tz[1] - tz[0];//top
         vx[2]=tx[0]; vy[2]=ty[0]; vz[2]=tz[0];
          float CAMx = -tx[8];
         float CAMy = ty[8];
         float CAMz = -tz[8];
         if(rd<31)redconst=rd;else redconst =90;
         if(gn<31)greenconst=gn;else greenconst =90;
         if(bl<31)blueconst=bl;else blueconst = 90;
            PVx[2] = (double)(Pay[2]*Pbz[2])-(Paz[2]*Pby[2]);
                PVy[2] = (double)(Paz[2]*Pbx[2])-(Pax[2]*Pbz[2]);
                PVz[2] = (double)(Pax[2]*Pby[2])-(Pay[2]*Pbx[2]);
                PVM[2] = Math.sqrt(Math.pow(PVx[2],2)+Math.pow(PVy[2],2)+Math.pow(PVz[2],2));
                //normalize it
                PVxNorm[2] = (float)(PVx[2]/PVM[2]);
                PVyNorm[2] = (float)(PVy[2]/PVM[2]);
                PVzNorm[2] = (float)(PVz[2]/PVM[2]);
                //compute plane constant
                planeconst[2] = -((PVxNorm[2]*vx[2])+(PVyNorm[2]*vy[2])+(PVzNorm[2]*vz[2]));
                NV[2] = (PVxNorm[2] * CAMx)+(PVyNorm[2] * CAMy)+(PVzNorm[2] * CAMz);
                BF[2] = planeconst[2] + NV[2]+Override[2];
                //lightsource vector
                LV[2] = (PVxNorm[2] * litex)+(PVyNorm[2] * litey)+(PVzNorm[2] * litez);
                LVM[2] =  Math.sqrt(Math.pow((double)(litex),2)+Math.pow((double)(litey),2)+Math.pow((double)(litez),2));
                LF[2] = (((planeconst[2] - LV[2])/((float)(LVM[2])))+1)/2;//LF returns a value between 0(black) and 1(white).
                tricR = (int)(LF[2]*rd/2)+redconst;
                tricG = (int)(LF[2]*gn/2)+greenconst;
                tricB = (int)(LF[2]*bl/2)+blueconst;
                tric[2] = new Color(tricR,tricG,tricB);
                xvxtop[0] = (int)(((tx[0]+tx[8])*340/(tz[0]+tz[8]))+160);
                xvxtop[1] =     (int)(((tx[1]+tx[8])*340/(tz[1]+tz[8]))+160);
                xvxtop[2] =     (int)(((tx[5]+tx[8])*340/(tz[5]+tz[8]))+160);
                xvxtop[3] =     (int)(((tx[4]+tx[8])*340/(tz[4]+tz[8]))+160);
                yvxtop[0] = (int)(((ty[0]+ty[8])*340/(tz[0]+tz[8]))+160);
                yvxtop[1] = (int)(((ty[1]+ty[8])*340/(tz[1]+tz[8]))+160);
                yvxtop[2] = (int)(((ty[5]+ty[8])*340/(tz[5]+tz[8]))+160);
                yvxtop[3] = (int)(((ty[4]+ty[8])*340/(tz[4]+tz[8]))+160);
                brickside[2] = new Polygon(xvxtop,yvxtop,4);

                }


void leftpoly(double xtheta,double ytheta)
        {
        yt =ytheta*(pi/180);
        double ct = Math.cos(yt);
        double st = Math.sin(yt);
        float Nax = (float)(tx[9+0] * ct + (tz[9+0] * st));
        float  Naz = (float)(-(tx[9+0] * st) + (tz[9+0] * ct));
        float Nbx = (float)(tx[9+1] * ct + (tz[9+1] * st));
        float Nbz = (float)(-(tx[9+1] * st) + (tz[9+1] * ct));
        float Ncx = (float)(tx[9+2] * ct + (tz[9+2] * st));
        float Ncz = (float)(-(tx[9+2] * st)+ (tz[9+2] * ct));
        float Ndx = (float)(tx[9+3] * ct + (tz[9+3] * st));
        float Ndz = (float)(-(tx[9+3] * st)+ (tz[9+3] * ct));
        tx[0] = Nax; tz[0] = Naz;
        tx[1] = Nbx; tz[1] = Nbz;
        tx[2] = Ncx; tz[2] = Ncz;
        tx[3] = Ndx; tz[3] = Ndz;
        xt = xtheta*(pi/180);
        ct = Math.cos(xt);
        st = Math.sin(xt);
        float Nay = (float)(ty[9+0] * ct - (tz[0] * st));
         Naz = (float)(ty[9+0] * st + (tz[0] * ct));
        float Nby = (float)(ty[9+1] * ct - (tz[1] * st));
         Nbz = (float)(ty[9+1] * st + (tz[1] * ct));
        float Ncy = (float)(ty[9+2] * ct - (tz[2] * st));
         Ncz = (float)(ty[9+2] * st + (tz[2] * ct));
        float Ndy = (float)(ty[9+3] * ct - (tz[3] * st));
         Ndz = (float)(ty[9+3] * st + (tz[3] * ct));
        ty[0] = Nay; tz[0] = Naz;
        ty[1] = Nby; tz[1] = Nbz;
        ty[2] = Ncy; tz[2] = Ncz;
        ty[3] = Ndy; tz[3] = Ndz;
        Pax[0] = tx[1] - tx[0];Pbx[0] = tx[2] - tx[1];
        Pay[0] = ty[1] - ty[0];Pby[0] = ty[2] - ty[1];
        Paz[0] = tz[1] - tz[0];Pbz[0] = tz[2] - tz[1];//endL
        vx[0]=tx[0]; vy[0]=ty[0]; vz[0]=tz[0];
        float CAMx = -tx[8];
         float CAMy = ty[8];
         float CAMz = -tz[8];
         if(rd<31)redconst=rd;else redconst =90;
         if(gn<31)greenconst=gn;else greenconst =90;
         if(bl<31)blueconst=bl;else blueconst = 90;
                PVx[0] = (double)(Pay[0]*Pbz[0])-(Paz[0]*Pby[0]);
                PVy[0] = (double)(Paz[0]*Pbx[0])-(Pax[0]*Pbz[0]);
                PVz[0] = (double)(Pax[0]*Pby[0])-(Pay[0]*Pbx[0]);
                PVM[0] = Math.sqrt(Math.pow(PVx[0],2)+Math.pow(PVy[0],2)+Math.pow(PVz[0],2));
                //normalize it
                PVxNorm[0] = (float)(PVx[0]/PVM[0]);
                PVyNorm[0] = (float)(PVy[0]/PVM[0]);
                PVzNorm[0] = (float)(PVz[0]/PVM[0]);
                //compute plane constant
                planeconst[0] = -((PVxNorm[0]*vx[0])+(PVyNorm[0]*vy[0])+(PVzNorm[0]*vz[0]));
                NV[0] = (PVxNorm[0] * CAMx)+(PVyNorm[0] * CAMy)+(PVzNorm[0] * CAMz);
                BF[0] = planeconst[0] + NV[0]+Override[0];
                //lightsource vector
                LV[0] = (PVxNorm[0] * litex)+(PVyNorm[0] * litey)+(PVzNorm[0] * litez);
                LVM[0] =  Math.sqrt(Math.pow((double)(litex),2)+Math.pow((double)(litey),2)+Math.pow((double)(litez),2));
                LF[0] = (((planeconst[0] - LV[0])/((float)(LVM[0])))+1)/2;//LF returns a value between 0(black) and 1(white).
                tricR = (int)(LF[0]*rd/2)+redconst;
                tricG = (int)(LF[0]*gn/2)+greenconst;
                tricB = (int)(LF[0]*bl/2)+blueconst;
                tric[0] = new Color(tricR,tricG,tricB);
                xvxendL[0] = (int)(((tx[0]+tx[8])*340/(tz[0]+tz[8]))+160); xvxendL[1]= (int)(((tx[1]+tx[8])*340/(tz[1]+tz[8]))+160); xvxendL[2] = (int)(((tx[2]+tx[8])*340/(tz[2]+tz[8]))+160);xvxendL[3] = (int)(((tx[3]+tx[8])*340/(tz[3]+tz[8]))+160);
                yvxendL[0] = (int)(((ty[0]+ty[8])*340/(tz[0]+tz[8]))+160); yvxendL[1]= (int)(((ty[1]+ty[8])*340/(tz[1]+tz[8]))+160); yvxendL[2] = (int)(((ty[2]+ty[8])*340/(tz[2]+tz[8]))+160);yvxendL[3] = (int)(((ty[3]+ty[8])*340/(tz[3]+tz[8]))+160);
                brickside[0] = new Polygon(xvxendL,yvxendL,4);

        }

 void rightpoly(double xtheta,double ytheta)
        {
        yt =ytheta*(pi/180);
        double ct = Math.cos(yt);
        double st = Math.sin(yt);
        float Nax2 = (float)(tx[9+4] * ct + (tz[9+4] * st));
        float Naz2 = (float)(-(tx[9+4] * st) + (tz[9+4] * ct));
        float Nbx2 = (float)(tx[9+5] * ct + (tz[9+5] * st));
        float Nbz2 = (float)(-(tx[9+5] * st) + (tz[9+5] * ct));
        float Ncx2 = (float)(tx[9+6] * ct + (tz[9+6] * st));
        float Ncz2 = (float)(-(tx[9+6] * st)+ (tz[9+6] * ct));
        float Ndx2 = (float)(tx[9+7] * ct + (tz[9+7] * st));
        float Ndz2 = (float)(-(tx[9+7] * st)+ (tz[9+7] * ct));
        tx[4] = Nax2; tz[4] = Naz2;
        tx[5] = Nbx2; tz[5] = Nbz2;
        tx[6] = Ncx2; tz[6] = Ncz2;
        tx[7] = Ndx2; tz[7] = Ndz2;
        xt = xtheta*(pi/180);
        ct = Math.cos(xt);
        st = Math.sin(xt);
        float Nay2 = (float)(ty[9+4] * ct - (tz[4] * st));
         Naz2 = (float)(ty[9+4] * st + (tz[4] * ct));
        float Nby2 = (float)(ty[9+5] * ct - (tz[5] * st));
         Nbz2 = (float)(ty[9+5] * st + (tz[5] * ct));
        float Ncy2 = (float)(ty[9+6] * ct - (tz[6] * st));
         Ncz2 = (float)(ty[9+6] * st + (tz[6] * ct));
        float Ndy2 = (float)(ty[9+7] * ct - (tz[7] * st));
         Ndz2 = (float)(ty[9+7] * st + (tz[7] * ct));
         ty[4] = Nay2; tz[4] = Naz2;
        ty[5] = Nby2; tz[5] = Nbz2;
        ty[6] = Ncy2; tz[6] = Ncz2;
        ty[7] = Ndy2; tz[7] = Ndz2;
         Pax[1] = tx[5] - tx[4];Pbx[1] = tx[5] - tx[6];
         Pay[1] = ty[5] - ty[4];Pby[1] = ty[5] - ty[6];
         Paz[1] = tz[5] - tz[4];Pbz[1] = tz[5] - tz[6];//endR
         vx[1]=tx[4]; vy[1]=ty[4]; vz[1]=tz[4];
         float CAMx = -tx[8];
         float CAMy = ty[8];
         float CAMz = -tz[8];
         if(rd<31)redconst=rd;else redconst =90;
         if(gn<31)greenconst=gn;else greenconst =90;
         if(bl<31)blueconst=bl;else blueconst = 90;
         PVx[1] = (double)(Pay[1]*Pbz[1])-(Paz[1]*Pby[1]);
                PVy[1] = (double)(Paz[1]*Pbx[1])-(Pax[1]*Pbz[1]);
                PVz[1] = (double)(Pax[1]*Pby[1])-(Pay[1]*Pbx[1]);
                PVM[1] = Math.sqrt(Math.pow(PVx[1],2)+Math.pow(PVy[1],2)+Math.pow(PVz[1],2));
                //normalize it
                PVxNorm[1] = (float)(PVx[1]/PVM[1]);
                PVyNorm[1] = (float)(PVy[1]/PVM[1]);
                PVzNorm[1] = (float)(PVz[1]/PVM[1]);
                //compute plane constant
                planeconst[1] = -((PVxNorm[1]*vx[1])+(PVyNorm[1]*vy[1])+(PVzNorm[1]*vz[1]));
                NV[1] = (PVxNorm[1] * CAMx)+(PVyNorm[1] * CAMy)+(PVzNorm[1] * CAMz);
                BF[1] = planeconst[1] + NV[1]+Override[1];
                //lightsource vector
                LV[1] = (PVxNorm[1] * litex)+(PVyNorm[1] * litey)+(PVzNorm[1] * litez);
                LVM[1] =  Math.sqrt(Math.pow((double)(litex),2)+Math.pow((double)(litey),2)+Math.pow((double)(litez),2));
                LF[1] = (((planeconst[1] - LV[1])/((float)(LVM[1])))+1)/2;//LF returns a value between 0(black) and 1(white).
                tricR = (int)(LF[1]*rd/2)+redconst;
                tricG = (int)(LF[1]*gn/2)+greenconst;
                tricB = (int)(LF[1]*bl/2)+blueconst;
                tric[1] = new Color(tricR,tricG,tricB);
                xvxendR[0] = (int)(((tx[4]+tx[8])*340/(tz[4]+tz[8]))+160); xvxendR[1]= (int)(((tx[5]+tx[8])*340/(tz[5]+tz[8]))+160); xvxendR[2] = (int)(((tx[6]+tx[8])*340/(tz[6]+tz[8]))+160);xvxendR[3] = (int)(((tx[7]+tx[8])*340/(tz[7]+tz[8]))+160);
                yvxendR[0] = (int)(((ty[4]+ty[8])*340/(tz[4]+tz[8]))+160); yvxendR[1]= (int)(((ty[5]+ty[8])*340/(tz[5]+tz[8]))+160); yvxendR[2] = (int)(((ty[6]+ty[8])*340/(tz[6]+tz[8]))+160);yvxendR[3] = (int)(((ty[7]+ty[8])*340/(tz[7]+tz[8]))+160);
                brickside[1] = new Polygon(xvxendR,yvxendR,4);

        }

  void frontpoly(double xtheta,double ytheta)
        {
        yt =ytheta*(pi/180);
        double ct = Math.cos(yt);
        double st = Math.sin(yt);
        float Nbx = (float)(tx[9+1] * ct + (tz[9+1] * st));
        float Nbz = (float)(-(tx[9+1] * st) + (tz[9+1] * ct));
        float Ncx = (float)(tx[9+2] * ct + (tz[9+2] * st));
        float Ncz = (float)(-(tx[9+2] * st)+ (tz[9+2] * ct));
        float Nbx2 = (float)(tx[9+5] * ct + (tz[9+5] * st));
        float Nbz2 = (float)(-(tx[9+5] * st) + (tz[9+5] * ct));
        float Ncx2 = (float)(tx[9+6] * ct + (tz[9+6] * st));
        float Ncz2 = (float)(-(tx[9+6] * st)+ (tz[9+6] * ct));
        tx[1] = Nbx; tz[1] = Nbz;
        tx[2] = Ncx; tz[2] = Ncz;
        tx[5] = Nbx2; tz[5] = Nbz2;
        tx[6] = Ncx2; tz[6] = Ncz2;
        xt = xtheta*(pi/180);
        ct = Math.cos(xt);
        st = Math.sin(xt);
        float Nby = (float)(ty[9+1] * ct - (tz[1] * st));
         Nbz = (float)(ty[9+1] * st + (tz[1] * ct));
        float Ncy = (float)(ty[9+2] * ct - (tz[2] * st));
         Ncz = (float)(ty[9+2] * st + (tz[2] * ct));
        float Nby2 = (float)(ty[9+5] * ct - (tz[5] * st));
         Nbz2 = (float)(ty[9+5] * st + (tz[5] * ct));
        float Ncy2 = (float)(ty[9+6] * ct - (tz[6] * st));
         Ncz2 = (float)(ty[9+6] * st + (tz[6] * ct));
        ty[1] = Nby; tz[1] = Nbz;
        ty[2] = Ncy; tz[2] = Ncz;
        ty[5] = Nby2; tz[5] = Nbz2;
        ty[6] = Ncy2; tz[6] = Ncz2;
        Pax[4] = tx[2] - tx[1];Pbx[4] = tx[1] - tx[5];
         Pay[4] = ty[2] - ty[1];Pby[4] = ty[1] - ty[5];
         Paz[4] = tz[2] - tz[1];Pbz[4] = tz[1] - tz[5];//front
          vx[4]=tx[1]; vy[4]=ty[1]; vz[4]=tz[1];
         float CAMx = -tx[8];
         float CAMy = ty[8];
         float CAMz = -tz[8];
         if(rd<31)redconst=rd;else redconst =90;
         if(gn<31)greenconst=gn;else greenconst =90;
         if(bl<31)blueconst=bl;else blueconst = 90;
         PVx[4] = (double)(Pay[4]*Pbz[4])-(Paz[4]*Pby[4]);
                PVy[4] = (double)(Paz[4]*Pbx[4])-(Pax[4]*Pbz[4]);
                PVz[4] = (double)(Pax[4]*Pby[4])-(Pay[4]*Pbx[4]);
                PVM[4] = Math.sqrt(Math.pow(PVx[4],2)+Math.pow(PVy[4],2)+Math.pow(PVz[4],2));
                //normalize it
                PVxNorm[4] = (float)(PVx[4]/PVM[4]);
                PVyNorm[4] = (float)(PVy[4]/PVM[4]);
                PVzNorm[4] = (float)(PVz[4]/PVM[4]);
                //compute plane constant
                planeconst[4] = -((PVxNorm[4]*vx[4])+(PVyNorm[4]*vy[4])+(PVzNorm[4]*vz[4]));
                NV[4] = (PVxNorm[4] * CAMx)+(PVyNorm[4] * CAMy)+(PVzNorm[4] * CAMz);
                BF[4] = planeconst[4] + NV[4]+Override[4];
                //lightsource vector
                LV[4] = (PVxNorm[4] * litex)+(PVyNorm[4] * litey)+(PVzNorm[4] * litez);
                LVM[4] =  Math.sqrt(Math.pow((double)(litex),2)+Math.pow((double)(litey),2)+Math.pow((double)(litez),2));
                LF[4] = (((planeconst[4] - LV[4])/((float)(LVM[4])))+1)/2;//LF returns a value between 0(black) and 1(white).
                tricR = (int)(LF[4]*rd/2)+redconst;
                tricG = (int)(LF[4]*gn/2)+greenconst;
                tricB = (int)(LF[4]*bl/2)+blueconst;
                tric[4] = new Color(tricR,tricG,tricB);
                xvxfront[0] = (int)(((tx[1]+tx[8])*340/(tz[1]+tz[8]))+160);
                xvxfront[1] = (int)(((tx[5]+tx[8])*340/(tz[5]+tz[8]))+160);
                xvxfront[2] = (int)(((tx[6]+tx[8])*340/(tz[6]+tz[8]))+160);
                xvxfront[3] = (int)(((tx[2]+tx[8])*340/(tz[2]+tz[8]))+160);
                yvxfront[0] = (int)(((ty[1]+ty[8])*340/(tz[1]+tz[8]))+160);
                yvxfront[1] = (int)(((ty[5]+ty[8])*340/(tz[5]+tz[8]))+160);
                yvxfront[2] = (int)(((ty[6]+ty[8])*340/(tz[6]+tz[8]))+160);
                yvxfront[3] = (int)(((ty[2]+ty[8])*340/(tz[2]+tz[8]))+160);
                brickside[4] = new Polygon(xvxfront,yvxfront,4);


    }

  void backpoly(double xtheta,double ytheta)
        {
        yt =ytheta*(pi/180);
        double ct = Math.cos(yt);
        double st = Math.sin(yt);
        float Nax = (float)(tx[9+0] * ct + (tz[9+0] * st));
        float  Naz = (float)(-(tx[9+0] * st) + (tz[9+0] * ct));
        float Ndx = (float)(tx[9+3] * ct + (tz[9+3] * st));
        float Ndz = (float)(-(tx[9+3] * st)+ (tz[9+3] * ct));
        float Nax2 = (float)(tx[9+4] * ct + (tz[9+4] * st));
        float Naz2 = (float)(-(tx[9+4] * st) + (tz[9+4] * ct));
        float Ndx2 = (float)(tx[9+7] * ct + (tz[9+7] * st));
        float Ndz2 = (float)(-(tx[9+7] * st)+ (tz[9+7] * ct));

        tx[0] = Nax; tz[0] = Naz;
        tx[3] = Ndx; tz[3] = Ndz;
        tx[4] = Nax2; tz[4] = Naz2;
        tx[7] = Ndx2; tz[7] = Ndz2;
        xt = xtheta*(pi/180);
        ct = Math.cos(xt);
        st = Math.sin(xt);
        float Nay = (float)(ty[9+0] * ct - (tz[0] * st));
        Naz = (float)(ty[9+0] * st + (tz[0] * ct));
        float Ndy = (float)(ty[9+3] * ct - (tz[3] * st));
         Ndz = (float)(ty[9+3] * st + (tz[3] * ct));
        float Nay2 = (float)(ty[9+4] * ct - (tz[4] * st));
         Naz2 = (float)(ty[9+4] * st + (tz[4] * ct));
        float Ndy2 = (float)(ty[9+7] * ct - (tz[7] * st));
         Ndz2 = (float)(ty[9+7] * st + (tz[7] * ct));
        ty[0] = Nay; tz[0] = Naz;
        ty[3] = Ndy; tz[3] = Ndz;
        ty[4] = Nay2; tz[4] = Naz2;
        ty[7] = Ndy2; tz[7] = Ndz2;
        Pax[3] = tx[3] - tx[0];Pbx[3] = tx[7] - tx[3];
        Pay[3] = ty[3] - ty[0];Pby[3] = ty[7] - ty[3];
        Paz[3] = tz[3] - tz[0];Pbz[3] = tz[7] - tz[3];//back
        vx[3]=tx[0]; vy[3]=ty[0]; vz[3]=tz[0];
        float CAMx = -tx[8];
         float CAMy = ty[8];
         float CAMz = -tz[8];
         if(rd<31)redconst=rd;else redconst =90;
         if(gn<31)greenconst=gn;else greenconst =90;
         if(bl<31)blueconst=bl;else blueconst = 90;
                PVx[3] = (double)(Pay[3]*Pbz[3])-(Paz[3]*Pby[3]);
                PVy[3] = (double)(Paz[3]*Pbx[3])-(Pax[3]*Pbz[3]);
                PVz[3] = (double)(Pax[3]*Pby[3])-(Pay[3]*Pbx[3]);
                PVM[3] = Math.sqrt(Math.pow(PVx[3],2)+Math.pow(PVy[3],2)+Math.pow(PVz[3],2));
                //normalize it
                PVxNorm[3] = (float)(PVx[3]/PVM[3]);
                PVyNorm[3] = (float)(PVy[3]/PVM[3]);
                PVzNorm[3] = (float)(PVz[3]/PVM[3]);
                //compute plane constant
                planeconst[3] = -((PVxNorm[3]*vx[3])+(PVyNorm[3]*vy[3])+(PVzNorm[3]*vz[3]));
                NV[3] = (PVxNorm[3] * CAMx)+(PVyNorm[3] * CAMy)+(PVzNorm[3] * CAMz);
                BF[3] = planeconst[3] + NV[3]+Override[3];
                //lightsource vector
                LV[3] = (PVxNorm[3] * litex)+(PVyNorm[3] * litey)+(PVzNorm[3] * litez);
                LVM[3] =  Math.sqrt(Math.pow((double)(litex),2)+Math.pow((double)(litey),2)+Math.pow((double)(litez),2));
                LF[3] = (((planeconst[3] - LV[3])/((float)(LVM[3])))+1)/2;//LF returns a value between 0(black) and 1(white).
                tricR = (int)(LF[3]*rd/2)+redconst;
                tricG = (int)(LF[3]*gn/2)+greenconst;
                tricB = (int)(LF[3]*bl/2)+blueconst;
                tric[3] = new Color(tricR,tricG,tricB);
                xvxback[0] = (int)(((tx[0]+tx[8])*340/(tz[0]+tz[8]))+160);
                xvxback[1] = (int)(((tx[3]+tx[8])*340/(tz[3]+tz[8]))+160);
                xvxback[2] = (int)(((tx[7]+tx[8])*340/(tz[7]+tz[8]))+160);
                xvxback[3] = (int)(((tx[4]+tx[8])*340/(tz[4]+tz[8]))+160);
                yvxback[0] = (int)(((ty[0]+ty[8])*340/(tz[0]+tz[8]))+160);
                yvxback[1] = (int)(((ty[3]+ty[8])*340/(tz[3]+tz[8]))+160);
                yvxback[2] = (int)(((ty[7]+ty[8])*340/(tz[7]+tz[8]))+160);
                yvxback[3] = (int)(((ty[4]+ty[8])*340/(tz[4]+tz[8]))+160);
                brickside[3] = new Polygon(xvxback,yvxback,4);
    }



}

