Advertisement
Vita_Harvey

Region_B

Mar 2nd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 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. /**
  9.  * @author Vita Wiebe
  10.  * @version Feb. 24, 2019
  11.  */
  12.  
  13. /**
  14.  * Designs a JPanel-type object representing a "region" of the Sudoku board.
  15.  * Is used to regulate the shading schemata of the final SudokuBoard object.
  16.  */
  17. public class Region extends JPanel {
  18.    
  19.    // A field to hold a Cell instance while populating Region.
  20.    private Cell cell;
  21.  
  22.    // Our class fields.
  23.    private int rows;
  24.    private int columns;
  25.    private int size;
  26.  
  27.    private SudokuBase b;
  28.        
  29.    /** Our class constructor.  Designs a Region object of Cells, which will in turn be
  30.     * added to the board to make the overarching schemata of our SudokuBoard.    
  31.     * @param Cell cell.
  32.     */
  33.    public Region(SudokuStub b) {
  34.    
  35.       // Gets the number of rows and columns from the SudokuBase/Stub object.
  36.       rows = b.getRows();
  37.       columns = b.getColumns();
  38.       size = b.getSize();
  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), row, col);              
  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 selected = cell.setSelected(row, col);
  66.             repaint();
  67.          }
  68.          @Override
  69.          public void mouseEntered(MouseEvent entered) {
  70.             setBackground(Color.CYAN);
  71.             repaint();
  72.          }
  73.          @Override
  74.          public void mouseExited(MouseEvent exited) {
  75.             setBackground(null);
  76.             //repaint();
  77.          }                            
  78.       });
  79.       return cell;                                                                          
  80.     }  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement