import java.awt.*;
import java.util.Random;
import java.awt.event.*;

public
class leotsunamitwo extends java.applet.Applet implements Runnable,ActionListener {
	public Image offScreenImage;
	public double height[]=new double[1001];
	public double depth[]=new double[1001];
	public double amp[]=new double[1001];
	public double vel[]=new double[1001];
	public double height2[]=new double[1001];
	public double velocity;
	public int leftEdge;
	public double omega;//angular frequency=2*pi*frequency, frequency is set to 5/hr=0.0014hz
	public double lamda; //wavelength=sqrt(g*depth)/frequency
    public int profile;
	Color blueColor;
    Color bg;
    Color redColor;
    Color black;
    Button runButton;
    Button stepButton;
    Button stopButton;
    Button measButton;
    
    
    
    Random rand=new Random();
    
    public boolean holdIt;
    public int frame;
    public int delay;
    Thread animator;

    /**
     * Initialize the applet and compute the delay between frames.
     */
    public void init()
    
   {
	String str = getParameter("fps");
	int fps = (str != null) ? Integer.parseInt(str) : 10;
	delay = 5;
	frame=0;
    blueColor=Color.blue;
    bg=Color.orange;
    redColor=Color.red;
    black=Color.black;
    setBackground(bg);
    setLayout(null);
    resize(1000,600);
    omega=2*Math.PI/300;
    profile=1;
    runButton=new Button("Run");
    stepButton=new Button("Step");
    stopButton=new Button("Stop");
    measButton=new Button("Measure");
    
    runButton.setBounds(5,5,50,50);
    stepButton.setBounds(55,5,50,50);
    stopButton.setBounds(105,5,50,50);
    measButton.setBounds(155,5,50,50);
    
    add(runButton);
    add(stepButton);
    add(stopButton);
    add(measButton);
   
   runButton.addActionListener(this);
   stepButton.addActionListener(this);
   stopButton.addActionListener(this);
   measButton.addActionListener(this);
   
    //for (int i=1;i<=1000;i++)vel[i]=Math.sqrt((300-depth[i])/100);
    for (int i=1;i<=290;i++)height[i]=0;
    for (int i=290;i<=590;i++)
    {
    	//height[i]=50*Math.sin((omega/(Math.sqrt(9.8*3000)))*i);
    	height[i]=-5*Math.sin(omega*i+3);
    }
    for (int i=590;i<=1000;i++)
    {
    	height[i]=0;
    }
    
    }
    public void actionPerformed(ActionEvent evt)
    {
    	if(evt.getSource()==stopButton)
    		stop();
    	if(evt.getSource()==runButton)
    		start();
    }
    public void start() {
	animator = new Thread(this);
	animator.start();
    }
  
    public void run() {
	// Remember the starting time
	long tm = System.currentTimeMillis();
	while (Thread.currentThread() == animator) {
	    // Display the next frame of animation.
	    repaint();
	    try {
		tm += delay;
		Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));
	    } catch (InterruptedException e) {
		break;
	    }

	    frame++;
	}
    }
    public boolean handleEvent(Event event){
    	if(event.target==this && event.id==Event.KEY_PRESS){
    		String thePressedKey=""+(char)event.key;
    		/*if (thePressedKey.equalsIgnoreCase("1")){
    			profile=1;
    			repaint();
    		}
    		if (thePressedKey.equalsIgnoreCase("2")){
    			profile=2;
    			repaint();
    		}
    		if (thePressedKey.equalsIgnoreCase("s")){
    			holdIt=!holdIt;
    			if(holdIt){
    				stop();
    			}
    			else{
    				start();
    				repaint();
    			}
    			}*/
    		}
    	return false;
    	}
    
  
    public void stop() {
    	animator.stop();
	animator = null;
    }

    public void paint(Graphics g) {
	update(g);
    }
    public void update(Graphics g) {
       if (profile==1){
    	for (int i=1;i<=700;i++)
    		{
    		depth[i]=0;
    		}
        for (int i=700;i<=850;i++)depth[i]=1.9*i-1330;
        for (int i=850;i<=900;i++)depth[i]=.3*i+30;
        for (int i=900;i<=1000;i++)depth[i]=300;}
       if (profile==2){
    	   	for (int i=1;i<=700;i++)depth[i]=0;
            for (int i=700;i<=850;i++)depth[i]=111*Math.sqrt(i)-2937;
            for (int i=850;i<=1000;i++)depth[i]=300;}
      g.setColor(black);
      g.fillRect(950,280,8,8);
      g.drawLine(954, 288, 954, 294);
      g.drawLine(954,294,950,300);
      g.drawLine(954,294,958,300);
      g.drawLine(954,293,959,287);
      g.drawLine(954,293,949,287);
    	 g.setColor(blueColor);
    	 for (int i=1;i<=1000;i++){
    		 g.fillRect(i,(int)(300-height[i]),1,(int)((300-depth[i])+height[i]));
    	 }
    	 g.setColor(bg);
    	 for (int i=1;i<=1000;i++)
    	 {
    	 g.fillRect(i,1, 1, (int)(300-height[i]));
    	 }
    	 g.setColor(redColor);
    	 for (int i=1;i<=1000;i++)
    	 {
    		 g.fillRect(i,(int)(600-depth[i]),1,(int)depth[i]);
    	 }
    	 for (int i=999;i>0;i--)
 	    {	
    		velocity=Math.sqrt((301-depth[i])/300);
    		
 	    	height[i+1]=velocity*height[i]+height[i+1];
 	    	height[i]=height[i]*(1-velocity);
 	    }
    
    }
}
