Advertisement
Vita_Harvey

SudokuBoard_D

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