Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package csc143.sudoku;
- import javax.swing.*;
- import csc143.sudoku.*;
- import java.awt.*;
- import java.awt.event.*;
- /**
- * @author Vita Wiebe
- * @version Feb. 24, 2019
- */
- /**
- * Designs a JPanel-type object representing a "region" of the Sudoku board.
- * Is used to regulate the shading schemata of the final SudokuBoard object.
- */
- public class Region extends JPanel {
- // A field to hold a Cell instance while populating Region.
- private Cell cell;
- // Our class fields.
- private int rows;
- private int columns;
- private int size;
- private SudokuBase b;
- /** Our class constructor. Designs a Region object of Cells, which will in turn be
- * added to the board to make the overarching schemata of our SudokuBoard.
- * @param Cell cell.
- */
- public Region(SudokuStub b) {
- // Gets the number of rows and columns from the SudokuBase/Stub object.
- rows = b.getRows();
- columns = b.getColumns();
- size = b.getSize();
- // Instantiate b, our SudokuStub/Base/Core object.
- this.b = new SudokuStub(rows, columns);
- GridLayout myLayout = new GridLayout(this.rows, this.columns, 0, 0);
- setLayout(myLayout);
- for (int row = 0; row < rows; row++) {
- for (int col = 0; col < columns; col++) {
- this.add(createCell(row, col), row, col);
- }
- }
- }
- private Cell createCell(int row, int col) {
- // make new cell, add to region/board.
- // (row, col) passed to (row, column) give each Cell instance its own "address" on the board.
- Cell cell = new Cell(row, col);
- cell.addMouseListener(new MouseAdapter()
- {
- @Override
- public void mouseClicked(MouseEvent click) {
- cell.setOpaque(!cell.isOpaque());
- cell.setBackground(Color.YELLOW);
- //Cell selected = cell.setSelected(row, col);
- repaint();
- }
- @Override
- public void mouseEntered(MouseEvent entered) {
- setBackground(Color.CYAN);
- repaint();
- }
- @Override
- public void mouseExited(MouseEvent exited) {
- setBackground(null);
- //repaint();
- }
- });
- return cell;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement