/*
 Sin_Grating_Output.java

 Animated graph for explaining diffraction by a sinusoidal grating.

 Copyright (c) R Hunt November 2001.
 Last change:  RLH   6 Nov 2001   10:24 pm
*/

import java.awt.*;
import java.util.StringTokenizer;


// E is the input E field

public class Sin_Grating_Output extends java.applet.Applet implements Runnable
{
    Thread runner = null;    // The thread that displays the images
    private Image Buffer;
    private Graphics gBuffer;
    int x ,E=-10;
    boolean up=true;
    double f1(double x, double E)
    {
	return ((Math.sin(x/7)*E/10 - E/10) * getSize().height / 16);
    }
    double f2(double x, double E)
    {
	return ((Math.sin(x/7)*E/10) * getSize().height / 16);
    }

    public void init()
    {
	Buffer=createImage((getSize().width),(getSize().height)); // Create graphics buffer same size as
	gBuffer=Buffer.getGraphics();  		               	// applet for flicker free graphics.
    }


    public void start()
    {
	runner = new Thread(this);
	runner.start();
    }


    public void stop()
    {
	runner.stop();
	runner = null;
    }


    public void run()
    {
	while (true)
	{
            try
	    {
                runner.sleep(100);
            }
            catch (InterruptedException e) { }
	    gBuffer.setColor(Color.blue);
	    gBuffer.fillRect(0,0, getSize().width,getSize().height); //	Background
            gBuffer.setColor(Color.cyan);
	    gBuffer.drawLine(10, 10, 10, (getSize().height / 3) - 10); // Top vert axis
	    gBuffer.drawLine(10, ((getSize().height / 3) + 10), 10, (getSize().height * 2 / 3) - 10); // Middle vert axis
	    gBuffer.drawLine(10, ((getSize().height * 2 / 3) + 10), 10, (getSize().height) - 10); // Bottom vert axis
	    gBuffer.drawLine(0, (getSize().height / 6), getSize().width, (getSize().height / 6)); // Top horiz axis
            gBuffer.drawLine(0, (getSize().height / 2), getSize().width, (getSize().height / 2)); // Middle horiz axis
            gBuffer.drawLine(0, (getSize().height * 5 / 6), getSize().width, (getSize().height *5 / 6)); // Bottom horiz axis
            gBuffer.setFont(new Font("TimesRoman", Font.PLAIN, 10));
	    gBuffer.drawString("x", getSize().width-10, getSize().height / 6);
	    gBuffer.drawString("E", 10, 20);
	    gBuffer.drawString("x", getSize().width-10, getSize().height / 2);
	    gBuffer.drawString("E", 10, getSize().height * 2 / 6 + 20);
	    gBuffer.drawString("x", getSize().width-10, getSize().height * 5 / 6);
	    gBuffer.drawString("E", 10, getSize().height * 4 / 6 + 20);
            gBuffer.setFont(new Font("TimesRoman", Font.PLAIN, 24));
	    gBuffer.drawString("=", getSize().width / 2, getSize().height * 2 / 6 + 10);
	    gBuffer.drawString("+", getSize().width / 2, getSize().height * 4 / 6 + 10);
	    if ( E==10 )
	        up=false;
	    if ( E==-10 )
	        up=true;
    	    for (int x = 0 ; x < getSize().width ; x+=2)
    	    {
	        gBuffer.setColor(Color.yellow);
	        gBuffer.drawLine(x, (int)f1(x,E) + (getSize().height / 6), x + 2, (int)f1(x + 1,E) + (getSize().height / 6)); // Top graph
	        gBuffer.drawLine(x, (int)f2(x,E) + (getSize().height * 5 / 6), x + 2, (int)f2(x + 1,E) + (getSize().height * 5 / 6)); // Bottom graph
            }
                gBuffer.drawLine(0, ((getSize().height / 2)-(getSize().height / 16)*E/10), getSize().width,
		    ((getSize().height / 2)-(getSize().height / 16)*E/10)); // Middle graph
	    if ( up==true )
	    	E++;
	    else
	    	E--;
	    repaint();
	}
    }

    public void update(Graphics g) //Prevents background being overwritten.
    {
	paint(g);
    }

    public void paint(Graphics g)
    {
	g.drawImage (Buffer,0,0, this);
    }

    public String getAppletInfo()
    {
  	return "Animated graph for explaining diffraction by a sinusoidal grating.";
    }

}
