/*
 * 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.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.image.ColorModel;
import java.awt.image.ImageConsumer;
import java.awt.image.ImageProducer;

public class PolygonMore extends Polygon implements ImageProducer, Runnable
{
	Algo A = new Awt();
	boolean invalidA = true;
	Thread Produce = null;
	Rectangle BBox = null;

	void stopProduce()
	{
		if (Produce != null)
		{
			Produce.stop();
			Produce = null;
		};
	}
	void startProduce()
	{
		if (Produce == null)
		{
			Produce = new Thread(this);
			Produce.start();
		};
	}

	public void addPoint(int x, int y)
	{
		stopProduce();
		super.addPoint(x, y);
		invalidA = true;
		if (BBox == null)
			BBox = new Rectangle(x, y, 0, 0);
		else
			BBox.add(x, y);
		startProduce();
	}

	public void removeAllPoints()
	{
		stopProduce();
		npoints = 0;
		invalidA = true;
		BBox = null;
		startProduce();
	}

	public Rectangle getMapBox()
	{
		if (BBox == null)
			return (new Rectangle(2, 2));
		else
		{
			Rectangle MB = new Rectangle(BBox.x, BBox.y, BBox.width, BBox.height);
			MB.grow(10, 10);
			return (MB);
		}
	}

	public void setA(Algo A)
	{
		stopProduce();
		this.A = A;
		invalidA = true;
		startProduce();
	}

	public int isInside(int x, int y)
	{
		if (invalidA)
		{
			A.setPolygon(this);
			invalidA = false;
		}
		return (A.isInside(x, y));
	}

	ImageConsumer ic = null;
	public void addConsumer(ImageConsumer ic)
	{
		this.ic = ic;
	}
	public boolean isConsumer(ImageConsumer ic)
	{
		return (this.ic == ic);
	}
	public void removeConsumer(ImageConsumer ic)
	{
		if (this.ic == ic)
			this.ic = null;
	}
	public void requestTopDownLeftRightResend(ImageConsumer ic)
	{
	}
	public void startProduction(ImageConsumer ic)
	{
		if (ic != null)
			this.ic = ic;
		startProduce();
	}

	public void run()
	{
		if (ic == null)
		{
			Produce = null;
			return;
		};

		int bak = (Color.white).getRGB();
		int out = (Color.yellow).getRGB();
		int brd = (Color.black).getRGB();
		int ins = (Color.red).getRGB();
		int rub = (Color.pink).getRGB();
		ColorModel model = ColorModel.getRGBdefault();

		Rectangle MB = getMapBox();
		int width = MB.width;
		int[] pix = new int[width];
		for (int i = 0; i < width; i++)
			pix[i] = bak;

		try
		{
			ic.setDimensions(MB.width, MB.height);
			ic.setColorModel(model);

			for (int z = 16; z > 0; z /= 4)
				for (int y = 0; y < MB.height; y += z)
					for (int x = 0; x < width; x += z)
					{
						switch (isInside(x + MB.x, y + MB.y))
						{
							case -1 :
								pix[x] = out;
								break;
							case 0 :
								pix[x] = brd;
								break;
							case 1 :
								pix[x] = ins;
								break;
							default :
								pix[x] = rub;
						}
						ic.setPixels(0, y, width, 1, model, pix, 0, width);
					}

			ic.imageComplete(ImageConsumer.STATICIMAGEDONE);
		}
		catch (NullPointerException e)
		{
		};

		Produce = null;
	}

	public void draw(Graphics g)
	{
		if (npoints > 0)
		{
			g.drawPolygon(this);
			g.drawLine(
				xpoints[0],
				ypoints[0],
				xpoints[npoints - 1],
				ypoints[npoints - 1]);
			for (int i = 0; i < npoints; i++)
			{
				g.drawOval(xpoints[i] - 5, ypoints[i] - 5, 10, 10);
				g.drawString(String.valueOf(i), xpoints[i] + 5, ypoints[i] + 5);
			}
		}
	}

}
