import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.lang.Math;
import Ekg;



public class EkgUebung extends java.applet.Applet implements ActionListener { 
	 // Graph Applet
    private float Herzwinkel;
    private boolean WinkelAnzeigen;
    private boolean WinkelBeurteilen;
    private boolean WinkelOk;
    private Button ok;
    private TextField WinkelUser;
    private Label Aufforderung;
    private Ekg EinEKG;
    private Button Wuerfeln;
    private Font ControlFont;
    private Button Aufloesung;
	 // Sprache: 0=Deutsch, 1=English, 2=Svenska
	 private int l;
    int AnzahlVersuche;

    public void init() {
		  try {
				l=Integer.parseInt(getParameter("sprache"));
		  }
        catch (NumberFormatException exception) {
				l=0; // Deutsch
		  };
		  EinEKG=new Ekg();
		  setBackground(Color.white);
		  ControlFont=new Font("Helvetica",Font.BOLD,12);
		  String[] sAufforderung={"Gib' einen Winkel ein:",
										  "Enter an angle:",
		                          "Ange en vinkel:"};
		  Aufforderung=new Label(sAufforderung[l]);
		  Aufforderung.setFont(ControlFont);
		  WinkelUser=new TextField(5);
		  WinkelUser.setFont(ControlFont);
		  WinkelUser.addActionListener(this);
		  ok=new Button("OK");
		  ok.addActionListener(this);
		  ok.setFont(ControlFont);
		  String[] sWuerfeln={"Neues EKG würfeln",
									 "Dice a new ECG",
		                      "Slå nytt EKG"};
		  Wuerfeln=new Button(sWuerfeln[l]);
		  Wuerfeln.addActionListener(this);
		  Wuerfeln.setFont(ControlFont);
		  String[] sAufloesung={"AUFLÖSUNG",
										"ANSWER",
		                        "Svar"};
		  Aufloesung=new Button(sAufloesung[l]);
		  Aufloesung.addActionListener(this);
		  Aufloesung.setFont(ControlFont);
		  this.add(Aufforderung);
		  this.add(WinkelUser);
		  this.add(ok);
		  this.add(Wuerfeln);
		  this.add(Aufloesung);
		  NeuerHerzwinkel();
	 }


    public void actionPerformed(ActionEvent e) {
		  if (e.getSource()==Wuerfeln) {
				NeuerHerzwinkel();
				repaint();
        }
		  else 
				if ((e.getSource()==ok)||(e.getSource()==WinkelUser)) {
					 PruefeEingabe();
					 repaint();
				}
				else
					 if (e.getSource()==Aufloesung) {
						  WinkelAnzeigen=true;
						  WinkelBeurteilen=false;
						  repaint();
					 }
	 }


    private void PruefeEingabe() {
		  try {
				AnzahlVersuche++;
				int Achse=Integer.parseInt(WinkelUser.getText());
				WinkelOk=((Math.abs(Achse-Herzwinkel)<10)||
							 (Math.abs((Achse-360)-Herzwinkel)<6)||
							 (Math.abs((Achse+360)-Herzwinkel)<6)   );
				if (WinkelOk) {
					 WinkelAnzeigen=true;
				}
				WinkelBeurteilen=true;
        } catch (NumberFormatException exception) {}
	 }


    private void NeuerHerzwinkel() {
		  if (((int)(Math.random()*10.0))==2) {
				Herzwinkel=(int)(Math.random()*360.0-150.0);
        }
		  else {
				Herzwinkel=(int)(Math.random()*150.0-30.0);
        }
		  WinkelAnzeigen=false;
		  WinkelBeurteilen=false;
		  AnzahlVersuche=0;
		  WinkelUser.setText("");
	 }



    public void WinkelZuXY(Point p,Point m,double Herzw,double r) {
		  int cx=getSize().width;
		  int xx=cx/7;
		  int yy=xx;
		  m.x=getSize().width/5;
		  m.y=getSize().height/4+50;
		  double pf=180.0/(Math.acos(0)*2.0);
		  p.x=m.x+(int)(Math.cos(Herzw/pf)*((double)xx)*r);
		  p.y=m.y+(int)(Math.sin(Herzw/pf)*((double)yy)*r);
	 }


    private void MaleEKG(Graphics g,
                         int x1,int y1,int x2,int y2,double a) {
		  double Propfaktor=1.0/512.0; // Dehnungsfaktor
		  int isoII=40;     // Lage der isoelektrische Linie
		  int dx=x2-x1;
		  if (dx>EinEKG.n) {
				dx=EinEKG.n;
		  }
		  int dy=y2-y1;
		  if ((dx<0)||(dy<0)) {
				return;
		  }
		  int y=dy/2;
		  y=y+y1;
		  g.setColor(Color.blue);
		  g.drawLine(x1,y,x2,y);
		  g.setColor(Color.black);
		  for(int x=1;x<dx;x++) {
				double ya=-(double)(EinEKG.Daten[x-1][2]-isoII);
				ya=ya*a*Propfaktor;
				ya=ya*dy;
				ya=ya+(dy/2);
				ya=ya+y1;
				double yb=-(double)(EinEKG.Daten[x][2]-isoII);
				yb=yb*a*Propfaktor;
				yb=yb*dy;
				yb=yb+(dy/2);
				yb=yb+y1;
				g.drawLine(x+x1-1,(int)ya,x+x1,(int)yb);
		  }
	 }







	 private void MaleDreieck(Graphics g) { // Male Dreieck
		  String Kommentar[]={"I","II","III","aVR","aVL","aVF"};
		  double pf=180.0/(Math.acos(0)*2.0);
		  double phiI=((double)Herzwinkel)/pf;
		  double a[]=new double[10];
		  a[0]=Math.cos(phiI); // I
		  a[5]=Math.sin(phiI); // aVF

		  double phiII=((double)(Herzwinkel-30))/pf;
		  a[2]=Math.sin(phiII); // II

		  double phiIII=((double)(-Herzwinkel+150))/pf;
		  a[1]=Math.sin(phiIII); // III

		  double phiaVR=((double)(-Herzwinkel+210))/pf;
		  a[3]=Math.cos(phiaVR); // aVR

		  double phiaVL=((double)(Herzwinkel+30))/pf;
		  a[4]=Math.cos(phiaVL);  // aVL

		  Font font=new Font("Helvetica",Font.BOLD,18);
		  g.setFont(font);

		  Point p=new Point();
		  Point md=new Point();
		  WinkelZuXY(p,md,-30,1.2);
		  double dy=((double)(getSize().height-p.y))/7.0;

		  for(int i=0;i<6;i++) {
				int y=(int)(((double)i)*dy+(getSize().height-p.y)/7);
				MaleEKG(g,
						  p.x,p.y+y,
						  getSize().width,p.y+y+(int)dy,
						  a[i]);
				g.drawString(Kommentar[i],
								 p.x,y+p.y+(int)(dy/2.0));
		  }
		
		  int xm,ym,x,y;
		  Point p1=new Point();
		  Point p2=new Point();
		  Point p3=new Point();
		  Point m=new Point();
		  // Dreieck:
		  g.setColor(Color.blue);
		  WinkelZuXY(p1,m,-30,1.2);
		  WinkelZuXY(p2,m,90,1.2);
		  g.drawLine(p1.x,p1.y,p2.x,p2.y);
		  WinkelZuXY(p3,m,-150,1.2);
		  g.drawLine(p2.x,p2.y,p3.x,p3.y);
		  g.drawLine(p1.x,p1.y,p3.x,p3.y);
		  // Beschriftung:
		  int dd=6;
		  WinkelZuXY(p1,m,-30,1.3);
		  g.drawString("L",p1.x-dd,p1.y);
		  WinkelZuXY(p1,m,-150,1.3);
		  g.drawString("R",p1.x-dd,p1.y);
		  WinkelZuXY(p1,m,-90,0.7);
		  g.drawString("I",p1.x-dd,p1.y);
		  WinkelZuXY(p1,m,30,0.8);
		  g.drawString("III",p1.x-dd,p1.y);
		  WinkelZuXY(p1,m,150,0.8);
		  g.drawString("II",p1.x-dd,p1.y);
		  WinkelZuXY(p1,m,90,1.4);
		  g.drawString("F",p1.x-dd,p1.y+dd);
		  if (WinkelAnzeigen) { // Pfeil
				g.setColor(Color.red);
				WinkelZuXY(p1,m,Herzwinkel,1);
				g.drawLine(m.x,m.y,p1.x,p1.y);
				WinkelZuXY(p2,m,Herzwinkel+5,0.9);
				WinkelZuXY(p3,m,Herzwinkel-5,0.9);
				g.drawLine(p1.x,p1.y,p3.x,p3.y);
				g.drawLine(p1.x,p1.y,p2.x,p2.y);
				WinkelZuXY(p3,m,90,1.5);
				g.drawString("  "+String.valueOf(Herzwinkel)+"°",0,p3.y);
        } // Pfeil

		  WinkelZuXY(p,md,90,1.6);

		  g.setColor(Color.red);
		  if (WinkelBeurteilen) {
				int ytext=p.y+(getSize().height-p.y)/2;
				if (WinkelOk) {
					 String[] tRichtig={"  JAA! Winkel stimmt!!!",
											  "  YES! The angle is ok!",
                                   "  JAA! Rätt vinkel!!!!!"};
					 g.drawString(tRichtig[l],1,ytext);
				}
				else {
					 String[] tFalsch={"  Daneben: nochmal!",
											 "  Sorry, once again!",
                                  "  Fel: försök igen!"};
					 g.drawString(tFalsch[l],1,ytext);
				}
        }
	 } // Dreieck





    public void paint(Graphics g) 
	 { // Paint
		  if (WinkelAnzeigen) {
				ok.setEnabled(false);
				WinkelUser.setEnabled(false);
		  }
		  else {
				ok.setEnabled(true);
				WinkelUser.setEnabled(true);
		  }
		  if (AnzahlVersuche>3) {
				Aufloesung.setVisible(true);
		  }
		  else {
				Aufloesung.setVisible(false);
		  }
		  MaleDreieck(g);
	 } // Paint
} // Graph applet


