Advertisement
Vita_Harvey

SudokuBoard_E

Feb 24th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.97 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.                // This determines whether region gets shading or not.
  60.                // I wrote it in if/else if form rather than using || so that
  61.                // my program could employ short-circuit evaluation and, in doing so,
  62.                // conserve computer resources.
  63.                if ((row % 2 == 0) && (col % 2 == 0)) {
  64.                   region.setBackground(Color.WHITE);                                        
  65.                } else if ((row % 2 == 1) && (col % 2 == 1)) {
  66.                   region.setBackground(Color.WHITE);                  
  67.                } else {
  68.                   region.setBackground(new Color(220, 220, 220));
  69.                }                            
  70.                add(region);                                                        
  71.             }                      
  72.         }
  73.                      
  74.     }
  75.            
  76.     /**
  77.     * This method renders one large rectangle border.
  78.     * @param g The Graphics object use to render
  79.     */
  80.     @Override
  81.     public void paintComponent(java.awt.Graphics g) {
  82.        
  83.         // paint the underlying component
  84.         super.paintComponent(g);
  85.                              
  86.         // set the color of the outline        
  87.         g.setColor(Color.BLACK);
  88.        
  89.         // Set the size of outside black rectangle.
  90.         // width is the number of columns times 50 pixels wide, plus the number of
  91.         // columns plus 1 to account for borders.
  92.         int width = (columns*50) + (columns + 1);
  93.        
  94.         // height is the number of rows in the board  times 50 pixels, plus the
  95.         // number of columns plus 1 to account for borders.
  96.         int height = (rows*50) + (rows + 1);
  97.        
  98.         g.drawRect(0, 0, width, height);              
  99.     }
  100.      
  101.     /*
  102.      *@param None
  103.      *@return b
  104.      */              
  105.     SudokuBase getBase() {
  106.         return b;
  107.     }
  108.    
  109.    /**
  110.     * Set the selected cell to the given row and column.
  111.     * @param row The indicated row
  112.     * @param col The indicated column
  113.     * @throws IllegalArgumentException
  114.     */
  115.    public void setSelected(int row, int col) {
  116.       if ((row < 0) || (col < 0)) {
  117.           throw new IllegalArgumentException("Values must be greater than "
  118.            + "or equal to 0.");
  119.       } else{
  120.           this.selectedRow = row;
  121.           this.selectedColumn = col;
  122.       }
  123.    }
  124.    
  125.    /**
  126.     * Retrive the row of the currently selected cell.
  127.     * @return The row in which the selected cell is located.
  128.     */
  129.    public int getSelectedRow() {
  130.        return this.selectedRow;
  131.    }  
  132.    
  133.    /**
  134.     * Retrive the column of the currently selected cell.
  135.     * @return The column in which the selected cell is located.
  136.     */
  137.    public int getSelectedColumn(){
  138.        return this.selectedColumn;
  139.    }
  140.  
  141.    
  142.     public static void main(String[] args) {
  143.        
  144.         javax.swing.JFrame win = new javax.swing.JFrame("Test 2x3");
  145.         win.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
  146.         win.add(new SudokuBoard(new SudokuStub(2, 3)));
  147.         win.pack();
  148.         win.setVisible(true);
  149.            
  150.     }
  151.    
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement