Advertisement
Vita_Harvey

SudokuBoard_A

Jan 27th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.96 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.     Region region;
  33.      
  34.     // Create an empty 2D array of JPanels the size
  35.     // of the underlying board element (SudokuBase).
  36.     // This shall serve as our array of Regions, which in turn are 2D arrays of Cells.
  37.     //JPanel[][] regions;
  38.        
  39.     /** Our class constructor.
  40.      * @param SudokuBase b.
  41.      *
  42.     */    
  43.     public SudokuBoard(SudokuBase b) {
  44.        
  45.         // Gets the number of rows and columns from the SudokuBase/Stub object b passed to our constructor.
  46.         rows = b.getRows();
  47.         columns = b.getColumns();
  48.         size = b.getSize();
  49.        
  50.         // Instantiate b, our SudokuStub/Base object, SudokuBoard's sole parameter and underlying element.
  51.         this.b = new SudokuStub(rows, columns);
  52.        
  53.         // Determine the layout scheme of the overall board structure.
  54.         grid = new GridLayout(this.rows, this.columns, 2, 2);
  55.        
  56.         // Creates our overall "board" element, one big JPanel, to set into container (JFrame, frame).    
  57.         JPanel board = new JPanel(grid);
  58.            
  59.         // An index tracking position of the current region on the overall board.
  60.         // I thought this might come in useful in later stages of this project.
  61.         int regionIndex = 0;
  62.          
  63.         // Iterate thru loops and populate 2D array, "cells", with cells made using our Cell helper class.        
  64.         for (int row = 0; row < rows; row++) {
  65.        
  66.             for (int col = 0; col < columns; col++) {
  67.            
  68.                // make new region with Region class and add to board.
  69.                region = new Region(this.b);              
  70.                board.add(region);
  71.                              
  72.                // Update x to reflect how many pixels traversed from left to right.
  73.                x = (50 * col);
  74.                
  75.                // Update the region index to reflect current total number of regions.
  76.                regionIndex++;
  77.                
  78.                // This determines whether region gets shading or not.
  79.                // I wrote it in if/else if form rather than using || so that
  80.                // my program could employ short-circuit evaluation and, in doing so,
  81.                // conserve computer resources.
  82.                if ((row % 2 == 0) && (col % 2 == 0)) {                
  83.                   region.setBackground(Color.WHITE);                        
  84.                } else if ((row % 2 == 1) && (col % 2 == 1)) {
  85.                   region.setBackground(Color.WHITE);                  
  86.                }else {
  87.                   region.setBackground(new Color(220, 220, 220));
  88.                }            
  89.             }
  90.                        
  91.             // Reset x once a row of columns has been iterated through.
  92.             x = 0;
  93.             // Update y to reflect how many pixels traversed from top to bottom.
  94.             y = (50 * row);
  95.         }
  96.        
  97.         // Creates the container window onto which we place our board.
  98.         JFrame frame = new JFrame (rows + "x" + columns);
  99.         frame.setPreferredSize(new Dimension(rows*70, columns*70));
  100.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  101.         frame.add(board);
  102.         frame.getContentPane().setLayout(grid);
  103.         frame.pack();
  104.         frame.setVisible(true);
  105.        
  106.         board.setPreferredSize(new Dimension(x, y));
  107.         board.setBorder(BorderFactory.createLineBorder(Color.BLACK));
  108.         board.setLayout(grid);
  109.         board.setVisible(true);
  110.                                              
  111.         this.repaint();              
  112.     }
  113.        
  114.     /**
  115.     * This method renders one large rectangle border.
  116.     * @param g The Graphics object use to render
  117.     */
  118.     @Override
  119.     public void paintComponent(java.awt.Graphics g) {
  120.        
  121.         // paint the underlying component
  122.         super.paintComponent(g);
  123.                              
  124.         // set the color of the outline        
  125.         g.setColor(Color.BLACK);
  126.        
  127.         // Set the size of board matrix.
  128.         // width is the number of columns times 50 pixels wide, plus the number of
  129.         // columns plus 1 to account for borders.
  130.         int width = (columns*50) + (columns + 1);
  131.        
  132.         // height is the number of rows in the board  times 50 pixels, plus the
  133.         // number of columns plus 1 to account for borders.
  134.         int height = (rows*50) + (rows + 1);
  135.        
  136.         g.drawRect(0, 0, width, height);              
  137.     }
  138.      
  139.     /*
  140.      *@param None
  141.      *@return b
  142.      */              
  143.     SudokuBase getBase() {
  144.         return b;
  145.     }
  146.    
  147.     public static void main(String[] args) {
  148.     //
  149. //         Region stub1 = new Region(new SudokuStub(3, 3));
  150. //         Region stub2 = new Region(new SudokuStub(2, 3));
  151. //         Region stub3 = new Region(new SudokuStub(4, 4));
  152.        
  153.         // Instantiate SudokuStub to pass to our constructor.
  154.         SudokuStub stub1 = new SudokuStub(3, 3);
  155.         SudokuStub stub2 = new SudokuStub(2, 3);
  156.         SudokuStub stub3 = new SudokuStub(4, 4);
  157.                
  158.         SudokuBoard game1 = new SudokuBoard(stub1);
  159.         SudokuBoard game2 = new SudokuBoard(stub2);
  160.         SudokuBoard game3 = new SudokuBoard(stub3);
  161.  
  162.            
  163.     }
  164.    
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement