// Transiente Absorption: Schematische Darstellung
// Ingo Ramsteiner (http://www.iramsteiner.de)
// November 2000

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

public class TASchema extends Applet implements MouseListener {

    private Color PulsfarbeHell, PulsfarbeDunkel;
    private int x1=0, y1=0, dx=0, x2=0, y2=0, dy=0;
    private Font font1, smallfont;

    public void paint(Graphics g) {

	// Komponenten-Setup
	Laser(g,340,40);
	Divider(g,140,40,1);
	Mirror(g,50,40,2);
	Frequencydoubler(g,53,300);
	Mirror(g,140,160,1); Mirror(g,270,160,3); // Delayline
	Mirror(g,270,200,4); Mirror(g,140,200,2); // Delayline
	Weiss(g,140,300);       
	Mirror(g,50,440,1);
	Divider(g,140,440,2);
	Probe(g,270,437,Color.blue);
	Detector(g,355,437);

	// Beschriftungen
	g.setFont(smallfont);
	g.setColor(Color.green);
	g.drawString("Freq.-",62,298);
	g.drawString("Doubl.",62,317);
	g.drawString("Weisslicht-",190,295); 
	g.drawString("Generator",190,314);
	g.drawString("Delay Line",185,185);
	g.drawString("Detektor",327,410);
	g.drawString("Probe",253,410);
	g.drawString("Ti:Sa-Laser",300,100);

	// Rechtecke
	g.setColor(Color.white);
	g.drawRect(120,140,170,80);   // um Delayline
	g.drawRect(240,390,60,70);    // um Probe
	g.drawRect(320,390,70,70);    // um Detektor
	g.drawRect(120,265,170,70);   // um WLG
	g.drawRect(23,265,85,70);     // um Frequenzverdoppler
    }

    public void init() {
	setBackground(Color.black);
	font1     = new Font("Helvetica",Font.BOLD + Font.ITALIC,19);
	smallfont = new Font("Helvetica",Font.PLAIN,13);
	this.addMouseListener(this);
    }

    // Komponenten

    public void Laser(Graphics gr,int x,int y){
	gr.setColor(Color.gray);
	gr.fillRect(x-40,y-20,80,55);
	gr.setColor(Color.lightGray);
	gr.drawRect(x-40,y-20,80,55);
    }

    public void Divider(Graphics gr,int x,int y, int Alignment){
	//Alignment 1: "/", 2: "\"
	gr.setColor(Color.cyan);
	if (Alignment == 1) gr.drawLine(x-20,y+20,x+20,y-20);
	if (Alignment == 2) gr.drawLine(x-20,y-20,x+20,y+20);
    }

    public void Frequencydoubler(Graphics gr, int x,int y){
	gr.setColor(Color.cyan);
	gr.fillRect(x-5,y-5,10,10);
	gr.setColor(Color.white);
	gr.drawRect(x-5,y-5,10,10); 
    }

    public void Mirror(Graphics gr, int x, int y, int Alignment){
	//Alignment: 1 topright, 2 bottomright, 3 bottomleft, 4 topleft
	x1 = x-10; x2= x+10; y1=y-10; y2=y+10; dx=-1; dy=1;
	if (Alignment == 2) {x1=x1+20; x2=x2-20; dy=-1;}
	if (Alignment == 3) {dx=1; dy=-1;}
	if (Alignment == 4) {x1=x1+20; x2=x2-20; dx=1; dy=1;}
	gr.setColor(Color.white);
	gr.drawLine(x1,y1,x2,y2);
	gr.setColor(Color.gray);
	gr.drawLine(x1+dx,y1,x2+dx,y2);
	gr.drawLine(x1+dx,y1+dy,x2+dx,y2+dy);
    }

    public void Weiss(Graphics gr, int x, int y){
	gr.setColor(Color.gray);        gr.fillRect(x-5,y-2,50,4);
	gr.setColor(Color.lightGray);   gr.fillRect(x+10,y-2,30,4);
	gr.setColor(Color.white);       gr.fillRect(x+20,y-2,10,4); 
    }

    public void Probe(Graphics gr, int x, int y, Color Farbe) {
	gr.setColor(Farbe);           gr.fillRect(x-5,y-8,10,20);
	gr.setColor(Color.white);     gr.drawRect(x-5,y-15,10,27);
    }

    public void Detector(Graphics gr, int x, int y) {
	gr.setColor(Color.gray);      gr.fillRect(x-15,y-7,30,14);
	gr.setColor(Color.lightGray); gr.fillRect(x-15,y-5,30,5);
	gr.setColor(Color.white);     gr.fillRect(x-15,y-4,30,2);
    }

    public void mousePressed(MouseEvent event) {
	Graphics g = getGraphics();
	Pulse pulssequenz = new Pulse(g);
	pulssequenz.start();
    }

    public void mouseReleased(MouseEvent event) {}
    public void mouseClicked(MouseEvent event) {}
    public void mouseEntered(MouseEvent event) {}
    public void mouseExited(MouseEvent event) {}

    //}

class Pulse extends Thread {

    private int x1,y1,dx1,dy1,x2,y2,dx2,dy2;
    private Color farbe1,farbe2;
    private boolean sicht1,sicht2,ende;
    private Graphics gr;

    public Pulse(Graphics graphics){
	gr = graphics;
    }

    public void run(){

	//Anfangswerte
	x1=294; y1=40; dx1=-1; dy1=0; sicht1=true;  farbe1=Color.magenta;
	x2=294; y2=40; dx2=-1; dy2=0; sicht2=false; farbe2=Color.magenta;
	ende=false;

	while (ende == false) {

	    // Pulse zeichnen
	    gr.setColor(farbe1);
	    if (sicht1 == true) gr.fillOval(x1,y1,6,6);
	    gr.setColor(farbe2);
	    if (sicht2 == true) gr.fillOval(x2,y2,6,6);

	    // Zeitverzoegerung
	    try{
	    	Thread.sleep(20);
	    }
	    catch (InterruptedException e) {
	    	System.err.println("Thread Interrupted");
	    }

	    // Pulse loeschen   
	    gr.setColor(Color.black);
	    if (sicht1 == true) gr.fillOval(x1,y1,6,6);
	    gr.setColor(Color.black);
	    if (sicht2 == true) gr.fillOval(x2,y2,6,6);
	    // Rechtecke wiederherstellen	
	    gr.setColor(Color.white);
	    gr.drawRect(120,140,170,80);   // um Delayline
	    gr.drawRect(240,390,60,70);    // um Probe
	    gr.drawRect(320,390,70,70);    // um Detektor
	    gr.drawRect(120,265,170,70);   // um WLG
	    gr.drawRect(23,265,85,70);     // um Frequenzverdoppler


	    // Strahlengang festlegen - Abfrage
	    if ((x1==140)&&(y1==40)) {dx1=0; dy1=+1;}
	    if ((x1==140)&&(y1==155)) {dx1=1; dy1=0;}
	    if ((x1==260)&&(y1==155)) {dx1=0; dy1=+1;}
	    if ((x1==260)&&(y1==198)) {dx1=-1; dy1=0;}
	    if ((x1==142)&&(y1==198)) {dx1=0; dy1=+1;}
	    if ((x1==142)&&(y1==292)) {sicht1=false;}
	    if ((x1==142)&&(y1==301)) {sicht1=true; farbe1=Color.white;}
	    if ((x1==142)&&(y1==435)) {dx1=+1; dy1=0;}
	    if ((x1==294)&&(y1==435)) {sicht1=false;}
	    if ((x1==300)&&(y1==435)) {sicht1=true;}
	    if ((x1==259)&&(y1==435)) {sicht1=false;}
	    if ((x1==276)&&(y1==435)) {sicht1=true;}
	    if ((x1==335)&&(y1==435)) {sicht1=false; ende = true; repaint();}
	
	    // Strahlengang festlegen - Anregung
	    if ((x2==130)&&(y2==40)) {sicht2 = true;}
	    if ((x2==50)&&(y2==40)) {dx2=0; dy2=1;}
	    if ((x2==50)&&(y2==288)) {sicht2=false;}
	    if ((x2==50)&&(y2==305)) {sicht2=true; farbe2=Color.cyan;}
	    if ((x2==50)&&(y2==435)) {dx2=+1; dy2=0;}
	    if ((x2==130)&&(y2==435)) {sicht2=false;}
	    if ((x2==142)&&(y2==435)) {sicht2=true;}
	    if ((x2==259)&&(y2==435)) {
		sicht2=false; dx2=0; // angeregte Probe zeichnen
		gr.setColor(Color.cyan); gr.fillRect(265,429,10,20);
	    }


	    // Position aktualisieren
	    x1=x1+dx1; y1=y1+dy1;
	    x2=x2+dx2; y2=y2+dy2;
	    
	}
    }
}

}


