Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package csc143.sudoku;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- /**
- * @author Vita Wiebe
- * @version PAx: Sudoku Serialization and Make-Up
- * A class to make create the SudokuBoard's visual functionality.
- */
- public class SudokuBoard extends JPanel
- implements SelectedCell {
- // Our class fields.
- private int rows;
- private int columns;
- // The building blocks of our Regions, Cells
- private Cell cell;
- // "chosenOne" is the currently selected cell.
- private Cell chosenOne;
- // Create a 2D array to store references to each cell
- // (our coordinate system).
- private Cell[][] cellCoordinates;
- // These are associated with methods of SelectedCell interface
- int selectedRow;
- int selectedColumn;
- // "Size" is how many Cells are in a Region
- // and how many Regions on the board.
- private int size;
- // Kept package private so can be accessed by both Region and SudokuBoard.
- SudokuStub b;
- // The building blocks of our board.
- Region region;
- /** Our class constructor.
- * @param SudokuBase b.
- *
- */
- public SudokuBoard(SudokuBase b) {
- // Gets the number of rows and columns from the SudokuBase/Stub
- // object b passed to our constructor.
- rows = b.getRows();
- columns = b.getColumns();
- size = b.getSize();
- // Instantiate b, our SudokuStub/Base/Core object, SudokuBoard's sole
- // parameter and underlying element.
- this.b = new SudokuStub(rows, columns);
- // Determine the layout scheme of the overall board structure.
- setLayout(new GridLayout(rows, columns, 2, 2));
- // Instantiate our coordinate system.
- cellCoordinates = new Cell[rows*rows][columns*columns];
- // Iterate thru loops and populate our SudokuBoard with Regions
- // from the Region helper class.
- for (int row = 0; row < rows; row++) {
- for (int col = 0; col < columns; col++) {
- // Make new region with Region class and add to board.
- region = new Region(this.b);
- // This determines whether region gets shading or not.
- // I wrote it in if/else if form rather than using || so that
- // my program could employ short-circuit evaluation and, in doing so,
- // conserve computer resources.
- if ((row % 2 == 0) && (col % 2 == 0)) {
- region.setBackground(Color.WHITE);
- } else if ((row % 2 == 1) && (col % 2 == 1)) {
- region.setBackground(Color.WHITE);
- } else {
- region.setBackground(new Color(220, 220, 220));
- }
- // Put the Region on the Board.
- add(region);
- // Temp variable for each cell. Makes code neater.
- Cell ourCell;
- // Add Cells to Region.
- for (int cellRow = 0; cellRow < (rows); cellRow++) {
- for (int cellCol = 0; cellCol < (columns); cellCol++) {
- // y is global row coordinate
- int y = row * rows + cellRow;
- // x is global column coordinate
- int x = col * columns + cellCol;
- ourCell = createCell(y, x);
- region.add(ourCell);
- // Store a reference to each cell in our coordinates array.
- cellCoordinates[y][x] = ourCell;
- }
- }
- }
- }
- }
- /**
- * A cell "factory" method.
- * Instantiates a Cell and adds functionality to it.
- * @param int row, the row it is associated with on the board.
- * @param int col, the column it is associated with on the board.
- */
- private Cell createCell(int row, int col) {
- Cell cell = new Cell(row, col);
- cell.addMouseListener(new MouseAdapter()
- {
- @Override
- public void mouseClicked(MouseEvent click) {
- if(cell.isSelected()) {
- cell.deSelect();
- chosenOne = null;
- } else {
- cell.select();
- if(chosenOne != null) {
- chosenOne.deSelect();
- }
- setSelected(row, col);
- chosenOne = cell;
- //print method to test these SelectedCell methods.
- System.out.println(getSelectedRow()+ ", " + getSelectedColumn());
- }
- repaint();
- }
- @Override
- public void mouseEntered(MouseEvent entered) {
- cell.setHoveredOver();
- repaint();
- }
- @Override
- public void mouseExited(MouseEvent exited) {
- cell.notHoveredOver();
- repaint();
- }
- });
- return cell;
- }
- /*
- *@param None
- *@return b
- */
- SudokuBase getBase() {
- return b;
- }
- /**
- * Set the selected cell to the given row and column.
- * @param row The indicated row
- * @param col The indicated column
- * @throws IllegalArgumentException
- */
- public void setSelected(int row, int col) {
- if ((row < 0) || (col < 0)) {
- throw new IllegalArgumentException("Values must be greater than "
- + "or equal to 0.");
- } else {
- selectedRow = row;
- selectedColumn = col;
- chosenOne = this.cellCoordinates[row][col];
- }
- }
- /**
- * Retrive the row of the currently selected cell.
- * @return int selectedRow, the row in which the selected cell is located.
- */
- public int getSelectedRow() {
- return this.selectedRow;
- }
- /**
- * Retrive the column of the currently selected cell.
- * @return int selectedColumn in which the selected cell is located.
- */
- public int getSelectedColumn(){
- return this.selectedColumn;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement