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


public class bvu3 extends Applet implements Runnable,ItemListener,ActionListener
{
	//x and y positions of Uteged relative to the gravity well
	int positieX[]=new int[740];
	int positieY[]=new int[740];
	//constants to correct for the used coordinate system
	final int offsetX=250;
	final int offsetY=117;
	final int DEFAULTINCREMENT=2;
	final int iNumberOfFrames=14;
	Thread m_AppletThread;
	// day counters
	int m_nValue;
	int oldValue;
	int veryOldValue;
	boolean blnPicturechanged;
	boolean eerstekeer;
	boolean wait;
	boolean blnSynchronize=false;
	int increment=DEFAULTINCREMENT;
	//enumerations for the diagrams
	final int iPrecipitation=2;
	final int iWinds=1;
	final int iTemperature=0;
	final int iWinter=0;
	final int iLazasSpring=1;
	final int iGreatSummer=2;
	final int iLazimSpring=3;
	final int iSummer=4;
	final int iLazimAutumn=5;
	final int iLazasAutumn=6;
	//enumerations for the language and initial language
	final int iEnglish=0;
	final int iNederlands=1;
	int iLanguage=iEnglish;
	//initial season and diagram
	int iKind;
	int iSeason=iWinter;
	//arrays holding the Dutch and English label names
	String strDiagram[][]=new String[2][3];
	String strSeasons[][]=new String[2][7];
	String strSunUp[]=new String[2];
	String strSunDown[]=new String[2];
	String strDay;
	String strTitelZonnestand;
	String strTitelKlimaat;
	final double conversionRadDeg=180/Math.PI;
	Label label1 = new Label();
	Label lblSeizoen;
	Label lblDay;
	Label lblSunUpLazim;
	Label lblSunDownLazim;
	Label lblSunUpLazas;
	Label lblSunDownLazas;
	Image Uteged[];
	Image Lazas;
	Image Lazim;
	Image imgDiagram[][];
	Image imgPrecipitation;
	Image imgLegenda[];
	Image imgOffScreen;
	Graphics go;
	CheckboxGroup cbgDiagram;
	Checkbox[] cbKeuze;
	CheckboxGroup cbgWeergave;
	Checkbox[] cbWeergave;
	//Panels
		// the three main Panels
			Panel pnlTop;
			Panel pnlMiddle;
			Panel pnlBottom;
		//
		//the sub Panels
			// Panels of the Climate graphs
				Panel pnlDiagram;
				Panel pnlKeuzeKnoppen;
			//
			//Panels of the Orbit
				Panel pnlBaanvak;
				Panel pnlLabels;
			//
		//
	//
	//Buttons
		Button btnPlay;
		Button btnReset;
	//
	//Canvases
		BaanvakCanvas cvsBaanvak;
		DiagramCanvas cvsDiagram;
	//
	//The Sunrise and Sunset subapplet
		Zonnestand zonnestand=new Zonnestand();
	//
	//The class holding the orbit of Uteged
		Baan baan=new Baan();
	//
	// The class loading the climategraphs
		Climateloader clLoader=new Climateloader();
	//
	public class Climateloader implements Runnable
	{
		Thread LoaderThread;
		public void start()
		{
			if(LoaderThread==null)
			{
				LoaderThread=new Thread(this);
				LoaderThread.start();
			}
		}
	
		public void stop()
		{
			if(m_AppletThread!=null)
			{
				LoaderThread.stop();
				LoaderThread=null;
			}
		}
		public void run()
		{

			imgDiagram[iPrecipitation][iWinter]=waitForImage("WinterBprecipitation.jpg");
			imgDiagram[iPrecipitation][iLazasSpring]=waitForImage("autumnBprecipitation.jpg");
			imgDiagram[iPrecipitation][iLazasAutumn]=imgDiagram[iPrecipitation][iLazasSpring];
			imgDiagram[iPrecipitation][iGreatSummer]=waitForImage("greatsummerprecipitation.jpg");
			imgDiagram[iPrecipitation][iLazimSpring]=waitForImage("autumnAprecipitation.jpg");
			imgDiagram[iPrecipitation][iLazimAutumn]=imgDiagram[iPrecipitation][iLazimSpring];
			imgDiagram[iPrecipitation][iSummer]=waitForImage("SummerAprecipitation.jpg");
			imgDiagram[iTemperature][iWinter]=waitForImage("Bwintertemperatuur.jpg");
			imgDiagram[iTemperature][iLazasSpring]=waitForImage("herfstBtemperatuur.jpg");
			imgDiagram[iTemperature][iLazasAutumn]=imgDiagram[iTemperature][iLazasSpring];
			imgDiagram[iTemperature][iGreatSummer]=waitForImage("grotezomertemperatuur.jpg");
			imgDiagram[iTemperature][iLazimSpring]=waitForImage("herfstAtemperatuur.jpg");
			imgDiagram[iTemperature][iLazimAutumn]=imgDiagram[iTemperature][iLazimSpring];
			imgDiagram[iTemperature][iSummer]=waitForImage("zomerAtemperatuur.jpg");
			imgDiagram[iWinds][iWinter]=waitForImage("Bwinterwinden.jpg");
			imgDiagram[iWinds][iLazasSpring]=waitForImage("herfstBwinden.jpg");
			imgDiagram[iWinds][iLazasAutumn]=imgDiagram[iWinds][iLazasSpring];
			imgDiagram[iWinds][iGreatSummer]=waitForImage("grotezomerwinden.jpg");
			imgDiagram[iWinds][iLazimSpring]=waitForImage("herfstAwinden.jpg");
			imgDiagram[iWinds][iLazimAutumn]=imgDiagram[iWinds][iLazimSpring];
			imgDiagram[iWinds][iSummer]=waitForImage("zomerAwinden.jpg");	
			imgLegenda[iPrecipitation]=waitForImage("legendaneerslag.jpg");
			imgLegenda[iTemperature]=waitForImage("legendaklimaat.jpg");
			imgLegenda[3]=waitForImage("legendazomer.jpg");
			try
			{
				Thread.sleep(10000);
			}
			catch(InterruptedException e)
			{}
			cbWeergave[0].setVisible(true);

		}
		
	}
	public class DiagramCanvas extends Canvas
	{
		Dimension dimParent;
		int intPictureWidth;
		public void DiagramCanvas()
		{}
		
		public void update(Graphics g)
		{
			//draw the selected diagram
			dimParent=getParent().getPreferredSize();
			intPictureWidth=imgDiagram[iKind][iSeason].getWidth(this);
			g.drawImage(imgDiagram[iKind][iSeason],(dimParent.width-intPictureWidth)/2,0,this);
			//draw legenda
			//great summer has differemt legenda for temperature
			if((iKind==iTemperature)&&(iSeason==iGreatSummer))
			{
				g.drawImage(imgLegenda[3],(dimParent.width-intPictureWidth)/2+intPictureWidth,0,this);
			}
			else if(iKind==iWinds)//no legenda yet
			{
				g.clearRect((dimParent.width-intPictureWidth)/2+intPictureWidth,0,80,85);
			}
			else 
			{
				g.drawImage(imgLegenda[iKind],(dimParent.width-intPictureWidth)/2+intPictureWidth,0,this);
			}
		}
		
		public void paint(Graphics g)
		{
			update(g);
		}
	}
	public class BaanvakCanvas extends Canvas
	{
		double tangentLazim;
		double dblArcLazim;
		int intArcLazim;
		double tangentLazas;
		double dblArcLazas;
		int intArcLazas;
		int intArcLazas2;
		int intArcOverlap;
		int intArcOverlapStart;
		int intArcDarkStart;
		int intArcDark;
		int x;
		int intFrame;
		int intOldFrame;
		public void update(Graphics g)
		{	
			if (eerstekeer==true)
			{
				intFrame=0;
				intOldFrame=intFrame;
				g.setColor(Color.black);
				g.fillRect(0,0,500,235);
				g.drawImage(Lazim,offsetX-100-40,offsetY-40,80,80,this);
				g.drawImage(Lazas,offsetX+100-40,offsetY-40,80,80,this);
				eerstekeer=false;
				calculateSunlight();
				
			}
			if ((!wait)&&blnSynchronize) calculateSunlight();
			if ((veryOldValue % 8<=1)&&!wait)
			{			
				g.setColor(Color.blue);
				g.fillOval(offsetX+baan.positieX[veryOldValue]-3,offsetY-baan.positieY[veryOldValue]-3,6,6);
			}
			if (blnSynchronize)
			{
				if(baan.positieY[m_nValue]<0)
				{
					intFrame=(int)(iNumberOfFrames*zonnestand.dblUurLazas/zonnestand.DAG);
				}
				else
				{
					intFrame=(int)(iNumberOfFrames*zonnestand.dblUurLazim/zonnestand.DAG);
				}
			}
			else
			{
				intFrame++;
			}
			if (intFrame>=iNumberOfFrames) intFrame=0;
			if(intFrame<0)intFrame+=iNumberOfFrames;
			if (intFrame!=intOldFrame)
			{
				go.setColor(Color.black);
				go.fillRect(0,0,60,60);
				go.drawImage(Uteged[intFrame],0,0,this);
				if (blnSynchronize)drawSunlight();
			}
			intOldFrame=intFrame;
			g.clearRect(offsetX+baan.positieX[oldValue]-30,offsetY-baan.positieY[oldValue]-30,60,
						60);
			g.drawImage(imgOffScreen,offsetX+baan.positieX[m_nValue]-30,
						offsetY-baan.positieY[m_nValue]-30,this);	
		}
		public void calculateSunlight()
		{
			tangentLazim=Math.atan2(baan.positieY[m_nValue],baan.positieX[m_nValue]+100);
			dblArcLazim=tangentLazim*conversionRadDeg;
			intArcLazim=(int)dblArcLazim+90;
			tangentLazas=Math.atan2(baan.positieY[m_nValue],baan.positieX[m_nValue]-100);
			dblArcLazas=tangentLazas*conversionRadDeg;
			intArcLazas=(int)dblArcLazas+90;
			if (intArcLazim>=intArcLazas)
			{
				intArcOverlapStart=intArcLazim-90;
				intArcOverlap=180-intArcLazim+intArcLazas;
				intArcDarkStart=intArcLazim+180;
				intArcDark=360-intArcDarkStart+intArcLazas;
			}
			else
			{
				intArcOverlapStart=intArcLazas-90;
				intArcOverlap=180-intArcLazas+intArcLazim;
				intArcDarkStart=intArcLazas+180;
				intArcDark=360-intArcDarkStart+intArcLazim;
				if(intArcLazas<180)
				{
					intArcLazas2=intArcLazas+180;
				}
				else
				{
					intArcLazas2=intArcLazas-180;
				}
			}
		}
		public void drawSunlight()
		{
					go.setColor(Color.darkGray);
				for(x=0;x<=3;x++)
				{
					go.drawArc(x,x,58-2*x,58-2*x,intArcDarkStart,intArcDark);
					go.drawArc(x,x+1,58-2*x,58-2*x,intArcDarkStart,intArcDark);
				}	
				go.setColor(Color.yellow);
				for(x=0;x<=3;x++)
				{
					go.drawArc(x,x,58-2*x,58-2*x,intArcLazim,180);
					go.drawArc(x,x+1,58-2*x,58-2*x,intArcLazim,180);
				}
				go.setColor(Color.red);
				for(x=0;x<=3;x++)
				{
					go.drawArc(x,x,58-2*x,58-2*x,intArcLazas,180);
					go.drawArc(x,x+1,58-2*x,58-2*x,intArcLazas,180);
				}
				if ((m_nValue!=185)&&(m_nValue!=555))
				{			
					go.setColor(new Color(255,128,0));//dark orange
					for(x=0;x<=3;x++)
					{
						go.drawArc(x,x,58-2*x,58-2*x,intArcOverlapStart+90,intArcOverlap);
						go.drawArc(x,x+1,58-2*x,58-2*x,intArcOverlapStart+90,intArcOverlap);
					}
				}
		}
		public void paint(Graphics g)
		{
			update(g);
		}
	}
	public void init()
	{
		usePageParams();
		initLabels();
		baan.init();
		initImages();
		imgDiagram=new Image[3][7];
		imgLegenda=new Image[4];
		clLoader.start();
		wait=true;
		imgOffScreen=createImage(60,60);
		go=imgOffScreen.getGraphics();
		m_AppletThread=null;
		//Default graph shows temperature
		iKind=iTemperature;
		eerstekeer=true;
		m_nValue=1;
		//initialisation of the canvases
		this.setBackground(Color.black);
		pnlBaanvak=new Panel();
		pnlDiagram=new Panel();
		pnlKeuzeKnoppen=new Panel();
		pnlKeuzeKnoppen.setBackground(Color.black);
		pnlKeuzeKnoppen.setForeground(Color.white);
		cvsBaanvak=new BaanvakCanvas();
		cvsDiagram=new DiagramCanvas();
		pnlBaanvak.setLayout(new BorderLayout());
		pnlDiagram.setLayout(new BorderLayout());
		cvsBaanvak.setSize(510,235);
		pnlBaanvak.add(cvsBaanvak,BorderLayout.CENTER);
		cvsDiagram.setSize(575,225);
		lblSeizoen=new Label(strSeasons[iLanguage][iSeason]);
		lblSeizoen.setAlignment(FlowLayout.CENTER);
		pnlDiagram.add(lblSeizoen,BorderLayout.NORTH);
		pnlDiagram.add(cvsDiagram,BorderLayout.CENTER);
		//initialisation of the radio buttons to choose the diagrams
		
		//initialize the checkboxes (gouped)
		cbgDiagram=new CheckboxGroup();
		cbKeuze=new Checkbox[3];
		cbKeuze[iTemperature]=new Checkbox(strDiagram[iLanguage][iTemperature],cbgDiagram,true);
		cbKeuze[iTemperature].addItemListener(this);
		pnlKeuzeKnoppen.add(cbKeuze[iTemperature]);
		cbKeuze[iWinds]=new Checkbox(strDiagram[iLanguage][iWinds],cbgDiagram,false);
		cbKeuze[iWinds].addItemListener(this);
		pnlKeuzeKnoppen.add(cbKeuze[iWinds]);
		cbKeuze[iPrecipitation]=new Checkbox(strDiagram[iLanguage][iPrecipitation],cbgDiagram,false);
		cbKeuze[iPrecipitation].addItemListener(this);
		pnlKeuzeKnoppen.add(cbKeuze[iPrecipitation]);
		pnlDiagram.add(pnlKeuzeKnoppen,BorderLayout.SOUTH);
		cbgWeergave=new CheckboxGroup();
		cbWeergave=new Checkbox[2];
		cbWeergave[0]=new Checkbox(strTitelKlimaat,cbgWeergave,false);
		cbWeergave[1]=new Checkbox(strTitelZonnestand,cbgWeergave,true);
		cbWeergave[0].addItemListener(this);
		cbWeergave[1].addItemListener(this);
		//initiate the Buttons
		btnPlay=new Button("Start");
		btnReset=new Button("Reset");
		btnPlay.addActionListener(this);
		btnReset.addActionListener(this);
		pnlBaanvak.add(btnPlay,BorderLayout.EAST);
		pnlBaanvak.add(btnReset,BorderLayout.WEST);
		pnlLabels=new Panel(new GridLayout(1,5));
		pnlLabels.setBackground(Color.black);
		pnlLabels.setForeground(Color.white);
		lblDay=new Label(strDay);
		pnlLabels.add(lblDay);
		pnlLabels.add(lblSunUpLazim=new Label(strSunUp[zonnestand.con.LAZIM]));
		pnlLabels.add(lblSunDownLazim=new Label(strSunDown[zonnestand.con.LAZIM]));
		pnlLabels.add(lblSunUpLazas=new Label(strSunDown[zonnestand.con.LAZAS]));
		pnlLabels.add(lblSunDownLazas=new Label(strSunUp[zonnestand.con.LAZAS]));
		pnlBaanvak.add(pnlLabels,BorderLayout.SOUTH);
		// default Climate graph
		imgPrecipitation=imgDiagram[iPrecipitation][iWinter];
		buildPanels();
		setVisibleLabels(false);
		
				pnlBottom.remove(pnlDiagram);
				pnlBottom.add(zonnestand);
				blnSynchronize=true;
				setVisibleLabels(true);
				zonnestand.intDag=m_nValue+1;
				zonnestand.validate();
	}
	
	public void start()
	{
		if(m_AppletThread==null)
		{
			m_AppletThread=new Thread(this);
			m_AppletThread.start();
		}
	}
	
	public void stop()
	{
		if(m_AppletThread!=null)
		{
			m_AppletThread.stop();
			m_AppletThread=null;
		}
	}
	
	public void run()
	{
		cbWeergave[0].setVisible(false);
		
		while(true)
		{
			try
			{
				cvsBaanvak.repaint();
				checkDiagram(iKind,iSeason);
				oldValue=m_nValue;
				veryOldValue=oldValue-32;
				if (veryOldValue<1) veryOldValue=740+veryOldValue;
				if (blnSynchronize)
				{
					if(((m_nValue>740-zonnestand.intSeizoenIncrement)&&
					   (zonnestand.intDag<zonnestand.intSeizoenIncrement))||
					   (Math.abs(m_nValue-zonnestand.intDag)>increment))
					{
						m_nValue+=increment;
					}
				}
				else if(!wait)
				{
					m_nValue+=increment;

				}
				if (m_nValue>740)
				{
					m_nValue-=740;
				}
				lblDay.setText(strDay+m_nValue);
				Thread.sleep(150);
			}
			catch (InterruptedException e)
			{
				stop();
			}
		}
	}

	
	private	final String labelParam = "label";
	private	final String backgroundParam = "background";
	private	final String foregroundParam = "foreground";
	private final String languageParam="language";

	/**
	 * Reads parameters from the applet's HTML host and sets applet
	 * properties.
	 */
	private void usePageParams()
	{
		final String defaultLabel = "Default label";
		final String defaultBackground = "C0C0C0";
		final String defaultForeground = "000000";
		String labelValue;
		String backgroundValue;
		String foregroundValue;
		String languageValue;


		labelValue = getParameter(labelParam);
		backgroundValue = getParameter(backgroundParam);
		foregroundValue = getParameter(foregroundParam);
		languageValue=getParameter(languageParam);

		if ((labelValue == null) || (backgroundValue == null) ||
			(foregroundValue == null))
		{
			/**
			 * There was something wrong with the HTML host tags.
			 * Generate default values.
			 */
			labelValue = defaultLabel;
			backgroundValue = defaultBackground;
			foregroundValue = defaultForeground;
			iLanguage=iEnglish;
		}

		/**
		 * Set the applet's string label, background color, and
		 * foreground colors.
		 */
		label1.setText(labelValue);
		label1.setBackground(stringToColor(backgroundValue));
		label1.setForeground(stringToColor(foregroundValue));
		if(languageValue.equals("Nederlands"))
		{
			iLanguage=iNederlands;
		}
		else
		{
			iLanguage=iEnglish;
		}
		this.setForeground(stringToColor(foregroundValue));
	}

	/**
	 * Converts a string formatted as "rrggbb" to an awt.Color object
	 */
	private Color stringToColor(String paramValue)
	{
		int red;
		int green;
		int blue;

		red = (Integer.decode("0x" + paramValue.substring(0,2))).intValue();
		green = (Integer.decode("0x" + paramValue.substring(2,4))).intValue();
		blue = (Integer.decode("0x" + paramValue.substring(4,6))).intValue();

		return new Color(red,green,blue);
	}

	/**
	 * External interface used by design tools to show properties of an applet.
	 */
	public String[][] getParameterInfo()
	{
		String[][] info =
		{
			{ labelParam, "String", "Label string to be displayed" },
			{ backgroundParam, "String", "Background color, format \"rrggbb\"" },
			{ foregroundParam, "String", "Foreground color, format \"rrggbb\"" },
		};
		return info;
	}
	
	public Image waitForImage(String filename)
	{
		Image image;
		MediaTracker tracker=new MediaTracker(this);
		image=getImage(getCodeBase(),filename);
		tracker.addImage(image,0);
		try
		{
			tracker.waitForID(0);
		}
		catch (InterruptedException e)
		{}
		return image;
	}
	
