Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import java.applet.*;
- public class GameOfLifeApplet extends Applet implements MouseListener,ActionListener
- {
- //the x and y coordinates to get the location of the clicked points
- private int xCo, yCo;
- private int diff_x, diff_y;
- private GameOfLife game = new GameOfLife();
- private Button nextButton = null;
- public void init()
- {
- setLayout(null);
- nextButton = new Button("Next Stage");
- diff_x = diff_y = 600 / game.getGridSize();
- nextButton.setLocation(250, 575);
- nextButton.setSize(120, 30);
- addMouseListener(this);
- }
- private void drawEmptyGrid(Graphics g)
- {
- g.setColor(Color.white);
- g.fillRect(0,0,600,600);
- g.setColor(Color.black);
- for(int i=0;i<game.getGridSize();++i)
- {
- g.drawLine(0,i*diff_x,600,i*diff_x);
- g.drawLine(i*diff_x,0,i*diff_x,600);
- }
- g.setColor(Color.white);
- }
- public void paint(Graphics g)
- {
- drawEmptyGrid(g);
- add(nextButton);
- g.setColor(Color.red);
- for(int i=0;i<game.getGridSize();++i)
- {
- for(int j=0;j<game.getGridSize();++j)
- {
- if( game.grid[i][j] )
- {
- g.fillRect(i*diff_x,j*diff_y,diff_x,diff_y);
- }
- }
- }
- g.setColor(Color.white);
- }
- // This method will be called when the mouse has been clicked.
- public void mouseClicked (MouseEvent me) {
- // Save the coordinates of the click lke this.
- xCo = me.getX();
- yCo = me.getY();
- int x_init = xCo / diff_x;
- int y_init = yCo / diff_y;
- game.grid[x_init][y_init] = true;
- //show the results of the click
- repaint();
- }
- // This is called when the mous has been pressed
- public void mousePressed (MouseEvent me) {}
- // When it has been released
- // not that a click also calls these Mouse-Pressed and Released.
- // since they are empty nothing hapens here.
- public void mouseReleased (MouseEvent me) {}
- // This is executed when the mouse enters the applet. it will only
- // be executed again when the mouse has left and then re-entered.
- public void mouseEntered (MouseEvent me) {}
- // When the Mouse leaves the applet.
- public void mouseExited (MouseEvent me) {}
- public void actionPerformed(ActionEvent evt)
- {
- // Here we will ask what component called this method
- if (evt.getSource() == nextButton)
- {
- System.out.println("I got clicked!");
- game.nextIteration();
- repaint();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement