Advertisement
Vita_Harvey

SudokuBoard_B

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