/*
Conway.java ( version 1.0 )
Copyright F. P. Marin ( April 07 13:12:53 1997 )
E-mail: felix@bloch.ciens.ucv.ve
Permission to use, copy, modify and distribute this software and its
documentation for NON-COMERCIAL purposes and without fee is hereby
granted provided that this copyright notice appears in all copies.
F. P. Marin makes no representations or warranties about the suitability
of the software, either express or implied, including but no limited to
the implied warranties of merchantability, fitness for a particular
purpose, or non-infringement. F. P. Marin shall not be liable for any
damage suffered by license as a result of using, modifying or
distributing this software or its derivatives.
HTML code is
*/
import java.awt.*;
public class Conway extends java.applet.Applet implements Runnable
{
final Color ALIVE=Color.yellow.brighter(),DEAD=Color.red.brighter();
final Color SCOLOR=Color.blue.brighter();
boolean mup=false,ns[][],os[][];
Button startb=new Button("Start"),stopb=new Button("Stop");
double prob=0.5;
Font fnt=new Font("TimesRoman",Font.BOLD + Font.ITALIC,20);
FontMetrics fm=getFontMetrics(fnt);
Graphics gr;
Image img;
int delay=42,dx,dy,last=21,nALIVE,ncells=484,nrc=22,olddelay;
int totalalive,x0,y0,x1,y1,xs,ys;
String s="Conway's life game = ",sta;
Thread conwaythread=null;
public void init()
{
update();
setLayout(new BorderLayout());
add("North",new NorthPanel(prob,this));
add("South",new SouthPanel(this));
add("East",stopb);
add("West",startb);
img=createImage(size().width,size().height);
gr=img.getGraphics();
repaint();
}
public void start()
{
if ( conwaythread==null ) {
conwaythread=new Thread(this);
conwaythread.start();
}
}
public void run()
{
int i,j;
repaint();
while ( true ) {
for ( i=0 ; ilast ) i=last;
if ( j<0 ) j=0 ; else if ( j>last ) j=last;
if ( !ns[i][j] ) {
ns[i][j]=true;
repaint();
}
}
return true;
}
public boolean mouseUp(Event e,int xmd,int ymd)
{
if ( ( x0<=xmd ) && ( xmd<=x1 ) && ( y0<=ymd ) && ( ymd<=y1 ) ) {
if ( conwaythread==null ) mup=true;
}
return true;
}
final public void newState(int i,int j)
{
nALIVE=state(i - 1,j - 1) + state(i,j - 1) + state(i + 1,j - 1) +
state(i - 1,j) + state(i + 1,j) +
state(i - 1,j + 1) + state(i,j + 1) + state(i + 1,j + 1);
if ( !os[i][j] ) ns[i][j]=( nALIVE==3 );
else ns[i][j]=( ( 1last ) i=0;
if ( j<0 ) j=last; else if ( j>last ) j=0;
return ( os[i][j] ) ? 1:0;
}
final public void update()
{
dx=(int)((0.8*size().width)/nrc);
dy=(int)((0.8*size().height)/nrc);
x0=(int)(0.5*(size().width - nrc*dx + 1));
y0=(int)(0.5*(size().height - nrc*dy + 1));
x1=x0 + nrc*dx;
y1=y0 + nrc*dy;
ys=y0 - 7;
ns=new boolean[nrc][nrc];
os=new boolean[nrc][nrc];
int i,j;
for ( i=0 ; i1.0 ) p=1.0;
mother.prob=p;
}
}
ncells.setText(String.valueOf(mother.ncells));
prob.setText(String.valueOf(mother.prob));
return true;
}
}
class SouthPanel extends Panel
{
Conway mother;
double ax;
int minsb;
Label l;
Scrollbar sb=new Scrollbar(Scrollbar.HORIZONTAL,0,5,0,255);
public SouthPanel(Conway mother)
{
super();
this.mother=mother;
setLayout(new GridLayout(1,3));
add(new Label("Delay = ",Label.RIGHT));
add(sb);
l=new Label(String.valueOf(mother.delay) + " msecs.",Label.LEFT);
add(l);
minsb=sb.getMinimum();
ax=4958.0/(sb.getMaximum() - minsb);
}
public boolean handleEvent(Event e)
{
int delay=(int)(42.5 + ax*(sb.getValue() - minsb));
if ( delay<42 ) delay=42; else if ( delay>5000 ) delay=5000;
mother.delay=delay;
l.setText(String.valueOf(mother.delay) + " msecs.");
return true;
}
}