Guest User

Untitled

a guest
Apr 20th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.31 KB | None | 0 0
  1. /********************************************************************
  2. BoardPanel.java
  3. /*********************************************************************/
  4.  
  5. package life;
  6.  
  7. import javax.swing.JPanel;
  8. import java.awt.*;
  9. import java.awt.event.*;
  10.  
  11. public class BoardPanel extends JPanel
  12. {
  13.     private Point currentPoint;
  14.     private int height;
  15.     private int width;
  16.     private int cellSize;
  17.    
  18.     private GameModel model;
  19.     private BoardPanel boardPanel;
  20.  
  21.     public BoardPanel(int boardHeight, int boardWidth, GameModel model, int cellSize)
  22.     {
  23.         // register with the mouse listener
  24.         addMouseListener (new BoardListener());
  25.      
  26.         //panel background id white
  27.         setBackground (Color.white);
  28.      
  29.         //set the board to the size of the arrays x the size of a square
  30.         this.cellSize = cellSize;
  31.         this.height = (boardHeight * cellSize);
  32.         this.width = (boardWidth * cellSize);
  33.         setPreferredSize(new Dimension((height),(width)));
  34.        
  35.         this.model = new GameModel(model);
  36.     }
  37.  
  38.    public void paintComponent (Graphics g)
  39.    {
  40.       super.paintComponent (g);
  41.      
  42.       g.setColor (Color.black);
  43.      
  44.      //draw the grid
  45.      for(int y=0; y < height; y+= cellSize)
  46.      {
  47.         for(int x = 0; x < width; x+= cellSize)
  48.         {
  49.             g.drawRect(x,y, cellSize, cellSize);
  50.         }
  51.        
  52.      }
  53.        
  54.         g.setColor(Color.red);
  55.         Graphics2D g2d = ( Graphics2D ) g;
  56.  
  57.        
  58.         for(int i = 0; i < (height/cellSize); i++)
  59.         {
  60.             for(int j = 0; j < (width/cellSize); j++)
  61.             {  
  62.                 System.out.println(i + " " + j);
  63.                 if ((model.getCellState(model.getCurrentBoard(), i, j)) == true)
  64.                 {
  65.                     g2d.fill3DRect(j*cellSize, i*cellSize, cellSize, cellSize, true);
  66.                 }
  67.             }
  68.         }
  69.        
  70.     }
  71.     /*
  72.     public void paintSquares(Graphics g, GameModel model)
  73.     {
  74.         super.paintComponent(g);
  75.        
  76.         g.setColor(Color.red);
  77.         Graphics2D g2d = ( Graphics2D ) g;
  78.  
  79.        
  80.         for(int i = 0; i < (height/cellSize); i++)
  81.         {
  82.             for(int j = 0; j < (width/cellSize); j++)
  83.             {  
  84.                 System.out.println(i + " " + j);
  85.                 if ((model.getCellState(model.getCurrentBoard(), i, j)) == true)
  86.                 {
  87.                     g2d.fill3DRect(j*cellSize, i*cellSize, cellSize, cellSize, true);
  88.                 }
  89.             }
  90.         }
  91.        
  92.     }*/
  93.    
  94.          
  95.     //*****************************************************************
  96.    //  Private inner class
  97.    //  Represents the listener for mouse events. Demonstrates the
  98.    //  ability to extend an adaptor class.
  99.    //  Just A stub - no functionality in this demonstration
  100.    //*****************************************************************
  101.    private class BoardListener extends MouseAdapter
  102.    {
  103.       //--------------------------------------------------------------
  104.       //  Computes the distance from the mouse pointer to the center
  105.       //  point of the frame.
  106.       //--------------------------------------------------------------
  107.       public void mouseClicked (MouseEvent event)
  108.       {
  109.          currentPoint = event.getPoint();
  110.          repaint();
  111.       }
  112.    }
  113.  }
Add Comment
Please, Sign In to add comment