	public void checkDiagram(int iKind,int loc_iSeason)
	{
		blnPicturechanged=false;
			switch ((int) (m_nValue/92))
		{
			case 0:
				if(m_nValue % 92<=increment)
				{
					//lblSeizoen.setText("Northern winter");
					if(loc_iSeason!=iWinter)
					{
						iSeason=iWinter;
						blnPicturechanged=true;
					}
				}
				break;
			case 1:
				if(m_nValue % 92<=increment)
				{
					//lblSeizoen.setText("Lazas spring");
					if(loc_iSeason!=iLazasSpring)
					{
						iSeason=iLazasSpring;
						blnPicturechanged=true;
					}
				}
				break;
			case 2:
				if(m_nValue % 92<=increment)
				{
					//lblSeizoen.setText("Great summer");
					if(loc_iSeason!=iGreatSummer)
					{
						iSeason=iGreatSummer;
						blnPicturechanged=true;
					}				}
				break;
			case 3:
				if(m_nValue % 92<=increment)
				{
					//lblSeizoen.setText("Lazìm spring");
					if(loc_iSeason!=iLazimSpring)
					{
						iSeason=iLazimSpring;
						blnPicturechanged=true;
					}			
				}
				break;
			case 4:
				if(m_nValue % 92<=increment)
				{
					if(loc_iSeason!=iSummer)
					{
						iSeason=iSummer;
						blnPicturechanged=true;
					}
				}
				break;
			case 5:
				if(m_nValue % 92<=increment)
				{
					if(loc_iSeason!=iLazimAutumn)
					{
						iSeason=iLazimAutumn;
						blnPicturechanged=true;
					}
				}
				break;
			case 6:
				if(m_nValue % 92<=increment)
				{
					if(loc_iSeason!=iGreatSummer)
					{
						iSeason=iGreatSummer;
						blnPicturechanged=true;
					}
				}
				break;			
			case 7:
				if(m_nValue % 92<=increment)
				{
					if(loc_iSeason!=iLazasAutumn)
					{
						iSeason=iLazasAutumn;
						blnPicturechanged=true;
					}					
				}
				break;
		}
		if(blnPicturechanged)
		{
			blnPicturechanged=false;
			imgPrecipitation=imgDiagram[iKind][iSeason];
			lblSeizoen.setText(strSeasons[iLanguage][iSeason]);
			cvsDiagram.repaint();
		}
	}
	public void itemStateChanged(ItemEvent e)
	{	
		if((e.getSource()==cbWeergave[0])||(e.getSource()==cbWeergave[1]))
		{
			//User chose Sunrise and Sunset
			if (cbWeergave[1].getState()==true)
			{
				pnlBottom.remove(pnlDiagram);
				pnlBottom.add(zonnestand);
				blnSynchronize=true;
				setVisibleLabels(true);
				zonnestand.intDag=m_nValue+1;
				zonnestand.validate();
			}
			//User chose Climate graphs
			else 
			{
				pnlBottom.remove(zonnestand);
				pnlBottom.add(pnlDiagram);
				blnSynchronize=false;
				setVisibleLabels(false);
			}
		}
		else
		{
			//User chose one of the Climate graphs: Temperature,Precipitation or Winds
			if(e.getSource()==cbKeuze[iTemperature])
			{
				if (cbKeuze[iTemperature].getState()==true)
				{
					iKind=iTemperature;
				}
			}
			else if(e.getSource()==cbKeuze[iPrecipitation])
			{
				if(cbKeuze[iPrecipitation].getState()==true)
				{
					iKind=iPrecipitation;
				}
			}
			else if(e.getSource()==cbKeuze[iWinds])
			{
				if(cbKeuze[iWinds].getState()==true)
					iKind=iWinds;
			}
			imgPrecipitation=imgDiagram[iKind][iSeason];
			cvsDiagram.repaint();
		}
	}
	public void actionPerformed(ActionEvent e)
	{
		//this method handles the button click event and passes them on
		//to the zonnestand applet
		if (e.getSource()==btnPlay)
		{
			//Pause the Applet
			if (btnPlay.getLabel()=="Pause")
			{
				btnPlay.setLabel("Start");
				wait=true;
				zonnestand.handleEvent(zonnestand.con.STOP);
			}
			else
			{
				//Start,resume the Applet
				btnPlay.setLabel("Pause");
				wait=false;
				eerstekeer=true;
				lblSeizoen.setText(strSeasons[iLanguage][iSeason]);
				cvsBaanvak.repaint();
				zonnestand.handleEvent(zonnestand.con.START);
			}
		}
		else //reset button was pressed
		{
			stop();
			eerstekeer=true;
			m_nValue=1;
			iKind=iTemperature;
			iSeason=iWinter;
			cbKeuze[iTemperature].setState(true);
			lblSeizoen.setText(strSeasons[iLanguage][iSeason]);			
			cvsDiagram.repaint();
			zonnestand.handleEvent(zonnestand.con.RESET);
			start();
		}
			
	}
	public void initImages()
	{
		//Wait for the images to load
		
		Lazim=waitForImage("Lazim.gif");
		Lazas=waitForImage("Lazas.gif");

		Uteged=new Image[iNumberOfFrames];
			for(int x=1;x<=iNumberOfFrames;x++)
			{
				Uteged[iNumberOfFrames-x]=waitForImage("Sprite"+x+".gif");
			}
	}

	public void initLabels()
	{
		//Initiate the labels of the different labels, choices and checkboxes.
		//Some labels are directly initiated in Dutch or English in this method,
		//others are inititated  in the init() method using label=new label(stringvariable[language][labeltext]),
		//the 'stringvariable's being initiated below.
		strDiagram[iNederlands][iTemperature]="Temperatuur";
		strDiagram[iNederlands][iPrecipitation]="Neerslag";
		strDiagram[iNederlands][iWinds]="Winden";
		strDiagram[iEnglish][iTemperature]="Temperature";
		strDiagram[iEnglish][iPrecipitation]="Precipitation";
		strDiagram[iEnglish][iWinds]="Winds";		
		strSeasons[iNederlands][iWinter]="Noordelijke winter";
		strSeasons[iNederlands][iLazasSpring]="Lazas lente";
		strSeasons[iNederlands][iGreatSummer]="Grote zomer";
		strSeasons[iNederlands][iLazimSpring]="Lazìm lente";
		strSeasons[iNederlands][iLazimAutumn]="Lazìm herfst";
		strSeasons[iNederlands][iLazasAutumn]="Lazas herfst";
		strSeasons[iNederlands][iSummer]="Noordelijke zomer";
		strSeasons[iEnglish][iWinter]="Northern winter";
		strSeasons[iEnglish][iLazasSpring]="Lazas spring";
		strSeasons[iEnglish][iGreatSummer]="Great summmer";
		strSeasons[iEnglish][iLazimSpring]="Lazìm spring";
		strSeasons[iEnglish][iLazimAutumn]="Lazìm autumn";
		strSeasons[iEnglish][iLazasAutumn]="Lazas autumn";
		strSeasons[iEnglish][iSummer]="Northern summer";
		if(iLanguage==iEnglish)
		{
			strTitelZonnestand="Sunrise and Sunset";
			strTitelKlimaat="Climate graphs";
			strDay="Day: ";
			strSunUp[zonnestand.con.LAZIM]="Lazim sunrise: ";
			strSunDown[zonnestand.con.LAZIM]="Lazim sunset: ";
			strSunDown[zonnestand.con.LAZAS]="Lazas sunset: ";
			strSunUp[zonnestand.con.LAZAS]="Lazas sunrise: ";
		}
		else
		{
			strTitelZonnestand="Zonnestanden";
			strDay="Dag: ";
			strTitelKlimaat="Klimaatgrafieken";
			strSunUp[zonnestand.con.LAZIM]="Lazim op: ";
			strSunDown[zonnestand.con.LAZIM]="Lazim onder: ";
			strSunDown[zonnestand.con.LAZAS]="Lazas onder: ";
			strSunUp[zonnestand.con.LAZAS]="Lazas op: ";
		}
		
	}
	public void buildPanels()
	{
		//** Here the main panels are built. The Top Panel holds the Orbit Graph
		//** The middle panel holds the radio buttons to choose between the Sunrise 
		//** and sunset graph or the climate graphs and the Bottom Panel holds either
		//** the Sunrise/sunset Applet(default) or the climate graphs
		final int TOPHEIGHT=270;//TOPWIDTH is used for all three panels.
		final int TOPWIDTH=578;
		final int MIDDLEHEIGHT=25;
		final int BOTTOMHEIGHT=zonnestand.getPreferredSize().height;
		this.setLayout(null);
		//build the top panel
		pnlTop=new Panel(null);
		add(pnlTop);
		pnlTop.setBounds(0,0,TOPWIDTH,TOPHEIGHT);
		pnlTop.add(pnlBaanvak);
		pnlBaanvak.setBounds(0,0,TOPWIDTH,TOPHEIGHT);
		//build the middle panel
		pnlMiddle=new Panel();
		pnlMiddle.setBackground(Color.black);
		pnlMiddle.setForeground(Color.white);
		add(pnlMiddle);
		pnlMiddle.setBounds(0,TOPHEIGHT+1,TOPWIDTH,MIDDLEHEIGHT);
		pnlMiddle.add(cbWeergave[1]);
		pnlMiddle.add(cbWeergave[0]);
		//build the lower panel
		pnlBottom=new Panel(null);
		add(pnlBottom);
		pnlBottom.setBounds(0,TOPHEIGHT+MIDDLEHEIGHT+1,TOPWIDTH,BOTTOMHEIGHT);
		pnlBottom.add(zonnestand);
		zonnestand.setBounds(0,0,TOPWIDTH,BOTTOMHEIGHT);
		pnlDiagram.setBounds(0,0,TOPWIDTH,BOTTOMHEIGHT);
		zonnestand.init();
		zonnestand.start();
		pnlBottom.add(pnlDiagram);
		pnlBottom.remove(zonnestand);
	}
	
	public class Zonnestand extends Applet implements Runnable
	{
	//Applet showing the Sunrises and Sunsets, the user can choose to see this Applet,
	//or to see the Climate graphs in the bottom Panel.
	final double  pi=Math.PI;
	final double convertDegRad=pi/180;
	final double convertRadDeg=180/pi;
	final int offsetY=200;
	final int offsetX=500;
	Enumeration con=new Enumeration();
	final int LAZASxCOORDINATE=100;
	final int LAZIMxCOORDINATE=-100;
	final int MAXDISTANCE=320;
	final int DEFAULTRADIUS=10;
	final double dblAlpha=30*convertDegRad;
	double dblBreedte=0;	
	double dblUurhoek[];
	double dblDeclinatie[];
	double dblZonnehoogte[];
	double dblLagLazim;
	int DAG=24;
	double INCREMENT=(double)DAG/96;
	int intDag=0;//-con.SEIZOEN/4;
	int intRadius[]=new int[2];
	int intLanguage;
	int intSeizoenIncrement=con.SEIZOEN/4;
	Thread m_AppletThread;
	Label label1=new Label();
	Label label2;
	Label label3=new Label();
	Label label4=new Label();
	Choice chBreedtegraad;
	Choice chUurIncrement;
	Choice chDagIncrement;
	double dblSeizoen[];
	double dblUurLazas=DAG;
	double dblUurLazim=DAG;
	double dblHourChange=0;
	Panel pnlPanel=new Panel();
	Panel pnlUren=new Panel();
	Panel pnlChoices=new Panel();
	Panel pnlTekenen=new Panel();
	ZonnenCanvas cvsCanvas=new ZonnenCanvas();
	UrenCanvas cvsUrenCanvas=new UrenCanvas();
	Image offScreenSky;
	Image offScreenUren;
	Graphics go;
	Graphics gUren;
	FontMetrics fm;
	AppletListener ilAppletListener=new AppletListener();
	String strSpeed[][];
	String strLabels[][];
	String strSeizoen[][];
	String strEtmaal[][];
	CheckboxGroup cbgEtmaal=new CheckboxGroup();
	Checkbox cb12;
	Checkbox cb24;
	Checkbox cbZonnestand;
	int intEtmaal=con.VIERENTWINTIG;
	Dimension dimSizeZonnestand=new Dimension(500,280);
	boolean blnWait=true;
	
	public Zonnestand()
	{}

	public Dimension getPreferredSize()
	{
		return dimSizeZonnestand;
	}
	public Dimension getMinimumSize()
	{
		return dimSizeZonnestand;
	}
	public class ZonnenCanvas extends Canvas
	{	
		//main canvas in which the suns are drawn
		int intZonnehoogte[]=new int[2];
		int intUur[]=new int[2];
		int intNewX[]=new int[2];
		int intOldX[]=new int[2];
		int intNewY[]=new int[2];
		int intOldY[]=new int[2];
		int intOldRadius[]=new int[2];
		boolean eerstekeer=true;
		boolean blnSunUp[]=new boolean[2];
		Color colBackground;
		int red,green,blue;
		Dimension dimSize=new Dimension(500,200);
		int intMagnification[]=new int[2];

		public void ZonnenCanvas()
		{}
		
		public Dimension getPreferredSize()
		{
			return dimSize;
		}
		public Dimension getMinimumSize()
		{
			return dimSize;
		}
		public void update(Graphics g)
		{
			paint(g);
		}
			
		public void paint(Graphics g)
		{	
			if (eerstekeer==true)
			{
				eerstekeer=false;
				intRadius[con.LAZIM]=radius(220,0,LAZIMxCOORDINATE);
				intRadius[con.LAZAS]=radius(220,0,LAZASxCOORDINATE);
			}
			// the following section calculates the backgrounc color taking into account
			// the height of the suns in the sky and their visible radius.
			blue=0;
			if ((dblZonnehoogte[con.LAZIM]>=-intRadius[con.LAZIM]-1)&&dblUurLazim>=0)
			{
				intZonnehoogte[con.LAZIM]=(int)dblZonnehoogte[con.LAZIM];
				green=255*intZonnehoogte[con.LAZIM]/90;
				green+=255*intRadius[con.LAZIM]/90+1;
				if(green>255) green=255;
				intMagnification[con.LAZIM]=(int)(intRadius[con.LAZIM]*intRadius[con.LAZIM]
					/DEFAULTRADIUS/DEFAULTRADIUS);
				blue=(25*intZonnehoogte[con.LAZIM]/90)*intMagnification[con.LAZIM];
				blue+=(25*intRadius[con.LAZIM]/90)*intMagnification[con.LAZIM];
			}
			else
			{
				intZonnehoogte[con.LAZIM]=-100;
				green=0;
			}
			if((dblZonnehoogte[con.LAZAS]>=-intRadius[con.LAZAS]-1)&&dblUurLazas>=0)
			{
				intZonnehoogte[con.LAZAS]=(int)dblZonnehoogte[con.LAZAS];
				red=255*intZonnehoogte[con.LAZAS]/90;
				red+=255*intRadius[con.LAZAS]/90+1;
				if(red>255) red=255;
				intMagnification[con.LAZAS]=(int)(intRadius[con.LAZAS]*intRadius[con.LAZAS]
					/DEFAULTRADIUS/DEFAULTRADIUS);
				blue+=(25*intZonnehoogte[con.LAZAS]/90)*intMagnification[con.LAZAS];
				blue+=(25*intRadius[con.LAZAS]/90)*intMagnification[con.LAZAS];
			}
			else
			{
				intZonnehoogte[con.LAZAS]=-100;
				red=0;
			}
			if(blue>255) blue=255;
			colBackground=new Color(red,green,blue);
			go.setColor(colBackground);
			go.fillRect(0,0,500,200);
			//
			//change the hour into the x coordinatee at which the sun will be drawn
			dblUurLazas=con.UURAFSTAND*dblUurLazas;
			intUur[con.LAZAS]=(int)dblUurLazas;
			dblUurLazas=dblUurLazas/con.UURAFSTAND;
			dblUurLazim=con.UURAFSTAND*dblUurLazim;
			intUur[con.LAZIM]=(int)dblUurLazim;
			dblUurLazim=dblUurLazim/con.UURAFSTAND;
			//
			//Draw the hours in the Canvas.
			go.setColor(Color.white);
			for(int x=0;x<=DAG;x++)
			{
				go.drawLine(offsetX-con.UURAFSTAND*x,offsetY,offsetX-con.UURAFSTAND*x,offsetY-5);
			}
			//
			//Draw the suns
			if ((intDag>185)&&(intDag<555))
			{
				tekenSter(con.LAZAS,intUur[con.LAZAS],Color.red);
				tekenSter(con.LAZIM,intUur[con.LAZIM],Color.yellow);
			}
			else
			{
				tekenSter(con.LAZIM,intUur[con.LAZIM],Color.yellow);
				tekenSter(con.LAZAS,intUur[con.LAZAS],Color.red);
			}
			// the Remarked out section below showed the Stars in the Zonnestand Canvas
			//go.setColor(Color.yellow);
			//go.fillOval((offsetX+LAZIMxCOORDINATE)/2-5,offsetY-50-5,10,10);
			//go.setColor(Color.red);
			//go.fillOval((offsetX+LAZASxCOORDINATE)/2-5,offsetY-50-5,10,10);
			//go.setColor(Color.green);
			//go.fillOval((offsetX+baan.positieX[intDag])/2,offsetY-50-baan.positieY[intDag]/2,5,5);
			g.drawImage(offScreenSky,0,0,this);
		}
		public void tekenSter(int ster,int Uur,Color color)
		{
			//Draws the suns
			intNewX[ster]=offsetX-intRadius[ster]/2-Uur;
			intNewY[ster]=offsetY-intZonnehoogte[ster]*2-intRadius[ster]/2;
			if(intNewY[ster]>offsetY-intRadius[ster]/2) return;
			go.setColor(color);
			go.fillOval(intNewX[ster],intNewY[ster],intRadius[ster],intRadius[ster]);
			intOldX[ster]=intNewX[ster];
			intOldY[ster]=intNewY[ster];
			intOldRadius[ster]=intRadius[ster];
		}
	}
	public class UrenCanvas extends Canvas
	{
		Dimension dimUrenSize=new Dimension(500,20);
		public Dimension getPreferredSize()
		{
			return dimUrenSize;
		}
		public Dimension getMinimumSize()
		{
			return dimUrenSize;
		}
		public void update(Graphics g)
		{
			g.drawImage(offScreenUren,0,0,this);
		}
		public void paint(Graphics g)
		{
			update(g);
		}
	}
			
