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

/**
 * This class reads PARAM tags from its HTML host page and sets
 * the color and label properties of the applet. Program execution
 * begins with the init() method. 
 */
public class OrbitSmall extends Applet implements Runnable
{
	int WIDTH=320;
	int HEIGHT=160;
	final int LAZIMxCOORDINATE=-100;
	final int LAZASxCOORDINATE=100;
	final int DEFAULTWIDTH=500;
	final int DEFAULTHEIGHT=250;
	final int DEFAULTOFFSETX=250;
	final int DEFAULTOFFSETY=150;
	final int DEFAULTRADIUSSTAR=80;
	final int DEFAULTRADIUSUTEGED=60;
	final int NUMBERofFRAMES=14;
	int offsetX,offsetY;
	int day;
	double scaleFactorX,scaleFactorY;
	int radiusStar,radiusUteged;
	Image imgUteged,imgLazim,imgLazas,imgFrame[];
	Image offScreenImage;
	Graphics go;
	Thread m_AppletThread;
	Baan baan=new Baan();
	OrbitCanvas orbitCanvas=new OrbitCanvas();
	
	public class OrbitCanvas extends Canvas
	{
		public Dimension getPreferredSize()
		{
			return new Dimension(320,160);//().getPreferredSize();
		}
		public Dimension getMinimumSize()
		{
			return new Dimension(320,160);//().getPreferredSize();			
		}
		public void paint(Graphics g)
		{
			go.setColor(Color.yellow);
			go.fillRect(0,0,320,160);
			go.setColor(Color.black);
			go.fillOval(0,0,160,160);
			go.fillOval(160,0,160,160);
			go.drawImage(imgLazim,offsetX+(int)(LAZIMxCOORDINATE*scaleFactorX)-radiusStar/2,
					   offsetY-radiusStar/2,radiusStar,radiusStar,this);

			go.drawImage(imgLazas,offsetX+(int)(LAZASxCOORDINATE*scaleFactorX)-radiusStar/2,
					   offsetY-radiusStar/2,radiusStar,radiusStar,this);
			go.drawImage(imgUteged,offsetX+(int)(baan.positieX[day]*scaleFactorX)-radiusUteged/2,
						offsetY-(int)(baan.positieY[day]*scaleFactorY)-radiusUteged/2,
						radiusUteged,radiusUteged,this);
			g.drawImage(offScreenImage,0,0,this);
		}
		public void update(Graphics g)
		{
			paint(g);
		}
		
	}
	/**
	 * The entry point for the applet. 
	 */
	public void init()
	{
		initForm();
		usePageParams();
		baan.init();
		setLayout(null);
		offScreenImage=createImage(320,160);
		go=offScreenImage.getGraphics();
		WIDTH=320;//this.getPreferredSize().width;
		HEIGHT=135;//this.getPreferredSize().height;
		scaleFactorX=(double)WIDTH/(double)DEFAULTWIDTH;
		scaleFactorY=(double)HEIGHT/(double)DEFAULTHEIGHT;
		offsetX=(int)(DEFAULTOFFSETX*scaleFactorX);
		offsetY=(int)(DEFAULTOFFSETY*scaleFactorY);
		radiusStar=(int)(DEFAULTRADIUSSTAR*(scaleFactorX+scaleFactorY)/2);
		radiusUteged=(int)(DEFAULTRADIUSUTEGED*(scaleFactorX+scaleFactorY)/2);
		imgFrame=new Image[NUMBERofFRAMES];
		for(int x=1;x<=NUMBERofFRAMES;x++)
		{
			imgFrame[NUMBERofFRAMES-x]=waitForImage("Sprite"+x+".gif");
		}
		imgUteged=imgFrame[0];
		imgLazim=waitForImage("Lazim.gif");
		imgLazas=waitForImage("Lazas.gif");
		orbitCanvas.setBounds(0,0,320,160);
		add(orbitCanvas);
		orbitCanvas.repaint();
	}
	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;
	}

	private	final String labelParam = "label";
	private	final String backgroundParam = "background";
	private	final String foregroundParam = "foreground";
	private final String dayParam="day";

	/**
	 * 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";
		final String defaultDay="0";
		String labelValue;
		String backgroundValue;
		String foregroundValue;
		String dayValue;

		/** 
		 * Read the <PARAM NAME="label" VALUE="some string">,
		 * <PARAM NAME="background" VALUE="rrggbb">,
		 * and <PARAM NAME="foreground" VALUE="rrggbb"> tags from
		 * the applet's HTML host.
		 */
		labelValue = getParameter(labelParam);
		backgroundValue = getParameter(backgroundParam);
		foregroundValue = getParameter(foregroundParam);
		dayValue=getParameter(dayParam);
		day=(Integer.valueOf(dayValue)).intValue();

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

		/**
		 * Set the applet's string label, background color, and
		 * foreground colors.
		 */
		label1.setText(labelValue);
		label1.setBackground(stringToColor(backgroundValue));
		label1.setForeground(stringToColor(foregroundValue));
		this.setBackground(stringToColor(backgroundValue));
		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\"" },
			{dayParam,"Integer","Day to show the Planet Uteged"}
		};
		return info;
	}

	Label label1 = new Label();

	/**
	 * Intializes values for the applet and its components
	 */
	void initForm()
	{
		this.setBackground(Color.lightGray);
		this.setForeground(Color.black);
		label1.setText("label1");
	}
	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 intFrame;
		while (true)
		{
			for(intFrame=0;intFrame<=13;intFrame++)
			{
				imgUteged=imgFrame[intFrame];
				orbitCanvas.repaint();
				try
				{
					Thread.sleep(150);
				}
				catch (InterruptedException e)
				{}	
			}
		}		
	}
}
