Advertisement
Guest User

GameOfLifeApplet

a guest
Feb 20th, 2012
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.applet.*;
  4.  
  5. public class GameOfLifeApplet extends Applet implements MouseListener,ActionListener  
  6. {
  7.   //the x and y coordinates to get the location of the clicked points
  8.   private int xCo, yCo;
  9.   private int diff_x, diff_y;
  10.   private GameOfLife game = new GameOfLife();
  11.   private Button nextButton = null;
  12.   public void init()
  13.   {
  14.     setLayout(null);
  15.     nextButton = new Button("Next Stage");
  16.     diff_x = diff_y = 600 / game.getGridSize();
  17.     nextButton.setLocation(250, 575);
  18.     nextButton.setSize(120, 30);
  19.     addMouseListener(this);
  20.   }
  21.  
  22.   private void drawEmptyGrid(Graphics g)
  23.   {
  24.     g.setColor(Color.white);
  25.     g.fillRect(0,0,600,600);
  26.     g.setColor(Color.black);
  27.     for(int i=0;i<game.getGridSize();++i)
  28.     {
  29.       g.drawLine(0,i*diff_x,600,i*diff_x);
  30.       g.drawLine(i*diff_x,0,i*diff_x,600);
  31.     }
  32.     g.setColor(Color.white);
  33.   }
  34.  
  35.   public void paint(Graphics g)
  36.   {
  37.     drawEmptyGrid(g);
  38.     add(nextButton);
  39.     g.setColor(Color.red);
  40.     for(int i=0;i<game.getGridSize();++i)
  41.     {
  42.       for(int j=0;j<game.getGridSize();++j)
  43.       {
  44.         if( game.grid[i][j] )
  45.         {
  46.           g.fillRect(i*diff_x,j*diff_y,diff_x,diff_y);
  47.         }
  48.       }
  49.     }
  50.     g.setColor(Color.white);
  51.   }
  52.  
  53.  // This method will be called when the mouse has been clicked.
  54.  public void mouseClicked (MouseEvent me) {
  55.  
  56.   // Save the coordinates of the click lke this.
  57.   xCo = me.getX();
  58.   yCo = me.getY();
  59.  
  60.   int x_init = xCo / diff_x;
  61.   int y_init = yCo / diff_y;
  62.  
  63.   game.grid[x_init][y_init] = true;
  64.   //show the results of the click
  65.   repaint();
  66.  
  67.  }
  68.  
  69.  // This is called when the mous has been pressed
  70.  public void mousePressed (MouseEvent me) {}
  71.  
  72.  // When it has been released
  73.  // not that a click also calls these Mouse-Pressed and Released.
  74.  // since they are empty nothing hapens here.
  75.  public void mouseReleased (MouseEvent me) {}
  76.  
  77.  // This is executed when the mouse enters the applet. it will only
  78.  // be executed again when the mouse has left and then re-entered.
  79.  public void mouseEntered (MouseEvent me) {}
  80.  
  81.  // When the Mouse leaves the applet.
  82.  public void mouseExited (MouseEvent me) {}  
  83.  
  84.  public void actionPerformed(ActionEvent evt)
  85.   {
  86.   // Here we will ask what component called this method
  87.     if (evt.getSource() == nextButton)
  88.     {
  89.       System.out.println("I got clicked!");
  90.       game.nextIteration();
  91.       repaint();
  92.     }
  93.   }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement