import java.util.Random;

public class IsingSimulation implements Runnable {

// Simulation code lives here 

public double beta=0.0;

// ugly - need to set L here as well as in applet 

private int L=8;
public int spin[][] = new int[L][L] ;

// contain current spin during update 
// needed by update to clip frame

public int iglobal,jglobal;

private Ising appletref;

// Constructor for simulation object

IsingSimulation(Ising ref){
int i,j;

for(i=0;i<L;i++)
for(j=0;j<L;j++)
spin[i][j]=1;

appletref=ref;
}

// Guts of simulation code lies in run method

public void run(){
double sum,dum;
Random number = new Random();
int i,j;

while(true){

i=(int)Math.floor(number.nextDouble()*L);
j=(int)Math.floor(number.nextDouble()*L);
iglobal=i;
jglobal=j;

sum=spin[(i+1)%L][j]+spin[(i+L-1)%L][j]+spin[i][(j+1)%L]+
spin[i][(j+L-1)%L];
dum=beta*2.0*spin[i][j]*sum;

if(dum<0.0){ spin[i][j]=-spin[i][j];}
else{
dum=Math.exp(-dum);
if(number.nextDouble()<dum) {spin[i][j]=-spin[i][j];}
}

// Instructs DrawSpins to repaint 

appletref.drawing.repaint();

// Sleeps thread for a while

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


}

}
}

