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 PA2
- * A class to make a SudokuBoard.
- */
- public class SudokuBoard extends JPanel implements SelectedCell {
- // Our class fields.
- private int rows;
- private int columns;
- // "Size" is how many cells are in the Sudoku board, total.
- 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 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));
- // 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));
- }
- add(region);
- }
- }
- // Iterate thru loops and populate "region" with cells made using
- // our Cell helper class.
- for (int row = 0; row < rows; row++) {
- for (int col = 0; col < columns; 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);
- addMouseListener(new MouseAdapter()
- {
- @Override
- public void mouseClicked(MouseEvent click) {
- setSelected(row, col);
- if (getBackground().equals(Color.YELLOW)) {
- setOpaque(false);
- setBackground(null);
- } else {
- setOpaque(true);
- setBackground(Color.YELLOW);
- repaint();
- }
- }
- });
- add(cell);
- }
- }
- }
- /**
- * This method renders one large rectangle border.
- * @param g The Graphics object use to render
- */
- @Override
- public void paintComponent(java.awt.Graphics g) {
- // paint the underlying component
- super.paintComponent(g);
- // set the color of the outline
- g.setColor(Color.BLACK);
- // Set the size of outside black rectangle.
- // width is the number of columns times 50 pixels wide, plus the number of
- // columns plus 1 to account for borders.
- int width = (columns*50) + (columns + 1);
- // height is the number of rows in the board times 50 pixels, plus the
- // number of columns plus 1 to account for borders.
- int height = (rows*50) + (rows + 1);
- g.drawRect(0, 0, width, height);
- }
- /*
- *@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
- */
- @Override
- public void setSelected(int row, int col) {
- this.selectedRow = row;
- this.selectedColumn = col;
- }
- /**
- * Retrive the row of the currently selected cell.
- * @return The row in which the selected cell is located.
- */
- public int getSelectedRow() {
- return this.selectedRow;
- }
- /**
- * Retrive the column of the currently selected cell.
- * @return The column in which the selected cell is located.
- // */
- // public int getSelectedColumn(){
- // //return this.selectedColumn;
- // }
- public static void main(String[] args) {
- javax.swing.JFrame win = new javax.swing.JFrame("Test 2x3");
- win.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
- win.add(new SudokuBoard(new SudokuStub(2, 3)));
- win.pack();
- win.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement