/**acetoy(number).java by gregvan 
 * RECOMMENDED SIZE: height 450, width 600
 **/

import java.applet.*;
import java.awt.*;
import java.awt.event.*;//new

public class acetoy3 extends Applet implements Runnable
{
int h,hh;  //line endpoints
int h1,hh1;   
int hhq=1;//endpoint up down factors
int hq=1;
int h1q=1;
int hh1q=2;
int w;//speed of line endpoint counters
int rr,gg,bb;//color variables 
int bw;//black and white 
int bwq=1;      
int rq=1; // color up-down factors
int gq=1;
int bq=1;
int wwq,wq;//color change speed
int cx; //center of screen x
int cy; //center of screen y
TextField num=new TextField(1);//new
int anum; //value of Textfield num
TextField num1=new TextField(1);//new
int anum1; //value of Textfield num1
TextField num2=new TextField(1);//new
int anum2; //value of Textfield num2
TextField num3=new TextField(1);//new
int anum3; //value of Textfield num3


        Thread    myRunner;
        Image     buffer;         
        Dimension appletSize;     
        Graphics  bufferGraphics; 

        public void init()
        {    
        setBackground(Color.black);
        appletSize = this.getSize();     
        buffer = this.createImage(appletSize.width, appletSize.height);
        bufferGraphics = buffer.getGraphics();
	cx=appletSize.width/2;
        cy=appletSize.height/2;
//new...this sets the layout as a BorderLayout
       setLayout(new BorderLayout());
       //the next line adds the TextField named num to the south section
      
        Font chosenFont = new Font("Dialog", Font.BOLD, 15);
	num.setFont(chosenFont);
	num.setForeground(Color.black);
	num.setBackground(Color.white);
	 num.setText("1");
       add("North",num);

num1.setFont(chosenFont);
	num1.setForeground(Color.black);
	num1.setBackground(Color.white);
	 num1.setText("2");
       add("West",num1);

num2.setFont(chosenFont);
	num2.setForeground(Color.black);
	num2.setBackground(Color.white);
	 num2.setText("3");
       add("South",num2);

num3.setFont(chosenFont);
	num3.setForeground(Color.black);
	num3.setBackground(Color.white);
	 num3.setText("4");
       add("East",num3);

//end new...
        }

        public void start()
        {
             if (myRunner == null)
             {
                 myRunner = new Thread(this);
                 myRunner.start();
             }
        }

        public void run()
        {
            Thread  executingThread;                   
            executingThread = Thread.currentThread();

            while (myRunner == executingThread)
            {
              

//random starting colors
rr=(int)(Math.random() *255);
gg=(int)(Math.random() *255);
bb=(int)(Math.random() *255);
bw=(int)(Math.random() *255);
h=bw;
hh=rr;
h1=gg;
hh1=bb;
               repaint();
               try
               {Thread.sleep(1000);}
               catch(InterruptedException e) {} 

               while (myRunner != null)
               {
                  repaint(); 
                  try
                  {Thread.sleep(100); }
                  catch (InterruptedException e) {}
               }  
           } 
        }

       // make sure the animation really stops when exiting webpage
        public void stop()
        { 
          if (myRunner != null)
             { myRunner = null; }
        }

        //used to prevent blinking graphics....
        public void update(Graphics g)
        {paint(g);}

        public void paint(Graphics g)
        {
w=(int)(Math.random() *4);
wq=(int)(Math.random() *15);
wwq=(int)(Math.random() *16);

  
             //for loop to paint many images before sleeping .1 second     
        for (int i=0;i<8;i++)
          {

w=w+1;
if (w>4){w=1;}
wq=wq+1;
if (wq>19){wq=1;}
wwq=wwq+1;
if(wwq>16){wwq=1;}

//Integer.parseInt(txtInputValue.getText());
anum=Integer.parseInt(num.getText());//new
anum1=Integer.parseInt(num1.getText());//new
anum2=Integer.parseInt(num2.getText());//new
anum3=Integer.parseInt(num3.getText());//new
             //color changer up-down counters
           rr=rr+rq;
           if(rr>239){rq=-wq;}
           if(rr<17){rq=wwq;}
	bw=bw+bwq;
           if(bw>236){bwq=-wwq;}
           if(bw<17){bwq=wq;}
         gg=gg+gq;
           if(gg>237){gq=-wwq;}
           if(gg<17){gq=wq;}

           bb=bb+bq;
           if(bb>238){bq=-wq;}
           if(bb<17){bq=wwq;}

 //apply the new numbers for color
        Color cNew = new Color(rr, gg, bb);
        bufferGraphics.setColor(cNew);  
  h1=h1+h1q;
if(h1>500){h1q=-anum;}  
if(h1<-50){h1q=anum;}
 
  hh1=hh1+hh1q;
if(hh1>800){hh1q=-anum1;}   
if(hh1<-200){hh1q=anum1;}   
 bufferGraphics.drawLine(0,h1,hh1,450);
bufferGraphics.drawLine(600,h1,600-hh1,450);
bufferGraphics.drawLine(0,450-h1,hh1,0);
bufferGraphics.drawLine(600,450-h1,600-hh1,0);
          
       


    

//apply the new numbers black 
        Color hNew = new Color(bw, bw, bw);
        bufferGraphics.setColor(hNew);  
  h=h+hq;
if(h>470){hq=-anum2;}
if(h<-20){hq=anum2;}   
  hh=hh+hhq;
if(hh>630){hhq=-anum3;} 
if(hh<-30){hhq=anum3;}
   
 bufferGraphics.drawLine(0,h,hh,450);
bufferGraphics.drawLine(600,h,600-hh,450);
bufferGraphics.drawLine(0,450-h,hh,0);
bufferGraphics.drawLine(600,450-h,600-hh,0);
       
    }//end of for loop i
     // move the entire buffer onto the display screen and change
     // pixels that are a different color...
      g.drawImage(buffer, 0, 0, this);
    }
} // end of the program...

