Advertisement
Vita_Harvey

SudokuBoard_I

Mar 17th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.06 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.         // Instantiate our coordinate system.
  64.         cellCoordinates = new Cell[rows*rows][columns*columns];
  65.        
  66.         // Iterate thru loops and populate our SudokuBoard with Regions
  67.         // from the Region helper class.        
  68.         for (int row = 0; row < rows; row++) {
  69.        
  70.             for (int col = 0; col < columns; col++) {
  71.                                    
  72.                // Make new region with Region class and add to board.
  73.                region = new Region(this.b);
  74.                                                                        
  75.                // This determines whether region gets shading or not.
  76.                // I wrote it in if/else if form rather than using || so that
  77.                // my program could employ short-circuit evaluation and, in doing so,
  78.                // conserve computer resources.
  79.                if ((row % 2 == 0) && (col % 2 == 0)) {
  80.                   region.setBackground(Color.WHITE);                                        
  81.                } else if ((row % 2 == 1) && (col % 2 == 1)) {
  82.                   region.setBackground(Color.WHITE);                  
  83.                } else {
  84.                   region.setBackground(new Color(220, 220, 220));
  85.                }
  86.                // Put the Region on the Board.                            
  87.                add(region);
  88.                                          
  89.                // Temp variable for each cell.  Makes code neater.
  90.                Cell ourCell;
  91.                // Add Cells to Region.
  92.                for (int cellRow = 0; cellRow < (rows); cellRow++) {
  93.            
  94.                   for (int cellCol = 0; cellCol < (columns); cellCol++) {
  95.                       // y is global row coordinate
  96.                       int y = row * rows + cellRow;
  97.                       // x is global column coordinate
  98.                       int x = col * columns + cellCol;
  99.                      
  100.                       ourCell = createCell(y, x);
  101.                       region.add(ourCell);
  102.                       // Store a reference to each cell in our coordinates array.
  103.                       cellCoordinates[y][x] = ourCell;
  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.                        //print method to test these SelectedCell methods.
  134.                        System.out.println(getSelectedRow()+ ", " + getSelectedColumn());                                        
  135.                   }                                  
  136.                   repaint();
  137.                }
  138.                @Override
  139.                public void mouseEntered(MouseEvent entered) {
  140.                   cell.setHoveredOver();                
  141.                   repaint();
  142.                }
  143.                @Override
  144.                public void mouseExited(MouseEvent exited) {
  145.                   cell.notHoveredOver();
  146.                   repaint();
  147.                }                            
  148.             });
  149.             return cell;                                                                          
  150.           }                      
  151.        
  152.     /*
  153.      *@param None
  154.      *@return b
  155.      */              
  156.     SudokuBase getBase() {
  157.         return b;
  158.     }
  159.    
  160.    /**
  161.     * Set the selected cell to the given row and column.
  162.     * @param row The indicated row
  163.     * @param col The indicated column
  164.     * @throws IllegalArgumentException
  165.     */
  166.    public void setSelected(int row, int col) {
  167.       if ((row < 0) || (col < 0)) {
  168.           throw new IllegalArgumentException("Values must be greater than "
  169.            + "or equal to 0.");
  170.       } else {
  171.           selectedRow = row;
  172.           selectedColumn = col;
  173.           chosenOne = this.cellCoordinates[row][col];
  174.       }
  175.    }
  176.    
  177.    /**
  178.     * Retrive the row of the currently selected cell.
  179.     * @return int selectedRow, the row in which the selected cell is located.
  180.     */
  181.    public int getSelectedRow() {
  182.        return this.selectedRow;
  183.    }  
  184.    
  185.    /**
  186.     * Retrive the column of the currently selected cell.
  187.     * @return int selectedColumn in which the selected cell is located.
  188.     */
  189.    public int getSelectedColumn(){
  190.        return this.selectedColumn;
  191.    }  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement