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.*;
- /**
- * @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;
- // X and Y track the pixel position in the cell matrix.
- // This can be used to calculate the area of each region in the current matrix.
- private int x;
- private int y;
- GridLayout grid;
- // 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.
- grid = new GridLayout(this.rows, this.columns, 2, 2);
- // Creates our overall "board" element, one big JPanel, to set into container (JFrame, frame).
- JPanel board = new JPanel(grid);
- // An index tracking position of the current region on the overall board.
- // I thought this might come in useful in later stages of this project.
- int regionIndex = 0;
- // Iterate thru loops and populate 2D array, "cells", with cells made using our Cell 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);
- board.add(region);
- // Update x to reflect how many pixels traversed from left to right.
- x = (50 * col);
- // Update the region index to reflect current total number of regions.
- regionIndex++;
- // 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));
- }
- }
- // Reset x once a row of columns has been iterated through.
- x = 0;
- // Update y to reflect how many pixels traversed from top to bottom.
- y = (50 * row);
- }
- // Creates the container window onto which we place our board.
- JFrame frame = new JFrame(rows + "x" + columns);
- frame.setPreferredSize(new Dimension(rows*70, columns*70));
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.add(board);
- frame.getContentPane().setLayout(grid);
- frame.pack();
- frame.setVisible(true);
- board.setPreferredSize(new Dimension(x, y));
- board.setBorder(BorderFactory.createLineBorder(Color.BLACK));
- board.setLayout(grid);
- board.setVisible(true);
- this.repaint();
- }
- /**
- * 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 board matrix.
- // 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;
- }
- public static void main(String[] args) {
- // Region stub1 = new Region(new SudokuStub(3, 3));
- // Region stub2 = new Region(new SudokuStub(2, 3));
- // Region stub3 = new Region(new SudokuStub(4, 4));
- // Instantiate SudokuStub to pass to our constructor.
- SudokuStub stub1 = new SudokuStub(3, 3);
- SudokuStub stub2 = new SudokuStub(2, 3);
- SudokuStub stub3 = new SudokuStub(4, 4);
- SudokuBoard game1 = new SudokuBoard(stub1);
- SudokuBoard game2 = new SudokuBoard(stub2);
- SudokuBoard game3 = new SudokuBoard(stub3);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement