John Conway's Game of Life
Hi can someone help me out, i need to create john conways game of life and using JFrame, and i am fairly new at java and was wondering if you guys could help us out. it you don't know what im talking about visit -
http://students.seattleu.edu/maidungh/Programming/Java/gameoflife.html
that site has the source code of the game but when i try to complie it, it comes up with some errors. who ever answer will get 10 duke points.
ok bye.
[462 byte] By [
force3ka] at [2007-9-28 0:40:50]

Don't forget the duke's
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class GameOL extends JFrame implements ActionListener
{
JButton random = new JButton("Random");
JButton start = new JButton("Start");
JButton clear = new JButton("Clear");
JButton stop= new JButton("Stop");
JButton step= new JButton("Step");
JLabel genl= new JLabel(" Generations:");
JLabel cell= new JLabel(" Cells:");
DPanel dpan= new DPanel();
Thread thread;
public GameOL()
{
super("Game of life v1.1");
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent ev)
{
dpan.noStopRequested = false;
dispose();
System.exit(0);
}
});
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(120,1));
panel.setBorder(BorderFactory.createRaisedBevelBorder());
panel.setLayout(new GridLayout(10,0,1,7));
clear.addActionListener(this);
start.addActionListener(this);
random.addActionListener(this);
stop.addActionListener(this);
step.addActionListener(this);
panel.add(random);
panel.add(clear);
panel.add(start);
panel.add(stop);
panel.add(step);
panel.add(genl);
panel.add(cell);
getContentPane().add(dpan, BorderLayout.CENTER);
getContentPane().add(panel, BorderLayout.EAST);
pack();
setVisible(true);
thread = new Thread(dpan);
thread.start();
}
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == stop)dpan.paused = true;
if (ae.getSource() == start) dpan.paused = false;
if (ae.getSource() == step)dpan.next();
if (ae.getSource() == clear) dpan.newG(false);
if (ae.getSource() == random) dpan.newG(true);
}
public class DPanel extends JPanel implements Runnable, MouseListener
{
boolean noStopRequested = true;
boolean paused = true;
intcx=52,cy=32;
boolean cells[][]= new boolean[cx][cy];;
intnebs[][]= new int[cx][cy];
intgens,cels;
public DPanel()
{
newG(false);
setPreferredSize(new Dimension(552,331));
setBackground(new Color(125,125,125));
addMouseListener(this);
}
public void run()
{
while(noStopRequested)
{
try
{
if (!paused) next();
Thread.sleep(550);
}
catch(InterruptedException e)
{
}
}
}
public void newG(boolean random)
{
paused = true;
gens=0;
for (int x=1; x < cx-1 ; x++)
for (int y=1; y < cy-1; y++)
{
cells[x][y] = false;
if (random)
{
int n = (int)(Math.random()*11);
if (n == 5) cells[x][y] = true;
}
}
repaint();
}
private void next()
{
gens++;
for (int x=0; x < cx ; x++)
for (int y=0; y < cy; y++) nebs[x][y]=0;
for (int x=1; x < cx-1 ; x++)
for (int y=1; y < cy-1; y++)
if (cells[x][y] == true)
{
nebs[x-1][y-1]++;
nebs[x][y-1]++;
nebs[x+1][y-1]++;
nebs[x-1][y]++;
nebs[x+1][y]++;
nebs[x-1][y+1]++;
nebs[x][y+1]++;
nebs[x+1][y+1]++;
}
for (int x=1; x < cx-1 ; x++)
for (int y=1; y < cy-1; y++)
switch(nebs[x][y])
{
case 2: break;
case 3: cells[x][y] = true;
break;
default: cells[x][y] = false;
break;
}
repaint();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.lightGray);
for( int x=0; x < cx-1; x++) g.fillRect(x*11,0,1,getHeight());
for( int y=0; y < cy-1; y++) g.fillRect(0,y*11,getWidth(),1);
g.setColor(Color.yellow);
cels = 0;
for (int x=1; x < cx-1 ; x++)
for (int y=1; y < cy-1; y++)
if (cells[x][y] == true)
{
cels++;
g.fillRect(x*11-10,y*11-10,10,10);
}
genl.setText(" Generations: "+gens);
cell.setText(" Cells: "+cels);
}
public void mouseEntered(MouseEvent m){}
public void mouseExited(MouseEvent m) {}
public void mouseClicked(MouseEvent m){}
public void mousePressed(MouseEvent m)
{
}
public void mouseReleased(MouseEvent m)
{
int x = m.getX() / 11;
int y = m.getY() / 11;
if (cells[x+1][y+1] == true) cells[x+1][y+1] = false;
elsecells[x+1][y+1] = true;
repaint();
}
}
public static void main (String[] args)
{
new GameOL();
}
}
Noah