Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 21st, 2012  |  syntax: None  |  size: 4.50 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Issue with Game of Life
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class Cell extends JComponent implements MouseListener {
  7.  
  8.   private int row, col;
  9.   private boolean isLiving;
  10.  
  11.   public Cell(int r, int c) {
  12.     this.row = r;
  13.     this.col = c;
  14.     this.addMouseListener(this);
  15.   }
  16.  
  17.   public void isAlive(int neighbors) {
  18.     if (this.isLiving) {
  19.       if (neighbors < 2) {
  20.         this.isLiving = false;
  21.       } else if (neighbors == 2 || neighbors == 3) {
  22.         this.isLiving = true;
  23.       } else if (neighbors > 3) {
  24.         this.isLiving = false;
  25.       }
  26.     } else {
  27.       if (neighbors == 3) {
  28.         this.isLiving = true;
  29.       }
  30.     }
  31.   }
  32.  
  33.   public boolean isLiving() {
  34.     return this.isLiving;
  35.   }
  36.  
  37.   public void paintComponent(Graphics g) {
  38.     if (this.isLiving) {
  39.       g.fillRect(0, 0, 10, 10);
  40.     } else {
  41.       g.drawRect(0, 0, 10, 10);
  42.     }
  43.   }
  44.  
  45.   public void mouseClicked(MouseEvent e) {
  46.     this.isLiving = !this.isLiving;
  47.   }
  48.  
  49.   public void mouseEntered(MouseEvent e) {
  50.   }
  51.  
  52.   public void mouseExited(MouseEvent e) {
  53.   }
  54.  
  55.   public void mousePressed(MouseEvent e) {
  56.   }
  57.  
  58.   public void mouseReleased(MouseEvent e) {
  59.   }
  60. }
  61.        
  62. import javax.swing.*;
  63. import java.awt.*;
  64. import java.awt.event.*;
  65. import java.util.Random;
  66.  
  67. class Cell extends JComponent implements MouseListener {
  68.  
  69.   private int row, col;
  70.   private boolean isLiving;
  71.  
  72.   public static Random random = new Random();
  73.  
  74.   public Cell(int r, int c) {
  75.     this.row = r;
  76.     this.col = c;
  77.     this.addMouseListener(this);
  78.     isLiving = random.nextBoolean();
  79.   }
  80.  
  81.   public boolean isAlive(int neighbors) {
  82.       boolean alive = false;
  83.     if (this.isLiving) {
  84.       if (neighbors < 2) {
  85.         alive = false;
  86.       } else if (neighbors == 2 || neighbors == 3) {
  87.         alive = true;
  88.       } else if (neighbors > 3) {
  89.         alive = false;
  90.       }
  91.     } else {
  92.       if (neighbors == 3) {
  93.         alive = true;
  94.       }
  95.     }
  96.     return alive;
  97.   }
  98.  
  99.   public void setAlive(boolean alive) {
  100.       isLiving = alive;
  101.   }
  102.  
  103.   public boolean isLiving() {
  104.     return this.isLiving;
  105.   }
  106.  
  107.   public void paintComponent(Graphics g) {
  108.     if (this.isLiving) {
  109.       g.fillRect(0, 0, getWidth()-1, getHeight()-1);
  110.     } else {
  111.       g.drawRect(0, 0, getWidth()-1, getHeight()-1);
  112.     }
  113.   }
  114.  
  115.   public void mouseClicked(MouseEvent e) {
  116.     this.isLiving = !this.isLiving;
  117.     repaint();
  118.   }
  119.  
  120.   public void mouseEntered(MouseEvent e) {
  121.   }
  122.  
  123.   public void mouseExited(MouseEvent e) {
  124.   }
  125.  
  126.   public void mousePressed(MouseEvent e) {
  127.   }
  128.  
  129.   public void mouseReleased(MouseEvent e) {
  130.   }
  131.  
  132.   public static void main(String[] args) {
  133.       final int s = 40;
  134.       final Cell[][] biosphere = new Cell[s][s];
  135.       final JPanel gui = new JPanel(new GridLayout(s,s,2,2));
  136.       for (int ii=0; ii<s; ii++) {
  137.           for (int jj=0; jj<s; jj++) {
  138.               Cell cell = new Cell(ii,jj);
  139.               cell.setPreferredSize(new Dimension(10,10));
  140.               gui.add(cell);
  141.               biosphere[ii][jj] = cell;
  142.           }
  143.       }
  144.  
  145.       ActionListener al = new ActionListener() {
  146.           public void actionPerformed(ActionEvent ae) {
  147.               boolean[][] living = new boolean[s][s];
  148.               for (int ii=0; ii<s; ii++) {
  149.                   for (int jj=0; jj<s; jj++) {
  150.                       int top = (jj>0 ? jj-1 : s-1);
  151.                       int btm = (jj<s-1 ? jj+1 : 0);
  152.                       int lft = (ii>0 ? ii-1 : s-1);
  153.                           int rgt = (ii<s-1 ? ii+1 : 0);
  154.                       int neighbors = 0;
  155.                       if ( biosphere[ii][top].isLiving() ) neighbors++;
  156.                       if ( biosphere[ii][btm].isLiving() ) neighbors++;
  157.                       if ( biosphere[lft][top].isLiving() ) neighbors++;
  158.                       if ( biosphere[lft][btm].isLiving() ) neighbors++;
  159.                       if ( biosphere[lft][jj].isLiving() ) neighbors++;
  160.                       if ( biosphere[rgt][jj].isLiving() ) neighbors++;
  161.                       if ( biosphere[rgt][top].isLiving() ) neighbors++;
  162.                       if ( biosphere[rgt][btm].isLiving() ) neighbors++;
  163.                       living[ii][jj] = biosphere[ii][jj].isAlive(neighbors);
  164.                   }
  165.               }
  166.               for (int ii=0; ii<s; ii++) {
  167.                   for (int jj=0; jj<s; jj++) {
  168.                       biosphere[ii][jj].setAlive( living[ii][jj] );
  169.                   }
  170.               }
  171.               gui.repaint();
  172.           }
  173.       };
  174.  
  175.       Timer timer = new Timer(50, al);
  176.       timer.start();
  177.  
  178.       JOptionPane.showMessageDialog(null, gui);
  179.       timer.stop();
  180.   }
  181. }