Advertisement
Vita_Harvey

Region_A

Feb 24th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. package csc143.sudoku;
  2.  
  3. import javax.swing.*;
  4. import csc143.sudoku.*;
  5. import java.awt.*;
  6. import java.awt.event.*;
  7. /**
  8.  * @author Vita Wiebe
  9.  * @version Feb. 4, 2019
  10.  */
  11.  
  12. /**
  13.  * Designs a JPanel-type object representing a "region" of the Sudoku board.
  14.  * Is used to regulate the shading schemata of the final SudokuBoard object.
  15.  */
  16. public class Region extends JPanel {
  17.    
  18.    // A field to hold a Cell instance while populating Region.
  19.    private Cell cell;
  20.  
  21.    // Our class fields.
  22.    private int rows;
  23.    private int columns;
  24.    private int size;
  25.  
  26.    private SudokuBase b;
  27.        
  28.    /** Our class constructor.  Designs a Region object of Cells, which will in turn be
  29.     * added to the board to make the overarching schemata of our SudokuBoard.    
  30.     * @param Cell cell.
  31.     */
  32.    public Region(SudokuStub b) {
  33.    
  34.       // Gets the number of rows and columns from the SudokuBase/Stub object.
  35.       rows = b.getRows();
  36.       columns = b.getColumns();
  37.       size = b.getSize();
  38.      
  39.                    
  40.       // Instantiate b, our SudokuStub/Base/Core object.
  41.       this.b = new SudokuStub(rows, columns);
  42.       GridLayout myLayout = new GridLayout(this.rows, this.columns, 0, 0);
  43.       setLayout(myLayout);    
  44.      
  45.       for (int row = 0; row < rows; row++) {
  46.        
  47.             for (int col = 0; col < columns; col++) {
  48.            
  49.                this.add(createCell(row, col), col, row);              
  50.             }
  51.       }                      
  52.    }
  53.    
  54.    private Cell createCell(int row, int col) {
  55.    
  56.       // make new cell, add to region/board.
  57.       // (row, col) passed to (row, column) give each Cell instance its own "address" on the board.
  58.       Cell cell = new Cell(row, col);              
  59.       cell.addMouseListener(new MouseAdapter()
  60.       {
  61.          @Override
  62.          public void mouseClicked(MouseEvent click) {                    
  63.             cell.setOpaque(!cell.isOpaque());
  64.             cell.setBackground(Color.YELLOW);            
  65.             //cell.setSelected(row, col);
  66.             repaint();
  67.          }                            
  68.       });
  69.       return cell;                                                                          
  70.     }  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement