Advertisement
Vita_Harvey

SudokuBoard_A

Jan 27th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.46 KB | None | 0 0
  1. package csc143.sudoku;
  2.  
  3. import javax.swing.*;
  4. import csc143.sudoku.*;
  5. import java.awt.*;
  6.  
  7. /**
  8.  * @author Vita Wiebe
  9.  * @version PA2
  10.  * A class to make a SudokuBoard.
  11.  */
  12. public class SudokuBoard extends JPanel
  13.    // implements SelectedCell
  14. {
  15.     // Our class fields.
  16.     private int rows;
  17.     private int columns;
  18.    
  19.     // "Size"  is how many cells are in the Sudoku board, total.
  20.     private int size;
  21.    
  22.     // X and Y track the pixel position in the cell matrix.
  23.     // This can be used to calculate the area of each region in the current matrix.
  24.     private int x;
  25.     private int y;
  26.    
  27.     GridLayout grid;
  28.    
  29.     // Kept package private so can be accessed by both Region and SudokuBoard.
  30.     SudokuStub b;
  31.      
  32.     // Create an empty 2D array of JPanels the size
  33.     // of the underlying board element (SudokuBase).
  34.     // This shall serve as our array of Regions, which in turn are 2D arrays of Cells.
  35.     //JPanel[][] regions;
  36.        
  37.     /** Our class constructor.
  38.      * @param SudokuBase b.
  39.      *
  40.     */    
  41.     public SudokuBoard(Region r) {
  42.        
  43.         // Gets the number of rows and columns from the SudokuBase/Stub object b passed to our constructor.
  44.         rows = b.getRows();
  45.         columns = b.getColumns();
  46.         size = b.getSize();
  47.        
  48.         // Instantiate b, our SudokuStub/Base object, SudokuBoard's sole parameter and underlying element.
  49.         this.b = new SudokuStub(rows, columns);
  50.        
  51.         // Determine the layout scheme of the overall board structure.
  52.         grid = new GridLayout(this.rows, this.columns, 2, 2);
  53.        
  54.         // Instantiate our 2D JPanel array, "regions".
  55.         // Do I need this to track shading of regions? Or do I just put each Region directly onto GridLayout-set
  56.         // "board"?
  57.         //JPanel[][] regions = new JPanel[rows][columns];
  58.        
  59.         // Creates our overall "board" element, one big JPanel, to set into container (JFrame, frame).    
  60.         JPanel board = new JPanel(grid);
  61.            
  62.         // An index tracking position of the current region on the overall board..
  63.         int regionIndex = 0;
  64.          
  65.         // Iterate thru loops and populate 2D array, "cells", with cells made using our Cell helper class.        
  66.         for (int row = 0; row < rows; row++) {
  67.             for (int col = 0; col < columns; col++) {
  68.                // make new region with Region class.
  69.                Region region = new Region();
  70.                //regions[row][col] = region;
  71.                board.add(region);              
  72.                // Update x to reflect how many pixels traversed from left to right.
  73.                x = (50 * col);
  74.                // Update the region index.
  75.                regionIndex++;
  76.                if (regionIndex % 2 == 0) {
  77.                   region.setBackground(Color.WHITE);
  78.                  
  79.                } else {
  80.                   region.setBackground(new Color(220, 220, 220));
  81.                  }
  82.                
  83.             }            
  84.             // Reset x once a row of columns has been iterated through.
  85.             x = 0;
  86.             // Update y to reflect how many pixels traversed from top to bottom.
  87.             y = (50 * row);
  88.         }
  89.        
  90.         JFrame frame = new JFrame (rows + "x" + columns);
  91.         frame.setSize(rows*70, columns*70);
  92.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  93.         frame.add(board);
  94.         frame.getContentPane().setLayout(grid);
  95.         frame.pack();
  96.         frame.setVisible(true);
  97.        
  98.         board.setPreferredSize(new Dimension(x, y));
  99.         board.setBorder(BorderFactory.createLineBorder(Color.BLACK));
  100.         board.setLayout(grid);
  101.         board.setVisible(true);
  102.                                              
  103.         this.repaint();              
  104.     }
  105.        
  106.     /**
  107.     * This method renders one large rectangle border.
  108.     * @param g The Graphics object use to render
  109.     */
  110.     @Override
  111.     public void paintComponent(java.awt.Graphics g) {
  112.        
  113.         // paint the underlying component
  114.         super.paintComponent(g);
  115.                              
  116.         // set the color of the outline        
  117.         g.setColor(Color.BLACK);
  118.        
  119.         // Set the size of board matrix.
  120.         // width is the number of columns times 50 pixels wide, plus the number of
  121.         // columns plus 1 to account for borders.
  122.         int width = (columns*50) + (columns + 1);
  123.        
  124.         // height is the number of rows in the board  times 50 pixels, plus the
  125.         // number of columns plus 1 to account for borders.
  126.         int height = (rows*50) + (rows + 1);
  127.        
  128.         g.drawRect(0, 0, width, height);              
  129.     }
  130.      
  131.      /*
  132.       *@param None
  133.       *@return b
  134.       */              
  135.      SudokuBase getBase() {
  136.         return b;
  137.     }
  138.    
  139.     public static void main(String[] args) {
  140.    
  141.         // Region region1 = new Region(new SudokuStub(3, 3));
  142. //         Region region2 = new Region(new SudokuStub(2, 3));
  143. //         Region region3 = new Region(new SudokuStub(4, 4));
  144.        
  145.         // Instantiate SudokuStub to pass to our constructor.
  146.         SudokuStub stub1 = new SudokuStub(3, 3);
  147.         SudokuStub stub2 = new SudokuStub(2, 3);
  148.         SudokuStub stub3 = new SudokuStub(4, 4);
  149.                
  150.         SudokuBoard game1 = new SudokuBoard(stub1);
  151.         SudokuBoard game2 = new SudokuBoard(stub2);
  152.         SudokuBoard game3 = new SudokuBoard(stub3);      
  153.     }
  154.    
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement