Advertisement
Vita_Harvey

SudokuBoard_C

Feb 4th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.82 KB | None | 0 0
  1. package csc143.sudoku;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5.  
  6. /**
  7.  * @author Vita Wiebe
  8.  * @version PA2
  9.  * A class to make a SudokuBoard.
  10.  */
  11. public class SudokuBoard extends JPanel {
  12.    
  13.     // Our class fields.
  14.     private int rows;
  15.     private int columns;
  16.    
  17.     // "Size"  is how many cells are in the Sudoku board, total.
  18.     private int size;
  19.    
  20.     // Kept package private so can be accessed by both Region and SudokuBoard.
  21.     SudokuStub b;
  22.    
  23.     // The building blocks of our board.
  24.     Region region;
  25.        
  26.     /** Our class constructor.
  27.      * @param SudokuBase b.
  28.      *
  29.      */    
  30.     public SudokuBoard(SudokuBase b) {
  31.        
  32.         // Gets the number of rows and columns from the SudokuBase/Stub object b passed to our constructor.
  33.         rows = b.getRows();
  34.         columns = b.getColumns();
  35.         size = b.getSize();
  36.        
  37.         // Instantiate b, our SudokuStub/Base object, SudokuBoard's sole parameter and underlying element.
  38.         this.b = new SudokuStub(rows, columns);
  39.        
  40.         // Determine the layout scheme of the overall board structure.
  41.         setLayout(new GridLayout(rows, columns, 2, 2));
  42.        
  43.         // Iterate thru loops and populate our SudokuBoard with Regions from the Region helper class.        
  44.         for (int row = 0; row < rows; row++) {
  45.        
  46.             for (int col = 0; col < columns; col++) {
  47.            
  48.            
  49.                // make new region with Region class and add to board.
  50.                region = new Region(this.b);                                                        
  51.                // This determines whether region gets shading or not.
  52.                // I wrote it in if/else if form rather than using || so that
  53.                // my program could employ short-circuit evaluation and, in doing so,
  54.                // conserve computer resources.
  55.                if ((row % 2 == 0) && (col % 2 == 0)) {
  56.                   region.setBackground(Color.WHITE);                                        
  57.                } else if ((row % 2 == 1) && (col % 2 == 1)) {
  58.                   region.setBackground(Color.WHITE);                  
  59.                } else {
  60.                   region.setBackground(new Color(220, 220, 220));
  61.                }              
  62.                add(region);                                          
  63.             }                      
  64.         }                                                                      
  65.     }
  66.    
  67.     /**
  68.     * This method renders one large rectangle border.
  69.     * @param g The Graphics object use to render
  70.     */
  71.     @Override
  72.     public void paintComponent(java.awt.Graphics g) {
  73.        
  74.         // paint the underlying component
  75.         super.paintComponent(g);
  76.                              
  77.         // set the color of the outline        
  78.         g.setColor(Color.BLACK);
  79.        
  80.         // Set the size of outside black rectangle.
  81.         // width is the number of columns times 50 pixels wide, plus the number of
  82.         // columns plus 1 to account for borders.
  83.         int width = (columns*50) + (columns + 1);
  84.        
  85.         // height is the number of rows in the board  times 50 pixels, plus the
  86.         // number of columns plus 1 to account for borders.
  87.         int height = (rows*50) + (rows + 1);
  88.        
  89.         g.drawRect(0, 0, width, height);              
  90.     }
  91.      
  92.     /*
  93.      *@param None
  94.      *@return b
  95.      */              
  96.     SudokuBase getBase() {
  97.         return b;
  98.     }
  99.    
  100.     public static void main(String[] args) {
  101.        
  102.         javax.swing.JFrame win = new javax.swing.JFrame("Test 2x3");
  103.         win.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
  104.         win.add(new SudokuBoard(new SudokuStub(2, 3)));
  105.         win.pack();
  106.         win.setVisible(true);
  107.            
  108.     }
  109.    
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement