Advertisement
Vita_Harvey

SudokuBoard_H

Mar 15th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.07 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 PAx: Sudoku Serialization and Make-Up
  11.  * A class to make create the SudokuBoard's visual functionality.
  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.     // The building blocks of our Regions, Cells
  22.     private Cell cell;
  23.     // "chosenOne" is the currently selected cell.    
  24.     private Cell chosenOne;
  25.    
  26.     // Create a 2D array to store references to each cell
  27.     // (our coordinate system).
  28.     private Cell[][] cellCoordinates;
  29.    
  30.     // These are associated with methods of SelectedCell interface
  31.     int selectedRow;
  32.     int selectedColumn;
  33.    
  34.     // "Size" is how many Cells are in a Region
  35.     // and how many Regions on the board.
  36.     private int size;
  37.    
  38.     // Kept package private so can be accessed by both Region and SudokuBoard.
  39.     SudokuStub b;
  40.    
  41.     // The building blocks of our board.
  42.     Region region;
  43.        
  44.     /** Our class constructor.
  45.      * @param SudokuBase b.
  46.      *
  47.      */    
  48.     public SudokuBoard(SudokuBase b) {
  49.        
  50.         // Gets the number of rows and columns from the SudokuBase/Stub
  51.         // object b passed to our constructor.
  52.         rows = b.getRows();
  53.         columns = b.getColumns();
  54.         size = b.getSize();
  55.                
  56.         // Instantiate b, our SudokuStub/Base/Core object, SudokuBoard's sole
  57.         // parameter and underlying element.
  58.         this.b = new SudokuStub(rows, columns);
  59.        
  60.         // Determine the layout scheme of the overall board structure.
  61.         setLayout(new GridLayout(rows, columns, 2, 2));
  62.        
  63.        
  64.        
  65.         // Iterate thru loops and populate our SudokuBoard with Regions
  66.         // from the Region helper class.        
  67.         for (int row = 0; row < rows; row++) {
  68.        
  69.             for (int col = 0; col < columns; col++) {
  70.                                    
  71.                // Make new region with Region class and add to board.
  72.                region = new Region(this.b);
  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.                // Put the Region on the Board.                            
  86.                add(region);
  87.                              
  88.                // Instantiate our coordinate system.
  89.                cellCoordinates = new Cell[size][size];
  90.                // Temp variable for each cell.  Makes code neater.
  91.                Cell ourCell;
  92.                // "reg" is the region number on board of current region.              
  93.                // int ordinal = 0;
  94.                   // Add Cells to Region.
  95.                   for (int cellRow = 0; cellRow < rows; cellRow++) {
  96.            
  97.                       for (int cellCol = 0; cellCol < columns; cellCol++) {
  98.                          ourCell = createCell(cellRow, cellCol);
  99.                          ordinal++;
  100.                          region.add(ourCell, cellRow, cellCol);
  101.                          // Store a reference to each cell in our coordinates array.
  102.                          //cellCoordinates[reg][ordinal] = ourCell;
  103.                       }
  104.                   }
  105.                }                                                        
  106.             }                      
  107.         }
  108.     }
  109.        
  110.         /**
  111.          * A cell "factory" method.
  112.          * Instantiates a Cell and adds functionality to it.
  113.          * @param int row, the row it is associated with on the board.
  114.          * @param int col, the column it is associated with on the board.
  115.          */
  116.         private Cell createCell(int row, int col) {
  117.                                    
  118.             Cell cell = new Cell(row, col);
  119.             cell.addMouseListener(new MouseAdapter()
  120.             {
  121.                @Override
  122.                public void mouseClicked(MouseEvent click) {
  123.                   if(cell.isSelected()) {
  124.                      cell.deSelect();
  125.                      chosenOne = null;                                      
  126.                   } else {
  127.                        cell.select();
  128.                        if(chosenOne != null) {
  129.                           chosenOne.deSelect();
  130.                        }
  131.                        setSelected(row, col);
  132.                        chosenOne = cell;
  133.                        // 2 print methods to test these SelectedCell methods.
  134.                        System.out.println(getSelectedRow());
  135.                        System.out.println(getSelectedColumn());                    
  136.                   }                                  
  137.                   repaint();
  138.                }
  139.                @Override
  140.                public void mouseEntered(MouseEvent entered) {
  141.                   cell.setHoveredOver();                
  142.                   repaint();
  143.                }
  144.                @Override
  145.                public void mouseExited(MouseEvent exited) {
  146.                   cell.notHoveredOver();
  147.                   repaint();
  148.                }                            
  149.             });
  150.             return cell;                                                                          
  151.           }                      
  152.        
  153.     /*
  154.      *@param None
  155.      *@return b
  156.      */              
  157.     SudokuBase getBase() {
  158.         return b;
  159.     }
  160.    
  161.    /**
  162.     * Set the selected cell to the given row and column.
  163.     * @param row The indicated row
  164.     * @param col The indicated column
  165.     * @throws IllegalArgumentException
  166.     */
  167.    public void setSelected(int row, int col) {
  168.       if ((row < 0) || (col < 0)) {
  169.           throw new IllegalArgumentException("Values must be greater than "
  170.            + "or equal to 0.");
  171.       } else {
  172.           selectedRow = row;
  173.           selectedColumn = col;
  174.           chosenOne = this.cellCoordinates[row][col];
  175.       }
  176.    }
  177.    
  178.    /**
  179.     * Retrive the row of the currently selected cell.
  180.     * @return int selectedRow, the row in which the selected cell is located.
  181.     */
  182.    public int getSelectedRow() {
  183.        return this.selectedRow;
  184.    }  
  185.    
  186.    /**
  187.     * Retrive the column of the currently selected cell.
  188.     * @return int selectedColumn in which the selected cell is located.
  189.     */
  190.    public int getSelectedColumn(){
  191.        return this.selectedColumn;
  192.    }  
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement