import java.awt.*;
import java.applet.*;

class SampleFrame extends Frame  {
   SampleFrame(String title)  {
     super(title);
     }
     
 public boolean handleEvent(Event evtObj)  {
     if(evtObj.id == Event.WINDOW_DESTROY)  {
     hide();
     return true;
   }
  return super.handleEvent(evtObj);
  }
  
  public void paint(Graphics g)  {
     g.drawString("JOS SAAT",20,40);
     g.drawString("Koodaa itse tapahtumankäsittely",10,70);
     }
 }
 
 public class AppletFrameCatch extends Applet implements Runnable{
     SampleFrame f;
     Thread saie1;
     int x,y,sleepTime, leftBorder, downBorder;
     Dimension screenDim;
     boolean toLeft, down;
     
     public void init() {
       f = new SampleFrame("Ota kiinni");
       Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize(); 
       f.show();
       f.resize(250,100);
       leftBorder=screenDim.width-250;
       downBorder=screenDim.height-100;
       sleepTime=100;
       down=true;
       x=0;
       y=200;
     
     }
     /*Coding help information -koodauksen aikaista tietoa*/
  public void paint(Graphics g) {
  	g.drawString(" "+x+toLeft, 50,50);
  	}
    
     public void start()
      {
	 if (saie1==null) {
	 saie1 = new Thread(this);
	 saie1.start();
	   }
	  }
   public void run() {
   	      while (true)
        {
        /*Stop for reading, pysäytä jotta voi lukea*/
       if(Math.random()<=.02) sleepTime=1500;
       else sleepTime = 100;
            
       if (x>=leftBorder)toLeft=true;
       if (x<=0)toLeft=false;
       if (y>=downBorder) down=false;
       if (y<=0) down=true;
               if (toLeft==true) x=x-50;
                if (toLeft==false)   
                 x=x+50;
                
              if (down)  y=y+10;
              if(!down)y=y-10;
                  repaint();      
               
        f.move(x,y);
              
                
		try {saie1.sleep(sleepTime);
		
		} catch (InterruptedException e) {}
	   }
	  }
	  public void stop()  {
	     if (saie1 !=null)  {
		 saie1.stop();
		 saie1 = null;
		}
		f.hide();
        }
   }


    // Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize(); 
 // screenDim.width =screenDim.width+200;
  //screenDim.height =screenDim.height+200;
  /* Show the frame */ 
  //setSize(screenDim.width,screenDim.height);
  
 
