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 Jan. 26, 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 int x;
- private int y;
- private SudokuBase b;
- private JPanel region;
- // Determine the layout scheme of the region.
- // Left package private so can be accessed by others in package,
- // since each component ("region") of the "board" object will have same
- // layout.
- GridLayout grid;
- /** 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.
- * @return None.
- */
- public Region(SudokuBase b) {
- // Gets the number of rows and columns from the SudokuBase/Stub object.
- rows = b.getRows();
- columns = b.getColumns();
- size = b.getSize();
- grid = new GridLayout(this.rows, this.columns, 0, 0);
- // Instantiate b, our SudokuStub/Base/Core object.
- this.b = new SudokuStub(rows, columns);
- // Instantiate our JPanel, "region". This is the fundamental
- // object defining the Board's "regions".
- region = new JPanel(grid);
- // Creates our overall element, one big JPanel, onto which we place our 2D array of cells,
- // "region". (Do I even need this?)
- JPanel regionBack = new JPanel(grid);
- // 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" JPanel
- Cell cell = new Cell();
- //region[row][col] = cell;
- region.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;
- }
- /**
- * This method renders one "region" of size == (x, y)
- * @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);
- g.drawRect(0, 0, 50 * rows, 50 * columns);
- region.setPreferredSize(new Dimension(x, y));
- region.setBorder(BorderFactory.createLineBorder(Color.BLACK));
- region.setLayout(grid);
- region.setVisible(true);
- }
- public static void main(String[] args) {
- Region r = new Region(new SudokuBase b);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement