import java.awt.*;

public class Geometry extends java.applet.Applet

implements Runnable

{

private Thread kicker;

GeomCanvas ConvexHull;

Label choiceLabel;

GeometryAlgorithm theAlgorithm;

GeomAlgorithmFactory algorithmFactory;

public Geometry()

{

setLayout(new BorderLayout());

ConvexHull = new GeomCanvas();

add("Center", ConvexHull);

Panel panel = new Panel();

panel.setLayout(new GridLayout(2,2));

panel.add(new Button("start")); // start and stop buttons

panel.add(new Button("stop"));

Choice choice = new Choice(); // choice of number of points

panel.add(choiceLabel = new Label ("No. of Points"));

choice.addItem("50");

choice.addItem("100");

choice.addItem("200");

panel.add(choice);

add("South", panel);

}

void pause( MyStack S )

{

if (kicker != null)

{

ConvexHull.displayStack = S;

ConvexHull.repaint();

}

try {Thread.sleep(100);} catch (InterruptedException e){}

}

public void init()

{

algorithmFactory = new StaticGeomFactory();

theAlgorithm = algorithmFactory.makeGeomAlgorithm();

theAlgorithm.setParent(this);

layout();

ConvexHull.init();

}

public boolean action(Event evt, Object what) // handle pushbuttons and choice menu

{

if ("start".equals(what))

{

startConvexHull();

}

else if ("stop".equals(what))

{

stop();

}

else if ("50".equals(what))

{

ConvexHull.NoPoints = 50;

}

else if ("100".equals(what))

{

ConvexHull.NoPoints = 100;

}

else if ("200".equals(what))

{

ConvexHull.NoPoints = 200;

}

else

{

return super.handleEvent(evt);

}

return true;

}

public void run()

{

if (theAlgorithm != null)

theAlgorithm.geometryStart(ConvexHull.SetOfPoints);

}

private synchronized void startConvexHull()

{

if (kicker == null || !kicker.isAlive() )

{

ConvexHull.randomPoints();

ConvexHull.repaint();

kicker = new Thread(this);

kicker.start();

}

}

public synchronized void stop()

{

if (kicker != null)

{

kicker.stop();

kicker = null;

}

}

}

class GeomCanvas extends Canvas

{

Dimension d;

int NoPoints = 50; // Initialize Number of Points in Set

Point SetOfPoints[]; // Set of Points

MyStack displayStack;

public void init()

{

d = size();

}

void randomPoints()

{

Point[] a = new Point[NoPoints];

for (int i = a.length; --i >= 0;)

{

int j = (int)(d.width * Math.random());

int k = (int)(d.height * Math.random());

a[i] = new Point(j, k);

}

SetOfPoints = a;

}

public void paint(Graphics g)

{

Point a, b;

Polygon c = new Polygon();

g.setColor(Color.white);

g.fillRect(0,0,d.width,d.height);

g.setColor(Color.blue);

for (int i = SetOfPoints.length; --i >= 0;)

g.fillOval(SetOfPoints[i].x, SetOfPoints[i].y, 2, 2);

if ( displayStack != null )

{

int len = displayStack.size();

for (int i=0; i<len-1; i++)

{

a = (Point) displayStack.elementAt(i);

c.addPoint(a.x, a.y);

b = (Point) displayStack.elementAt(i+1);

c.addPoint(b.x, b.y);

g.setColor(Color.yellow);

g.drawLine(a.x, a.y, b.x, b.y);

}

g.setColor(Color.yellow);

g.fillPolygon(c);

g.setColor(Color.blue);

for (int i = SetOfPoints.length; --i >= 0;)

g.fillOval(SetOfPoints[i].x, SetOfPoints[i].y, 2, 2);

}

}

public void update(Graphics g)

{

paint(g);

}

}

class Point

{

public int x, y;

public Point()

{

x=0;

y=0;

}

public Point( int x0, int y0 )

{

x = x0;

y = y0;

}

}