	public void init()
	{
		intLanguage=iLanguage;
		initTaal();
		setBackground(Color.black);
		setForeground(Color.white);
		this.setLayout(new BorderLayout());
		label1.setText(strLabels[intLanguage][1]);
		chBreedtegraad=new Choice();
		for(int x=90;x>=-90;x-=15)
		{
			chBreedtegraad.addItem(""+x);
		}
		chBreedtegraad.select(6);
		chBreedtegraad.setBackground(Color.white);
		chBreedtegraad.setForeground(Color.black);
		chBreedtegraad.addItemListener(ilAppletListener);
		label3.setText(strLabels[intLanguage][3]);
		label4.setText(strLabels[intLanguage][4]);
		chUurIncrement=new Choice();
		chDagIncrement=new Choice();
		for(int x=0;x<=4;x++)
		{
			chUurIncrement.addItem(strSpeed[intLanguage][x]);
			chDagIncrement.addItem(strSpeed[intLanguage][x]);
		}
		chUurIncrement.select(2);
		chDagIncrement.select(2);
		chUurIncrement.setBackground(Color.white);
		chUurIncrement.setForeground(Color.black);
		chDagIncrement.setBackground(Color.white);
		chDagIncrement.setForeground(Color.black);
		chUurIncrement.addItemListener(ilAppletListener);
		chDagIncrement.addItemListener(ilAppletListener);
		pnlChoices.setLayout(new FlowLayout());
		pnlChoices.add(label1);
		pnlChoices.add(chBreedtegraad);
		pnlChoices.add(label3);
		pnlChoices.add(chUurIncrement);
		pnlChoices.add(label4);
		pnlChoices.add(chDagIncrement);
		add(pnlChoices,BorderLayout.NORTH);
		cb12=new Checkbox(strEtmaal[intLanguage][con.TWAALF],cbgEtmaal,false);
		cb24=new Checkbox(strEtmaal[intLanguage][con.VIERENTWINTIG],cbgEtmaal,true);
		cb12.addItemListener(ilAppletListener);
		cb24.addItemListener(ilAppletListener);
		offScreenSky=createImage(500,200);
		go=offScreenSky.getGraphics();
		go.setColor(Color.black);
		offScreenUren=createImage(500,20);
		gUren=offScreenUren.getGraphics();
		gUren.setColor(Color.black);
		dblZonnehoogte=new double[2];
		dblUurhoek=new double[2];
		dblDeclinatie=new double[2];
		//Seasons are defined as follows:
		//    0=spring
		// 0.25=summer
		// 0.5 =autumn
		// 0.75=winter
		dblSeizoen=new double[2];
			//initial season is winter
			dblSeizoen[con.LAZIM]=0.75;
			dblSeizoen[con.LAZAS]=0.75;
		m_AppletThread=null;
		pnlPanel.setLayout(new BorderLayout());
		buildPanels();
		pnlPanel.add(pnlTekenen,BorderLayout.CENTER);
		pnlPanel.setSize(575,220);
		add(pnlPanel,BorderLayout.CENTER);
		fm=gUren.getFontMetrics();
		paintUren();
		pnlUren.setBackground(Color.black);
		pnlUren.setForeground(Color.white);
		pnlUren.add(cb12);
		pnlUren.add(cb24);
		add(pnlUren,BorderLayout.SOUTH);
	}
	public void paintUren()
	{
		int width;
		gUren.setColor(Color.black);
		gUren.fillRect(0,0,500,20);
		gUren.setColor(Color.white);
		for(int x=DAG;x>=1;x--)
		{
			width=fm.stringWidth(""+x);
			gUren.drawString(""+x,offsetX-x*con.UURAFSTAND-width/2,10);
		}
	}
	public void start()
	{
		if(m_AppletThread==null)
		{
			m_AppletThread=new Thread(this);
			m_AppletThread.start();
		}
	}
	
	public void stop()
	{
		if(m_AppletThread!=null)
		{
			m_AppletThread.stop();
			m_AppletThread=null;
		}
	}

	public void run()
	{
		int intSunUp[]=new int[2];
		int intSunDown[]=new int[2];
		boolean blnEtmaal[]=new boolean[2];
			blnEtmaal[con.LAZAS]=false;
			blnEtmaal[con.LAZAS]=false;
		double locUur[]=new double[2];
			locUur[con.LAZAS]=DAG;
			locUur[con.LAZIM]=DAG;
		setSunUpDown();
		while(true)
		{
			locUur[con.LAZIM]+=INCREMENT;
			locUur[con.LAZAS]+=INCREMENT;
			if(dblHourChange!=0)
			{
				locUur[con.LAZIM]*=dblHourChange;
				locUur[con.LAZAS]*=dblHourChange;
				dblHourChange=0;
			}
			if(!blnWait)
			{
				if(!blnEtmaal[con.LAZIM])
				{
					if (locUur[con.LAZIM]>DAG)
					{
						blnEtmaal[con.LAZIM]=true;
						locUur[con.LAZIM]-=DAG;
					}
					else
					{
						dblUurLazim=locUur[con.LAZIM];
					}
				}
				if(!blnEtmaal[con.LAZAS])
				{

					if(locUur[con.LAZAS]>DAG)
					{
						blnEtmaal[con.LAZAS]=true;
						locUur[con.LAZAS]-=DAG;
					}
					else
					{					
						dblUurLazas=locUur[con.LAZAS];
					}
				}
			}
			else
			{
				if(locUur[con.LAZIM]>DAG)
				{
					locUur[con.LAZIM]-=DAG;
					blnEtmaal[con.LAZIM]=true;
				}
				if(locUur[con.LAZAS]>DAG)
				{
					locUur[con.LAZAS]-=DAG;
					blnEtmaal[con.LAZAS]=true;
				}
				dblUurLazim=locUur[con.LAZIM];
				dblUurLazas=locUur[con.LAZAS];
			}
			if(blnEtmaal[con.LAZAS]&&blnEtmaal[con.LAZIM])
			{	
				blnEtmaal[con.LAZAS]=false;
				blnEtmaal[con.LAZIM]=false;
				if(!blnWait)
				{
					if (!((m_nValue>740-intSeizoenIncrement)&&(intDag<intSeizoenIncrement)))
					{
						if(Math.abs(intDag-m_nValue)<=2)intDag+=intSeizoenIncrement;
					}
					if(intDag>740) intDag-=740;
					intRadius[con.LAZIM]=radius(baan.positieX[intDag],baan.positieY[intDag],LAZIMxCOORDINATE);
					intRadius[con.LAZAS]=radius(baan.positieX[intDag],baan.positieY[intDag],LAZASxCOORDINATE);				
					dblSeizoen[con.LAZIM]=Math.atan2(baan.positieY[intDag],baan.positieX[intDag]-LAZIMxCOORDINATE);
					dblSeizoen[con.LAZIM]=(dblSeizoen[con.LAZIM]+pi)/(2*pi)+.25;
					if (dblSeizoen[con.LAZIM]>=1) dblSeizoen[con.LAZIM]-=1;
					dblSeizoen[con.LAZAS]=Math.atan2(baan.positieY[intDag],baan.positieX[intDag]-LAZASxCOORDINATE);
					dblSeizoen[con.LAZAS]=(dblSeizoen[con.LAZAS]+pi)/(2*pi)+.25;
					if (dblSeizoen[con.LAZAS]>=1) dblSeizoen[con.LAZAS]-=1;
					dblLagLazim=Math.abs(dblSeizoen[con.LAZAS]-dblSeizoen[con.LAZIM]);
					if(dblLagLazim>0.5) dblLagLazim=1-dblLagLazim;
					if(baan.positieY[intDag]<0)
					{
						locUur[con.LAZIM]=-dblLagLazim*DAG-INCREMENT;
						locUur[con.LAZAS]=-INCREMENT;
					}
					else
					{
						locUur[con.LAZAS]=-dblLagLazim*DAG-INCREMENT;
						locUur[con.LAZIM]=-INCREMENT;
					}
				}
				setSunUpDown();
			}
			dblZonnehoogte[con.LAZIM]=zonnehoogteSter(con.LAZIM,dblUurLazim);
			dblZonnehoogte[con.LAZAS]=zonnehoogteSter(con.LAZAS,dblUurLazas);
			cvsCanvas.repaint();
			try
			{
				m_AppletThread.sleep(75);
			}
			catch (InterruptedException e)
			{
				m_AppletThread.stop();
			}
		}
	}
	
	public double trimDecimal(double dblIn)
	{
		//reduce the double presentation to 3 digits
		int intIn;
		intIn=(int)(100*dblIn);
		return (double)(intIn)/100;
	}
	
	public void setSunUpDown()
	{
		double dblSunUp[]=new double[2];
		double dblSunDown[]=new double[2];
		dblSunUp[con.LAZIM]=zonop(DAG,dblBreedte,dblDeclinatie[con.LAZIM]);
		dblSunDown[con.LAZIM]=zononder(dblSunUp[con.LAZIM],DAG);
		dblSunUp[con.LAZAS]=zonop(DAG,dblBreedte,dblDeclinatie[con.LAZAS]);
		dblSunDown[con.LAZAS]=zononder(dblSunUp[con.LAZAS],DAG);
		if((intDag<185)||(intDag>=555))
		{
			if(baan.positieY[intDag]>=0)
			{
				dblSunUp[con.LAZIM]-=dblLagLazim*(double)DAG;
				dblSunDown[con.LAZIM]-=dblLagLazim*(double)DAG;
				if(dblSunUp[con.LAZIM]<0) dblSunUp[con.LAZIM]+=DAG;
			}
			else
			{
				dblSunUp[con.LAZIM]+=dblLagLazim*(double)DAG;
				dblSunDown[con.LAZIM]+=dblLagLazim*(double)DAG;
				if(dblSunDown[con.LAZIM]>=DAG) dblSunDown[con.LAZIM]-=DAG;
			}
		}
		else
		{
			if(baan.positieY[intDag]<0)
			{
				dblSunUp[con.LAZAS]-=dblLagLazim*(double)DAG;
				dblSunDown[con.LAZAS]-=dblLagLazim*(double)DAG;
				if(dblSunUp[con.LAZAS]<0) dblSunUp[con.LAZAS]+=DAG;
			}
			else
			{
				dblSunUp[con.LAZAS]+=dblLagLazim*(double)DAG;
				dblSunDown[con.LAZAS]+=dblLagLazim*(double)DAG;
				if(dblSunDown[con.LAZAS]>=DAG) dblSunDown[con.LAZAS]-=DAG;
			}						
		}
		lblSunUpLazas.setText(strSunUp[con.LAZAS]+trimDecimal(dblSunUp[con.LAZAS]));
		lblSunDownLazas.setText(strSunDown[con.LAZAS]+trimDecimal(dblSunDown[con.LAZAS]));
		lblSunDownLazim.setText(strSunDown[con.LAZIM]+trimDecimal(dblSunDown[con.LAZIM]));		
		lblSunUpLazim.setText(strSunUp[con.LAZIM]+trimDecimal(dblSunUp[con.LAZIM]));
	}

	public double zonnehoogteSter(int ster,double dblUur)
	{
		dblDeclinatie[ster]=declinatie(dblAlpha,dblSeizoen[ster]);
		dblUurhoek[ster]=uurhoek(dblUur,DAG);
		return zonnehoogte(dblUur,dblBreedte,dblDeclinatie[ster],dblUurhoek[ster])*convertRadDeg;
	}


	public int radius(int x, int y,int xVanSter)
	{
		int intResult;
		double dblResult;
		x=x-xVanSter;
		dblResult=Math.sqrt(x*x+y*y);
		intResult=(int)dblResult;
		intResult=DEFAULTRADIUS*MAXDISTANCE/intResult;
		return intResult;
	}
	public double uurhoek(double uur, int dag)
	{
		return (2*pi/dag)*uur-pi;
	}
	public double declinatie(double alpha, double seizoen)
	{
		return Math.atan(Math.tan(alpha)*Math.sin(2*pi*seizoen));
	}
	public double middaghoogte(double breedte, double declinatie)
	{
		return pi/2-breedte+declinatie;
	}
	public double zonnehoogte(double uur,double breedte, double declinatie, double uurhoek)
	{
		return Math.asin(Math.cos(breedte)*Math.cos(declinatie)*Math.cos(uurhoek)+Math.sin(breedte)*Math.sin(declinatie));
	}
	public double zonnerichting(double breedte, int declinatie)
	{
		return pi/2-Math.asin(Math.sin(declinatie)/Math.cos(breedte));
	}
	public double zonop(int dag,double breedte, double declinatie)
	{
		return ((dag/2)/pi)*Math.acos(Math.tan(breedte)*Math.tan(declinatie));
	}
	public double zononder(double zonop, int dag)
	{
		return dag-zonop;
	}
	public double middernachtzonStart(double breedte, int alpha)
	{
		return Math.asin(1/(Math.tan(breedte)/Math.tan(alpha)));
	}
	
	public class AppletListener implements ItemListener
	{
		int intIndex;
		public void itemStateChanged(ItemEvent ie)
		{
			if (ie.getSource()==chBreedtegraad)
			{
				intIndex=chBreedtegraad.getSelectedIndex();
				dblBreedte=(90-intIndex*15)*convertDegRad;
			}
			else if(ie.getSource()==chUurIncrement)
			{
				intIndex=chUurIncrement.getSelectedIndex();
				INCREMENT=(double)DAG/96;
				if (intIndex>=2)
				{
					for(int x=intIndex-2;x>0;x--)
					{
						INCREMENT*=2;
					}
				}
				else
				{
					for(int x=2-intIndex;x>=0;x--)
					{
						INCREMENT/=2;
					}
				}
			}
			else if(ie.getSource()==chDagIncrement)
			{
				intSeizoenIncrement=con.SEIZOEN;
				for(int x=4-chDagIncrement.getSelectedIndex();x>0;x--)
				{
					intSeizoenIncrement/=2;
				}
			}
			else if(ie.getSource()==cb12)
			{
				DAG=12;
				con.UURAFSTAND=40;
				INCREMENT/=2;
				dblUurLazim/=2;
				dblUurLazas/=2;
				dblHourChange=.5;
				paintUren();
				cvsUrenCanvas.repaint();
			}
			else if(ie.getSource()==cb24)
			{
				DAG=24;
				con.UURAFSTAND=20;
				INCREMENT*=2;
				dblUurLazas*=2;
				dblUurLazim*=2;
				dblHourChange=2;
				paintUren();
				cvsUrenCanvas.repaint();
			}
			setSunUpDown();
		}
	}
	public void initTaal()
	{
		strSpeed=new String[2][5];
			strSpeed[con.NEDERLANDS][0]="zeer langzaam";
			strSpeed[con.NEDERLANDS][1]="langzaam";
			strSpeed[con.NEDERLANDS][2]="normaal";
			strSpeed[con.NEDERLANDS][3]="snel";
			strSpeed[con.NEDERLANDS][4]="zeer snel";
			strSpeed[con.ENGLISH][0]="very slow";
			strSpeed[con.ENGLISH][1]="slow";
			strSpeed[con.ENGLISH][2]="normal";
			strSpeed[con.ENGLISH][3]="fast";
			strSpeed[con.ENGLISH][4]="very fast";
		strLabels=new String[2][5];
			strLabels[con.NEDERLANDS][con.BREEDTE]="Breedte:";
			strLabels[con.ENGLISH][con.BREEDTE]="Latitude:";
			strLabels[con.ENGLISH][con.UREN]="Hours elapse ";
			strLabels[con.NEDERLANDS][con.UREN]="Uren verstrijken ";
			strLabels[con.NEDERLANDS][con.DAGEN]="Dagen verstrijken ";
			strLabels[con.ENGLISH][con.DAGEN]="Days elapse ";	
		strSeizoen=new String[2][5];
			strSeizoen[con.NEDERLANDS][0]="0,0625 seizoen";
			strSeizoen[con.NEDERLANDS][1]="0,125 seizoen";
			strSeizoen[con.NEDERLANDS][2]="0,25 seizoen";
			strSeizoen[con.NEDERLANDS][3]="0,5 seizoen";
			strSeizoen[con.NEDERLANDS][4]="1 seizoen";
			strSeizoen[con.ENGLISH][0]="0,0625 season";
			strSeizoen[con.ENGLISH][1]="0,125 season";
			strSeizoen[con.ENGLISH][2]="0,25 season";
			strSeizoen[con.ENGLISH][3]="0,5 season";
			strSeizoen[con.ENGLISH][4]="1 season";
		strEtmaal=new String[2][2];
			strEtmaal[con.NEDERLANDS][con.VIERENTWINTIG]="24 uur per etmaal";
			strEtmaal[con.NEDERLANDS][con.TWAALF]="12 uur per etmaal";
			strEtmaal[con.ENGLISH][con.VIERENTWINTIG]="24 hours per day";
			strEtmaal[con.ENGLISH][con.TWAALF]="12 hours per day";								
	}
	public class Enumeration
	{
		final int LAZAS=0;
		final int LAZIM=1;
		final int NEDERLANDS=1;
		final int ENGLISH=0;
		final int BREEDTE=1;
		final int UREN=3;
		final int DAGEN=4;
		final int SEIZOEN=85;
		int UURAFSTAND=20;
		final int TWAALF=1;
		final int VIERENTWINTIG=0;
		final int START=0;
		final int STOP=1;
		final int RESET=2;
		
	}
	public void handleEvent(int intEvent)
	{
		if(intEvent==con.START)
		{
			blnWait=false;
		}
		else if(intEvent==con.STOP)
		{
			blnWait=true;
		}
		else if(intEvent==con.RESET)
		{
			INCREMENT=(double)DAG/96;
			intSeizoenIncrement=con.SEIZOEN/4;
			dblBreedte=0;
			chBreedtegraad.select(6);
			chUurIncrement.select(2);
			chDagIncrement.select(2);
			cb24.setState(true);
			intDag=0;
			DAG=24;
			con.UURAFSTAND=20;
			paintUren();
			cvsUrenCanvas.repaint();
		}
	}
	public void buildPanels()
	{	
		Dimension dimCanvas,dimUrenCanvas,dimParent;
		dimParent=new Dimension(getParent().getPreferredSize());
		dimCanvas=new Dimension(cvsCanvas.getPreferredSize());
		dimUrenCanvas=cvsUrenCanvas.getPreferredSize();
		pnlTekenen.setLayout(null);
		cvsCanvas.setBounds((dimParent.width-dimCanvas.width)/2,0,
							dimCanvas.width,dimCanvas.height);
		pnlTekenen.add(cvsCanvas);
		cvsUrenCanvas.setBounds((dimParent.width-dimUrenCanvas.width)/2,dimCanvas.height+1,
								dimUrenCanvas.width,dimUrenCanvas.height);
		pnlTekenen.add(cvsUrenCanvas);
		pnlTekenen.validate();
		
	}
}
	public void setVisibleLabels(boolean visible)
	{
		lblSunUpLazim.setVisible(visible);
		lblSunDownLazim.setVisible(visible);
		lblSunUpLazas.setVisible(visible);
		lblSunDownLazas.setVisible(visible);
	}
	
}