public class Simulation implements Runnable{
private DLA app;
private Plot plot;
private int lattice;
private double field;
private Cluster cluster;
private boolean ClusteronBoundary;
private Controls panel;

Simulation(DLA ref, Plot ref2, Cluster c, Controls ref3, int lat){
app=ref;
plot=ref2;
lattice=lat;
cluster=c;
panel=ref3;
}


public void run(){
int xnew,ynew,xpos,ypos;
boolean not_sticking;
double dum1,dum2;

// set up seed

while(true){

app.runner.suspend();

ClusteronBoundary=false;

plot.clearCluster();
plot.clearPath();
cluster.setOccupiedFalse();
cluster.setOccupied(lattice/2,lattice/2);
plot.addClusterPoint(lattice/2,lattice/2);
panel.setClusterSize(1);


while(!ClusteronBoundary){

//app.runner.suspend();

plot.clearPath();

// pick random boundary point 

not_sticking=true;

do{
dum1=Math.random();
dum2=Math.random();
if(dum1<0.25){xpos=0;ypos=(int)(dum2*lattice);}
else if((dum1>0.25)&&(dum1<0.5)){xpos=lattice-1;ypos=(int)(dum2*lattice);}
else if((dum1>0.5)&&(dum1<0.75)){ypos=0;xpos=(int)(dum2*lattice);}
else {ypos=lattice-1;xpos=(int)(dum2*lattice);}
}
while((cluster.isOccupied(xpos,ypos))||
      (cluster.isNeighbortoCluster(xpos,ypos)));

plot.addPathPoint(xpos,ypos);

// random walk point until sticks

while(not_sticking){


dum1=Math.random();
dum2=Math.random();

if(dum2>0.5){
if(dum1<0.5) {xnew=(xpos-1);if (xnew<0) {xnew=xpos;} ynew=ypos;}
else         {xnew=(xpos+1);if (xnew==lattice) {xnew=xpos;} ynew=ypos;}
}
else{
if(dum1<0.5) {ynew=(ypos-1);if (ynew<0) {ynew=ypos;} xnew=xpos;}
else         {ynew=(ypos+1);if (ynew==lattice) {ynew=ypos;} xnew=xpos;}
} 

if(!cluster.isOccupied(xnew,ynew)){
xpos=xnew;
ypos=ynew;
plot.addPathPoint(xpos,ypos);
}


if(cluster.isNeighbortoCluster(xpos,ypos)){
cluster.setOccupied(xpos,ypos);
panel.setClusterSize(plot.getlastClusterPoint()+2);
plot.addClusterPoint(xpos,ypos);
not_sticking=false;
ClusteronBoundary=cluster.BoundaryPoint(xpos,ypos);
}

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

}

}

}

}

}



