/*
 * Copyright (C) 1998  Ralf Wiebicke
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package de.rw7.polygon;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Checkbox;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Label;
import java.awt.Panel;
import java.awt.Rectangle;

public class PunktImPolygon extends Applet
{
	Choice AlgoChoice;
	PunktImPolygonCanvas pip;
	Label Mess;

	public void init()
	{
		setLayout(new BorderLayout(10, 10));

		Panel P = new Panel();
		P.setLayout(new GridLayout(0, 1, 5, 5));
		P.add(new Button("Polygon loeschen"));
		P.add(new Checkbox("Polygon zeichnen", null, true));
		P.add(new Label("Algorithmus", Label.LEFT));
		P.add(AlgoChoice = new Choice());
		for (int i = 0; i < pip.Algos.length; i++)
			AlgoChoice.addItem(pip.Algos[i]);
		P.add(new Button("Messlauf"));
		P.add(Mess = new Label("", Label.LEFT));
		add("East", P);

		doLayout();
		final Dimension S = getSize();
		S.width -= (P.getSize().width + 10);

		pip = new PunktImPolygonCanvas(this, S);
		add("West", pip);
	}

	public boolean action(Event e, Object arg)
	{
		return (pip.action(e, arg));
	}

	public void tellWhere(int w)
	{
		switch (w)
		{
			case -1 :
				showStatus("Draussen");
				break;
			case 0 :
				showStatus("AufDerKante");
				break;
			case 1 :
				showStatus("InneWendsch");
				break;
			default :
				showStatus("Hoppla (" + String.valueOf(w) + ")");
		}
	}

	public void tellMess(long time)
	{
		Mess.setText(String.valueOf(time) + " ms");
	}

} // class PunktImPolygon

class PunktImPolygonCanvas extends Canvas implements Runnable
{
	PolygonMore P = new PolygonMore();
	Image Map;
	PunktImPolygon app;
	Dimension mySize;

	boolean fullDraw = true;
	public boolean enabledVektor = true;

	public static String Algos[] = { "Awt", "Konvex", "Ahlemeyer", "Wiebi" };
	public static Algo getByString(String s)
	{
		if (s == "Awt")
			return (new Awt());
		if (s == "Konvex")
			return (new Konvex());
		if (s == "Ahlemeyer")
			return (new Ahlemeyer());
		if (s == "Wiebi")
			return (new Wiebi());
		return (null);
	}

	PunktImPolygonCanvas(PunktImPolygon app, Dimension mySize)
	{
		this.mySize = mySize;
		setSize(mySize);
		Map = createImage(P);
		prepareImage(Map, this);
		this.app = app;
	}

	public boolean mouseDown(Event e, int x, int y)
	{
		P.addPoint(x, y);
		fullDraw = true;
		Map.flush();
		repaint();
		return (true);
	}

	public boolean mouseMove(Event e, int x, int y)
	{
		app.tellWhere(P.isInside(x, y));
		return (true);
	}

	public boolean action(Event e, Object arg)
	{
		if (e.target instanceof Choice)
		{
			P.setA(getByString((String) arg));
		}
		else if (e.target instanceof Checkbox)
		{
			enabledVektor = ((Boolean) arg).booleanValue();
			fullDraw = true;
			repaint();
			return (true);
		}
		else if (e.target instanceof Button)
		{
			if (arg.equals("Polygon loeschen"))
				P.removeAllPoints();
			else if (arg.equals("Messlauf"))
				measure();
			else
				return (false);
		}
		else
			return (false);

		fullDraw = true;
		Map.flush();
		repaint();
		return (true);
	}

	public void update(Graphics g)
	{
		if (fullDraw)
		{
			Rectangle b = getBounds();
			g.clearRect(0, 0, b.width, b.height);
			g.drawRect(0, 0, b.width - 1, b.height - 1);
			fullDraw = false;
		}
		Rectangle MB = P.getMapBox();
		g.drawImage(Map, MB.x, MB.y, Color.white, this);
		g.setColor(Color.black);
		if (enabledVektor)
			P.draw(g);
	}

	public void paint(Graphics g)
	{
		fullDraw = true;
		update(g);
	}

	Thread Measure;

	synchronized public void measure()
	{
		if (Measure == null)
		{
			Measure = new Thread(this);
			Measure.setPriority(Thread.MAX_PRIORITY);
			Measure.start();
			try
			{
				wait();
			}
			catch (InterruptedException e)
			{
			};
		}
	}

	synchronized public void run()
	{
		app.showStatus("Zeitmessung...");
		System.gc();
		int width = getSize().width;
		int height = getSize().height;
		try
		{
			wait(50);
		}
		catch (InterruptedException e)
		{
		};

		long Time = System.currentTimeMillis();
		for (int x = 0; x < width; x++)
			for (int y = 0; y < height; y++)
				P.isInside(x, y);
		Time -= System.currentTimeMillis();

		app.tellMess(Math.abs(Time));
		fullDraw = true;
		app.showStatus("fertig.");
		repaint();
		notify();
		Measure = null;
	}

	public Dimension preferredSize()
	{
		return (mySize);
	}

	public Dimension minimumSize()
	{
		return (mySize);
	}

} // class PunktImPolygonCanvas
