/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package gameoflife;
/**
*
* @author gaurav
*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Gameoflife extends Applet implements ActionListener,Runnable{
Button [][]square;
Thread []t;
int i=0,j=0,k=0,l,s=1,p=0,q;
Button set,start,stop;
int [][]status;
int [][]coun;
int y=0;
public void init(){setLayout(null);
status=new int[40][40];
coun=new int[40][40];
for(int i=0;i<40;i++){for(int j=0;j<40;j++){status[i][j]=0;}}
for(int i=0;i<40;i++){for(int j=0;j<40;j++){coun[i][j]=0;}}
set=new Button("set");
add(set);
set.addActionListener(this);
set.setBackground(Color.red);
set.reshape(300,640,100,20);
start=new Button("START"); stop=new Button("stop");
add(stop);
stop.addActionListener(this);
stop.setBackground(Color.yellow);
stop.reshape(420,640,100,20);
add(start);start.addActionListener(this);
start.reshape(550,640,100,20);
start.setBackground(Color.green);
square=new Button[40][40];
for(int i=0;i<40;i++){
for(int j=0;j<40;j++)
{square[i][j]=new Button();
add(square[i][j]);square[i][j].setBackground(Color.white);
square[i][j].reshape(200+(15*j), 20+(15*i), 15, 15);
square[i][j].addActionListener(this);
}}
t=new Thread[100];
for(int x=0;x<100;x++)
t[x]=new Thread(this) ;
}
public void run()
{try{do{
Thread.sleep(300);
for(int i=1;i<39;i++){for(int j=1;j<39;j++)
{coun[i][j]=0;
if(status[i+1][j]==1)
coun[i][j]++;
if(status[i][j+1]==1)
coun[i][j]++;
if(status[i-1][j]==1)
coun[i][j]++;
if(status[i][j-1]==1)
coun[i][j]++;
if(status[i+1][j+1]==1)
coun[i][j]++;
if(status[i-1][j+1]==1)
coun[i][j]++;
if(status[i-1][j-1]==1)
coun[i][j]++;
if(status[i+1][j-1]==1)
coun[i][j]++;}}
if(status[1][0]==1)coun[0][0]++;
if(status[1][1]==1)coun[0][0]++;
if(status[0][1]==1)coun[0][0]++;
if(status[1][39]==1)coun[0][39]++;
if(status[1][38]==1)coun[0][39]++;
if(status[0][38]==1)coun[0][39]++;
if(status[38][0]==1)coun[39][0]++;
if(status[38][1]==1)coun[39][0]++;
if(status[39][1]==1)coun[39][0]++;
if(status[38][38]==1)coun[39][39]++;
if(status[38][39]==1)coun[39][39]++;
if(status[39][38]==1)coun[39][39]++;
for(int a=1;a<39;a++)
{if(status[0][a+1]==1)coun[0][a]++;
if(status[0][a-1]==1)coun[0][a]++;
if(status[1][a]==1)coun[0][a]++;
if(status[1][a-1]==1)coun[0][a]++;
if(status[1][a+1]==1)coun[0][a]++;
}
for(int a=1;a<39;a++)
{if(status[39][a+1]==1)coun[39][a]++;
if(status[39][a-1]==1)coun[39][a]++;
if(status[38][a]==1)coun[39][a]++;
if(status[38][a-1]==1)coun[39][a]++;
if(status[38][a+1]==1)coun[39][a]++;
}
for(int a=1;a<39;a++)
{if(status[a+1][0]==1)coun[a][0]++;
if(status[a-1][0]==1)coun[a][0]++;
if(status[a+1][1]==1)coun[a][0]++;
if(status[a][1]==1)coun[a][0]++;
if(status[a+1][1]==1)coun[a][0]++;
}
for(int a=1;a<39;a++)
{if(status[a+1][39]==1)coun[a][39]++;
if(status[a-1][39]==1)coun[a][39]++;
if(status[a+1][38]==1)coun[a][39]++;
if(status[a][38]==1)coun[a][39]++;
if(status[a-1][38]==1)coun[a][39]++;
}
for(int i=1;i<39;i++){for(int j=1;j<39;j++){
if(coun[i][j]<2&&status[i][j]==1){
square[i][j].setBackground(Color.white);status[i][j]=0;}
else if(coun[i][j]>=2&&coun[i][j]<=3&&status[i][j]==1){}
else if(coun[i][j]>3&&status[i][j]==1){status[i][j]=0;square[i][j].setBackground(Color.white);}
else if(coun[i][j]==3&&status[i][j]==0){status[i][j]=1;square[i][j].setBackground(Color.black);}
}}
// Thread.sleep(300);
} while(true);
}
catch(InterruptedException e)
{System.out.println("gaurav");
}
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==set)
{y++;
s=1;p=0;
repaint();for(int i=0;i<40;i++){
for(int j=0;j<40;j++){
square[i][j].setBackground(Color.white);
coun[i][j]=0;
status[i][j]=0;}}}
for(int i=0;i<40;i++){
for(int j=0;j<40;j++){if(ae.getSource()==square[i][j]){square[i][j].setBackground(Color.black);
status[i][j]=1;
}}}
if(ae.getSource()==start)
{s=0;p=0;repaint();
t[y].start();
}
if(ae.getSource()==stop){
p=1;s=0;repaint();
t[y].stop();
t.notify();
}
}
public void paint(Graphics g)
{
if(s==1){ g.setColor(Color.CYAN);
g.fillRect(350, 0, 300, 30);
g.setColor(Color.blue);
g.drawString("SET THE DESIGN", 400,15);}
if(p==1){ g.setColor(Color.CYAN);g.fillRect(350, 0, 300, 30);
g.setColor(Color.blue);
g.drawString("TO START AGAIN PRESS SET BUTTON", 375, 15);}
}
}
No comments:
Post a Comment