Glife1

Allikas: Lambda


import javax.swing.*;        
import java.awt.*;
import java.awt.event.*;
/*
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Timer;
*/

public class Glife1 {      
    
  
    // creates a small window with a text and a button in it
    public static void main(String[] args) {
      
        // create new frame 
      
        JFrame frame = new GLifeFrame();                          
       
        // react to window close, pack stuff, show out
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400,400);
        frame.setLocation(200,200);
        //frame.pack();  // give a suitable size to window automatically
        frame.setVisible(true);  // make window visible
        //panel.updateState(); // refresh panel
    }

}

class GLifeFrame extends JFrame {
  
    boolean debug=false;
    
    GLifeFrame frame;    
    JPanel toppanel;
    JPanel buttonpanel;
    DrawPanel drawpanel;
    JTextField textfield1, textfield2;
    JButton mybutton;
    JButton timerbutton;
    
    GLifeFrame() {
      
        // create a new window and panels
              
        toppanel = new JPanel();
        buttonpanel = new JPanel();
        drawpanel = new DrawPanel();
      
        // set layouts and colors of panels
      
        buttonpanel.setLayout(new FlowLayout());
        buttonpanel.setBackground(Color.yellow);    
        toppanel.setLayout(new BorderLayout());
        toppanel.setBackground(Color.black);      
        drawpanel.setBackground(Color.red);  
      
        // create and add components      
        
        // create a label widget with text, put on button panel
        //JLabel label = new JLabel("Life");         
        //buttonpanel.add(label);                 
        
        // Create and add a button to button panel
        mybutton = new JButton("Do next step");
        buttonpanel.add(mybutton); 
        timerbutton = new JButton("Start/stop timer");
        buttonpanel.add(timerbutton);
        mybutton.addActionListener(drawpanel);  
        timerbutton.addActionListener(drawpanel);  
        drawpanel.addMouseListener(drawpanel);
        Timer timer = new Timer(1000,drawpanel);
        timer.start();
              
        // add button and drawpanels to toppanel
        toppanel.add(BorderLayout.NORTH,buttonpanel);
        //ppanel.add(BorderLayout.WEST,buttonpanel1);
        //toppanel.add(BorderLayout.SOUTH,buttonpanel2);
        toppanel.add(BorderLayout.CENTER,drawpanel);
      
        // add toppanel to the window	
        this.getContentPane().add(toppanel);		                    
                                                               
    }

// Here starts the INTERNAL drawing panel class

  class DrawPanel extends JPanel 
     implements ActionListener, MouseListener {            
          
    int drawwidth;  
    int drawheight; 

    int cellsize=10;
      
    int cellareadim=200; 
    int steps;
    int[][] cells;
    int[][] newcells;  
    int[][] tmpcells; // temporary array pointer, not allocated
    int count;      
   
    // Class constructor function: called when class is created
      
    DrawPanel() {
      init();             
    }  
    
    // mouse stuff
    
    public void mousePressed(MouseEvent evt) {
      int x,y;  
      int i,j;  
      if (debug) System.out.print("mouse button was pressed"); 
      x=evt.getX();
      y=evt.getY();   
      if (debug) System.out.println(" coords "+x+" and "+y);  
      i=(int)(x/cellsize); // x=12 then i=1
      j=(int)(y/cellsize);  
      if (i<cellareadim && j<cellareadim) {  
        if (cells[i][j]==0)  cells[i][j]=1;
        else cells[i][j]=0;               
        //this.repaint();          
        toppanel.repaint();  
      }    
        
      //Component source = (Component)evt.getSource();
      //source.repaint();  // Call repaint() on the Component that was clicked.
    }

    public void mouseClicked(MouseEvent evt) { }
    public void mouseReleased(MouseEvent evt) { }
    public void mouseEntered(MouseEvent evt) { }
    public void mouseExited(MouseEvent evt) { }
    
    // button stuff
    
    public void actionPerformed(ActionEvent e) {
      if (debug) System.out.println("button was pressed");
      steps++;  
      if (debug) System.out.println("Step nr: "+steps);
      if (debug) printCells(cells);
      count=countCells(cells);
      if (count==0) {
        if (debug) System.out.println("Everybody dead at step nr: "+steps);
        return;         
      }   
      compNextGen(cells,newcells);      
      // swap two arrays: cells will become newcells, and vice versa  
      tmpcells=cells;  
      cells=newcells;
      newcells=tmpcells;
      // just draw the new situation
      toppanel.repaint();                    
    }
  
    // "reading" initialisation function
    
    void init() {     	
      if (debug) System.out.println("Init called");  
      cells=new int[cellareadim][cellareadim];
      newcells=new int[cellareadim][cellareadim];  
      initCells(cells);  
    } 	          
    
    // Drawing function
    
    public void paintComponent(Graphics g) {
       int i,j;  
       
       g.setColor(Color.GREEN);       
       for(i=0;i<cellareadim;i++) {
         for(j=0;j<cellareadim;j++) {
           // if cells[i][j]==1 then draw dot
           if (cells[i][j]==1) {
              g.fillRect(i*cellsize,j*cellsize,
                         cellsize,cellsize);
           }                
         } 
       }  
         
                                           
    }      
  
  // compute next generation of cells:
  // old: cells as they were before, will not be changed
  // next: next generation of cells, written anew
  
  void compNextGen(int[][] old,int[][] next) {
    int row,col;
    int n;  
    for(row=0;row<old.length;row++) {
      for(col=0;col<old[row].length;col++) {
        // compute nr of neighbours of one cell 
        n=cval(old,row-1,col-1)+cval(old,row-1,col)+
            cval(old,row-1,col+1);
        n=n+cval(old,row,col-1)+cval(old,row,col+1);
        n=n+cval(old,row+1,col-1)+cval(old,row+1,col)+
            cval(old,row+1,col+1);
        // compute next gen cell: dead or alive  
        if (old[row][col]==1 && n<2) next[row][col]=0;
        else if (old[row][col]==1 && n>3) next[row][col]=0;
        else if (old[row][col]==1 && (n==2 || n==3)) next[row][col]=1;          
        else if (old[row][col]==0 && n==3) next[row][col]=1;
        else next[row][col]=0;  
      }          
    }           
  }    
  
  // allows to calc neigbour values for cells on borders
  
  int cval(int[][] cells,int r, int c) {
    if (r<0 || r>=cells.length) return 0;
    if (c<0 || c>=cells[0].length) return 0;  
    return cells[r][c];                  
  }    
  
  // give initial position
  
  void initCells(int[][] cells) {
    initCells2(cells);        
  }  
  
  void initCells1(int[][] cells) {
    cells[1][1]=1;
    cells[3][1]=1;  
    cells[3][2]=1;
    cells[4][1]=1;  
    cells[4][2]=1;  
    cells[5][1]=1;          
  }    
  
  void initCells2(int[][] cells) {
    cells[1][1]=1;
    cells[1][2]=1;
    cells[1][3]=1;       
  }  
  
  // just print current cells
  
  void printCells(int[][] cells) {
    int row,col;
    for(row=0;row<cells.length;row++) {
      for(col=0;col<cells[row].length;col++) {
        if (cells[row][col]==1) {
          System.out.print("*");
        } else {
          System.out.print(" ");  
        }             
      }
      System.out.println();      
    }              
  }    
  
  // just count current cells
  
  int countCells(int[][] cells) {
    int row,col,count=0;
    for(row=0;row<cells.length;row++) {
      for(col=0;col<cells[row].length;col++) {
        count=count+cells[row][col];        
      }
    }  
    return count;    
  }     
    
  }   

}