Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********************************************************************
- BoardPanel.java
- /*********************************************************************/
- package life;
- import javax.swing.JPanel;
- import java.awt.*;
- import java.awt.event.*;
- public class BoardPanel extends JPanel
- {
- private Point currentPoint;
- private int height;
- private int width;
- private int cellSize;
- private GameModel model;
- private BoardPanel boardPanel;
- public BoardPanel(int boardHeight, int boardWidth, GameModel model, int cellSize)
- {
- // register with the mouse listener
- addMouseListener (new BoardListener());
- //panel background id white
- setBackground (Color.white);
- //set the board to the size of the arrays x the size of a square
- this.cellSize = cellSize;
- this.height = (boardHeight * cellSize);
- this.width = (boardWidth * cellSize);
- setPreferredSize(new Dimension((height),(width)));
- this.model = new GameModel(model);
- }
- public void paintComponent (Graphics g)
- {
- super.paintComponent (g);
- g.setColor (Color.black);
- //draw the grid
- for(int y=0; y < height; y+= cellSize)
- {
- for(int x = 0; x < width; x+= cellSize)
- {
- g.drawRect(x,y, cellSize, cellSize);
- }
- }
- g.setColor(Color.red);
- Graphics2D g2d = ( Graphics2D ) g;
- for(int i = 0; i < (height/cellSize); i++)
- {
- for(int j = 0; j < (width/cellSize); j++)
- {
- System.out.println(i + " " + j);
- if ((model.getCellState(model.getCurrentBoard(), i, j)) == true)
- {
- g2d.fill3DRect(j*cellSize, i*cellSize, cellSize, cellSize, true);
- }
- }
- }
- }
- /*
- public void paintSquares(Graphics g, GameModel model)
- {
- super.paintComponent(g);
- g.setColor(Color.red);
- Graphics2D g2d = ( Graphics2D ) g;
- for(int i = 0; i < (height/cellSize); i++)
- {
- for(int j = 0; j < (width/cellSize); j++)
- {
- System.out.println(i + " " + j);
- if ((model.getCellState(model.getCurrentBoard(), i, j)) == true)
- {
- g2d.fill3DRect(j*cellSize, i*cellSize, cellSize, cellSize, true);
- }
- }
- }
- }*/
- //*****************************************************************
- // Private inner class
- // Represents the listener for mouse events. Demonstrates the
- // ability to extend an adaptor class.
- // Just A stub - no functionality in this demonstration
- //*****************************************************************
- private class BoardListener extends MouseAdapter
- {
- //--------------------------------------------------------------
- // Computes the distance from the mouse pointer to the center
- // point of the frame.
- //--------------------------------------------------------------
- public void mouseClicked (MouseEvent event)
- {
- currentPoint = event.getPoint();
- repaint();
- }
- }
- }
Add Comment
Please, Sign In to add comment