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

public class DeflationApplet extends Applet implements MouseListener 
{
	MyPolygon poly;
	int old_point;
	private boolean initialized;
	int last_done;
	private boolean random_poly;

	public DeflationApplet()
	{
		initialized = false;
	}

	public void init()
	{
		initialize_poly();
		addMouseListener(this);
	}

	public void initialize_poly()
	{		
		Dimension size = this.getSize();
		String rand = getParameter("RANDOM");
		if(rand != null)
			random_poly = rand.equals("true");
		else
			random_poly = false;

		if(!random_poly){
			float multiplier = (float)size.width / (float)5;
			poly = new MyPolygon(4, this);
			poly.pts[0].x = multiplier;
			poly.pts[0].y = multiplier;
			poly.pts[1].x = 4 * multiplier;
			poly.pts[1].y = multiplier;
			poly.pts[2].x = 3 * multiplier;
			poly.pts[3].x = 2 * multiplier;
			poly.pts[3].y = poly.pts[2].y = (float) (multiplier + multiplier * Math.sqrt(3));
		} else {
			poly = new MyPolygon(-1, this);
			poly.makeRandomPoly(size.width, size.height);
		}
		old_point = -1;
		last_done = 1;
		poly.scaleToSize(size.width, size.height);
		initialized = true;
	}

	public void update(Graphics g)
	{
		paint(g);
		poly.draw(g);
	}

	public void paint(Graphics g)
	{
		Dimension d = getSize();
		g.setColor(Color.black);
		g.fillRect(0, 0, d.width - 1, d.height - 1);
		poly.draw(g);
		if(old_point != -1){
			poly.pts[old_point].draw(g);
		}
	}

	/*

	public void status(String s)
	{
		showStatus(s);
	}
	*/

	public void mousePressed(MouseEvent e)
	{
		int x, y;
		int new_point = -1;
		x = e.getX();
		y = e.getY();

		if((e.BUTTON1_MASK & e.getModifiers()) == e.BUTTON1_MASK){
			if(!random_poly){
				poly.reflect2Points(last_done % 2, (last_done % 2) + 2, random_poly);
				last_done++;
				repaint();
			} else {
				if(old_point == -1){
					old_point = poly.nearPoint(x, y);
					repaint();
					return;
				}
				else
					new_point = poly.nearPoint(x, y);
				if(Math.abs(new_point - old_point) < 2)
					return;
				if(new_point != -1){
					// we have two good points.  now reflect
					// the polygon around them.
					poly.reflect2Points(old_point, new_point, random_poly);
					repaint();
					// if the result isn't simple, reflect it back
					if(!poly.isSimple()){
						showStatus("not simple");
						poly.reflect2Points(old_point, new_point, random_poly);
					}
			
					repaint();
					old_point = -1;
				}
			}
		}
		else if((e.BUTTON3_MASK & e.getModifiers()) == e.BUTTON3_MASK){
			initialize_poly();
			repaint();
		} else { // work around old javas
			if(!random_poly){
				poly.reflect2Points(last_done % 2, (last_done % 2) + 2, random_poly);
				last_done++;
				repaint();
			} else {
				if(old_point == -1){
					old_point = poly.nearPoint(x, y);
					repaint();
					return;
				}
				else
					new_point = poly.nearPoint(x, y);
				if(Math.abs(new_point - old_point) < 2)
					return;
				if(new_point != -1){
					// we have two good points.  now reflect
					// the polygon around them.
					poly.reflect2Points(old_point, new_point, random_poly);
					repaint();
					// if the result isn't simple, reflect it back
					if(!poly.isSimple()){
						showStatus("not simple");
						poly.reflect2Points(old_point, new_point, random_poly);
					}
			
					repaint();
					old_point = -1;
				}
			}
		}
	}

	public void mouseReleased(MouseEvent e)
	{
	}
	
	public void mouseEntered(MouseEvent e)
	{
	}

	public void mouseExited(MouseEvent e)
	{
	}

	public void mouseClicked(MouseEvent e)
	{
	}
}


syntax highlighted by Code2HTML, v. 0.9