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;
- SudokuStub b;
- // Create an empty 2D array of JPanels the size
- // of the underlying board element (SudokuBase).
- // These shall serve as our cells.
- JPanel[][] cells;
- /** 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.
- GridLayout grid = new GridLayout(this.rows, this.columns, 2, 2);
- // Instantiate our 2D JPanel array, "cells".
- cells = new JPanel[rows][columns];
- // Creates our overall "board" element, one big JPanel, to set into container (JFrame, frame).
- JPanel board = new JPanel(grid);
- // Initialize a counter of the number of regions in the board.
- // This will be used to determine which parts of the board constitute a "region"
- // by determining when a cell is the first in a new region, and
- // thus, how they should be shaded.
- int region = 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 cell with CreateCell class object.
- Cell cell = new Cell();
- cells[row][col] = cell;
- board.add(cell);
- // Update x to reflect how many pixels traversed from left to right.
- x += 50;
- }
- }
- // 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;
- JFrame frame = new JFrame (rows + "x" + columns);
- frame.setSize(rows*60, columns*60);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.add(board);
- frame.getContentPane().setLayout(grid);
- frame.pack();
- frame.setVisible(true);
- board.setPreferredSize(new java.awt.Dimension(rows*54, columns*54));
- 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.
- int width = columns*54;
- //height is the number of rows in the board, times 50 pixels as well.
- int height = rows*54;
- g.drawRect(0, 0, width, height);
- }
- /*
- *@param None
- *@return b
- */
- SudokuBase getBase() {
- return b;
- }
- public static void main(String[] args) {
- // Instantiate SudokuStub to pass to our constructor.
- SudokuStub stub = new SudokuStub(3, 3);
- SudokuBoard game = new SudokuBoard(stub);
- // 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