import java.applet.Applet;
import java.util.*;
import java.awt.*;
public class ll extends Applet {
Vector CrossList;
Vector Centroids;
Choice SubsetChoice;
Button StartButton,RestartButton,ResetButton,RunButton,DrawGButton;
int step;
int subset;
Random rand;
boolean abort;
public void init() {
rand = new Random();
Centroids = new Vector();
StartButton = new Button("Empezar");
add(StartButton);
StartButton.setEnabled(false);
DrawGButton = new Button("Dibujar Cluster");
add(DrawGButton);
CrossList = new Vector();
SubsetChoice = new Choice();
SubsetChoice.addItem("2");
SubsetChoice.addItem("3");
SubsetChoice.addItem("4");
SubsetChoice.addItem("5");
add(SubsetChoice);
subset = 2;
step = -1;
}
public void paint(Graphics g)
{
Cross s;
int numShapes = CrossList.size();
for (int i = 0; i < numShapes; i++)
{
s = (Cross) CrossList.elementAt(i);
s.draw(g);
}
if (step != -1)
{
Quad t = new Quad();
int numCent = Centroids.size();
for (int i = 0; i < numCent; i++)
{
t = (Quad) Centroids.elementAt(i);
t.draw(g);
}
}
}
public boolean mouseUp(Event e, int x, int y) {
if ((step == -1) && (allowedMousePosition(x,y)== true))
{
StartButton.setEnabled(true);
Cross s = new Cross();
s.color = Color.black;
s.x = x;
s.y = y;
CrossList.addElement(s);
repaint();
}
return true;
}
public boolean allowedMousePosition(int x, int y)
{
if ((x>=5)&&(y>=55)&&(x<595)&&(y<345)) return true;
else return false;
}
public boolean action(Event event, Object eventobject)
{
if ((event.target==StartButton))
{
StartButton.setLabel("Step");
if (step ==-1) this.step1();
else if (step == 1) this.step2();
else if (step == 2) this.step3();
else if (step == 3) step = 4;
else if ((step == 4) && (abort==true))
{
StartButton.setEnabled(false);
step = 5;
}
else if ((step == 4) && (abort==false)) this.step2();
repaint();
return true;
}
if ((event.target==RunButton))
{
StartButton.setEnabled(false);
return true;
}
if ((event.target==DrawGButton))
{
if (CrossList.size()>0) Reset();
String SubsetString = SubsetChoice.getSelectedItem();
if (SubsetString.equals("2")) subset = 2;
if (SubsetString.equals("3")) subset = 3;
if (SubsetString.equals("4")) subset = 4;
if (SubsetString.equals("5")) subset = 5;
Vector GaussianList;
GaussianList = new Vector();
for (int i = 0; i<subset;i++)
{
Gaussian gaus = new Gaussian();
gaus.mux = 50 + Math.abs(rand.nextInt() % 450);
gaus.muy = 75 + Math.abs(rand.nextInt() % 275);
gaus.sigma = 10 + Math.abs(30 * rand.nextDouble());
GaussianList.addElement(gaus);
}
StartButton.setEnabled(true);
for (int i = 0; i<subset;i++)
{
Gaussian gaus;
gaus = (Gaussian) GaussianList.elementAt(i);
for (int j = 0;j<2800/subset;j++)
{
double r = 5*gaus.sigma*Math.pow(rand.nextDouble(),2);
double alpha = 2*Math.PI*rand.nextDouble();
int x = gaus.mux + (int) Math.round(r*Math.cos(alpha));
int y = gaus.muy + (int) Math.round(r*Math.sin(alpha));
if (allowedMousePosition(x,y)==true)
{
Cross s = new Cross();
s.color = Color.black;
s.x = x;
s.y = y;
CrossList.addElement(s);
}
}
}
repaint();
return true;
}
if ((event.target==RestartButton) && (step !=-1))
{
step = -1;
abort = false;
Centroids.removeAllElements();
int numShapes = CrossList.size();
Cross s;
for (int i = 0; i < numShapes; i++)
{
s = (Cross) CrossList.elementAt(i);
s.color = Color.black;
}
StartButton.setLabel("Empezar");
StartButton.setEnabled(true);
this.repaint();
return true;
}
if ((event.target==ResetButton))
{
Reset();
return true;
}
return true;
}
public void Reset()
{
step = -1;
abort = false;
Centroids.removeAllElements();
int numShapes = CrossList.size();
Cross s;
for (int i = 0; i < numShapes; i++)
{
s = (Cross) CrossList.elementAt(i);
s.color = Color.white;
}
StartButton.setLabel("Emepzar");
StartButton.setEnabled(false);
CrossList.removeAllElements();
this.repaint();
}
public void step1()
{
abort = false;
String SubsetString = SubsetChoice.getSelectedItem();
if (SubsetString.equals("2")) subset = 2;
if (SubsetString.equals("3")) subset = 3;
if (SubsetString.equals("4")) subset = 4;
if (SubsetString.equals("5")) subset = 5;
int numShapes = CrossList.size();
boolean ch[] = new boolean[numShapes];
for (int i = 0; i<numShapes;i++) ch[i]=false;
for (int i = 0; i<subset;)
{
Cross s;
Quad p = new Quad();
int r = Math.abs(rand.nextInt() % numShapes);
if (ch[r]==false)
{
s = (Cross) CrossList.elementAt(r);
p.x = s.x;
p.y = s.y;
if (i == 0) p.color = Color.green;
else if (i == 1) p.color = Color.red;
else if (i == 2) p.color = Color.blue;
else if (i == 3) p.color = Color.yellow;
p.History = new Vector();
Centroids.addElement(p);
ch[r] = true;
i++;
}
}
step = 1;
}
public void step2()
{
Cross s;
Quad p;
int numShapes = CrossList.size();
for (int i = 0; i < numShapes; i++)
{
s = (Cross) CrossList.elementAt(i);
int numCent = Centroids.size();
int min = 0;
double dist_min = 99999999.9;
for (int j = 0; j < numCent; j++)
{
p = (Quad) Centroids.elementAt(j);
double dist = Point.distance(s.x, s.y, p.x, p.y);
if (dist < dist_min)
{
dist_min = dist;
min = j;
}
}
p = (Quad) Centroids.elementAt(min);
s.color = p.color;
}
step = 2;
}
public void step3()
{
Quad p;
Cross s;
Point m = new Point();
double changes = 0.0;