Advertisement
Vita_Harvey

SudokuBoard_G

Mar 3rd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.22 KB | None | 0 0
  1. package csc143.sudoku;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.MouseAdapter;
  6. import java.awt.event.MouseEvent;
  7.  
  8. /**
  9.  * @author Vita Wiebe
  10.  * @version PA2
  11.  * A class to make a SudokuBoard.
  12.  */
  13. public class SudokuBoard extends JPanel
  14.    implements SelectedCell {
  15.    
  16.     // Our class fields.
  17.    
  18.     private int rows;
  19.     private int columns;
  20.    
  21.     // These are associated with methods of SelectedCell interface
  22.     int selectedRow;
  23.     int selectedColumn;
  24.    
  25.     // "Size"  is how many cells are in the Sudoku board, total.
  26.     private int size;
  27.    
  28.     // Kept package private so can be accessed by both Region and SudokuBoard.
  29.     SudokuStub b;
  30.    
  31.     // The building blocks of our board.
  32.     Region region;
  33.        
  34.     /** Our class constructor.
  35.      * @param SudokuBase b.
  36.      *
  37.      */    
  38.     public SudokuBoard(SudokuBase b) {
  39.        
  40.         // Gets the number of rows and columns from the SudokuBase/Stub object b passed to our constructor.
  41.         rows = b.getRows();
  42.         columns = b.getColumns();
  43.         size = b.getSize();
  44.        
  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.         setLayout(new GridLayout(rows, columns, 2, 2));
  51.        
  52.         // Iterate thru loops and populate our SudokuBoard with Regions from the Region helper class.        
  53.         for (int row = 0; row < rows; row++) {
  54.        
  55.             for (int col = 0; col < columns; col++) {
  56.                                    
  57.                // make new region with Region class and add to board.
  58.                region = new Region(this.b);
  59.                                                                        
  60.                // This determines whether region gets shading or not.
  61.                // I wrote it in if/else if form rather than using || so that
  62.                // my program could employ short-circuit evaluation and, in doing so,
  63.                // conserve computer resources.
  64.                if ((row % 2 == 0) && (col % 2 == 0)) {
  65.                   region.setBackground(Color.WHITE);                                        
  66.                } else if ((row % 2 == 1) && (col % 2 == 1)) {
  67.                   region.setBackground(Color.WHITE);                  
  68.                } else {
  69.                   region.setBackground(new Color(220, 220, 220));
  70.                }                            
  71.                add(region);                                                        
  72.             }                      
  73.         }
  74.                      
  75.     }
  76.            
  77.     /**
  78.     * This method renders one large rectangle border.
  79.     * @param g The Graphics object use to render
  80.     */
  81.     @Override
  82.     public void paintComponent(java.awt.Graphics g) {
  83.        
  84.         // paint the underlying component
  85.         super.paintComponent(g);
  86.                              
  87.         // set the color of the outline        
  88.         g.setColor(Color.BLACK);
  89.        
  90.         // Set the size of outside black rectangle.
  91.         // width is the number of columns times 50 pixels wide, plus the number of
  92.         // columns plus 1 to account for borders.
  93.         int width = (columns*50) + (columns + 1);
  94.        
  95.         // height is the number of rows in the board  times 50 pixels, plus the
  96.         // number of columns plus 1 to account for borders.
  97.         int height = (rows*50) + (rows + 1);
  98.        
  99.         g.drawRect(0, 0, width, height);              
  100.     }
  101.      
  102.     /*
  103.      *@param None
  104.      *@return b
  105.      */              
  106.     SudokuBase getBase() {
  107.         return b;
  108.     }
  109.    
  110.    /**
  111.     * Set the selected cell to the given row and column.
  112.     * @param row The indicated row
  113.     * @param col The indicated column
  114.     * @throws IllegalArgumentException
  115.     */
  116.    public void setSelected(int row, int col) {
  117.       if ((row < 0) || (col < 0)) {
  118.           throw new IllegalArgumentException("Values must be greater than "
  119.            + "or equal to 0.");
  120.       } else{
  121.           this.selectedRow = row;
  122.           this.selectedColumn = col;
  123.       }
  124.    }
  125.    
  126.    /**
  127.     * Retrive the row of the currently selected cell.
  128.     * @return The row in which the selected cell is located.
  129.     */
  130.    public int getSelectedRow() {
  131.        return this.selectedRow;
  132.    }  
  133.    
  134.    /**
  135.     * Retrive the column of the currently selected cell.
  136.     * @return The column in which the selected cell is located.
  137.     */
  138.    public int getSelectedColumn(){
  139.        return this.selectedColumn;
  140.    }
  141.  
  142.    
  143.     public static void main(String[] args) {
  144.        
  145.         javax.swing.JFrame win = new javax.swing.JFrame("Test 2x3");
  146.         win.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
  147.         win.add(new SudokuBoard(new SudokuStub(2, 3)));
  148.         win.pack();
  149.         win.setVisible(true);
  150.        
  151.         win = new javax.swing.JFrame("Test 3x3");
  152.         win.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
  153.         win.add(new SudokuBoard(new SudokuStub(3, 3)));
  154.         win.pack();
  155.         win.setVisible(true);
  156.            
  157.     }
  158.    
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement