package radar;

import java.awt.*;
import abstractAnimation.DoubledBufferedAnimationApplet;

public class Radar extends DoubledBufferedAnimationApplet 
{
	/**
	 * Permet l'exécution en tant qu'application
	 * @param applet
	 * @param arguments
	 */
	public static void main(String [] arguments)
	{
		DoubledBufferedAnimationApplet.main(new Radar(), arguments);
	}

	/**
	 * angle du radar
	 */
	private float angle;

	/**
	 * aperture du faisceau
	 */
    private float operture;
    
    private float increment;
    
	/**
	 * remise à "zero" de l'animation.
	 * (re)initialisation des coordonnées des objets de l'animation, etc.
	 */
	protected void reset()
	{
		angle = 0;
		operture = 1;
		increment = .3f;
	}
	
	/**
	 * "battement" de l'animation.
	 * mise à jour des coordonnées des objets de l'animation, etc.
	 */
	protected void tick()
	{
		// On fait tourner le radar
		angle += .75;
		//angle %= 360;
		
		operture += increment;
		if(Math.abs(operture) > 180) { increment = -increment; }
	}

	/**
	 * le traçage proprement dit du dessin.
	 */
	protected void trace(Graphics graphics, Dimension dimension)
	{
		/*
		graphics.setXORMode(Color.BLACK);
		graphics.setColor(Color.WHITE);
		graphics.fillRect(0, 0, dimension.width, dimension.height);
		Thread.yield();
		*/
		
		graphics.setPaintMode();
		
		graphics.setColor(Color.BLACK);
		graphics.fillRect(0, 0, dimension.width, dimension.height);
		
		final float range = 10;
		
		graphics.setXORMode(Color.CYAN);
		{
			/*
			final float radiusX = getSize().width / 2;
			final float radiusY = getSize().height / 2;
			final float centerX = getSize().width / 2;
			final float centerY = getSize().height / 2;
			final float left = centerX - radiusX;
			final float top  = centerY - radiusY;
			final float width = radiusX * 2;
			final float height = radiusY * 2;
			*/
			{
				for(float i = -range ; i <= range ; ++i)
				{
					if(i == 0) continue;
					{
						Color color = new Color(Math.abs(i) / range, Math.abs(i) / range, Math.abs(i) / range);
						graphics.setColor(color);
					}
					final float radiusX = getSize().width  / 2;
					final float radiusY = getSize().height / 2;
					final float centerX = (float)(getSize().width  / 2 + .1 * radiusX * i / range * Math.sin(angle));
					final float centerY = (float)(getSize().height / 2 + .1 * radiusY * i / range * Math.cos(angle));
					final float left = centerX - radiusX;
					final float top  = centerY - radiusY;
					final float width = radiusX * 2;
					final float height = radiusY * 2;
					graphics.fillArc((int)left, (int)top, (int)width, (int)height, (int)(angle * 3 * (i / range)), (int)operture);
				}
			}
		}

		graphics.setXORMode(Color.YELLOW);
		{
			for(float i = -range ; i <= range ; ++i)
			{
				if(i == 0) continue;
				{
					Color color = new Color(Math.abs(i) / range, Math.abs(i) / range, Math.abs(i) / range);
					graphics.setColor(color);
				}
				final float radiusX = getSize().width  / 2 * (float)Math.pow(Math.abs(i) / range, angle % 360 / 360);
				final float radiusY = getSize().height / 2 * (float)Math.pow(Math.abs(i) / range, angle % 360 / 360);
				final float centerX = (float)(getSize().width  / 2 + .2 * radiusX * i / range * Math.cos(angle));
				final float centerY = (float)(getSize().height / 2 + .2 * radiusY * i / range * Math.sin(angle));
				final float left = centerX - radiusX;
				final float top  = centerY - radiusY;
				final float width = radiusX * 2;
				final float height = radiusY * 2;
				graphics.fillArc((int)left, (int)top, (int)width, (int)height, (int)(angle * 3 * (i / range)), (int)operture);
			}
		}
		
		graphics.setXORMode(Color.PINK);
		{
			for(float i = -range ; i <= range ; ++i)
			{
				if(i == 0) continue;
				{
					Color color = new Color(1 - Math.abs(i) / range, 1 - Math.abs(i) / range, 1 - Math.abs(i) / range);
					graphics.setColor(color);
				}
				final float radiusX = getSize().width  / 2 * (1 - (float)Math.pow(Math.abs(i) / range, 1 + angle % 360 / 360));
				final float radiusY = getSize().height / 2 * (1 - (float)Math.pow(Math.abs(i) / range, 1 + angle % 360 / 360));
				final float centerX = (float)(getSize().width  / 2 + .2 * radiusX * i / range * Math.cos(-angle));
				final float centerY = (float)(getSize().height / 2 + .2 * radiusY * i / range * Math.sin(-angle));
				final float left = centerX - radiusX;
				final float top  = centerY - radiusY;
				final float width = radiusX * 2;
				final float height = radiusY * 2;
				graphics.fillArc((int)left, (int)top, (int)width, (int)height, (int)(angle * 3 * (i / range)), (int)operture);
			}
		}
	}
}
 
