Advertisement
Vita_Harvey

PA3_A

Jan 26th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.70 KB | None | 0 0
  1. package csc143.sudoku;
  2.  
  3. import javax.swing.*;
  4. import csc143.sudoku.*;
  5. import java.awt.*;
  6.  
  7. /**
  8.  * @author Vita Wiebe
  9.  * @version PA2
  10.  * A class to make a SudokuBoard.
  11.  */
  12. public class SudokuBoard extends JPanel
  13.    // implements SelectedCell
  14. {
  15.     // Our class fields.
  16.     private int rows;
  17.     private int columns;
  18.    
  19.     // "Size"  is how many cells are in the Sudoku board, total.
  20.     private int size;
  21.    
  22.     // X and Y track the pixel position in the cell matrix.
  23.     // This can be used to calculate the area of each region in the current matrix.
  24.     private int x;
  25.     private int y;
  26.    
  27.     SudokuStub b;
  28.      
  29.     // Create an empty 2D array of JPanels the size
  30.     // of the underlying board element (SudokuBase).
  31.     // These shall serve as our cells.
  32.     JPanel[][] cells;
  33.        
  34.     /** Our class constructor.
  35.      * @param SudokuBase b.
  36.      *
  37.     */    
  38.     public SudokuBoard(SudokuBase b) {
  39.        
  40.         // Gets the number of rows and columns from the SudokuBase/Stub object b passed to our constructor.
  41.         rows = b.getRows();
  42.         columns = b.getColumns();
  43.         size = b.getSize();
  44.         // Instantiate b, our SudokuStub/Base object, SudokuBoard's sole parameter and underlying element.
  45.         this.b = new SudokuStub(rows, columns);
  46.        
  47.         // Determine the layout scheme of the overall board structure.
  48.         GridLayout grid = new GridLayout(this.rows, this.columns, 2, 2);
  49.        
  50.         // Instantiate our 2D JPanel array, "cells".
  51.         cells = new JPanel[rows][columns];
  52.        
  53.         // Creates our overall "board" element, one big JPanel, to set into container (JFrame, frame).    
  54.         JPanel board = new JPanel(grid);
  55.            
  56.         // Initialize a counter of the number of regions in the board.
  57.         // This will be used to determine which parts of the board constitute a "region"
  58.         // by determining when a cell is the first in a new region, and
  59.         // thus, how they should be shaded.
  60.         int region = 0;
  61.          
  62.         // Iterate thru loops and populate 2D array, "cells", with cells made using our Cell helper class.        
  63.         for (int row = 0; row < rows; row++) {
  64.             for (int col = 0; col < columns; col++) {
  65.                // make new cell with CreateCell class object.
  66.                Cell cell = new Cell();
  67.                cells[row][col] = cell;
  68.                board.add(cell);              
  69.                // Update x to reflect how many pixels traversed from left to right.
  70.                x += 50;
  71.             }
  72.          }            
  73.          // Reset x once a row of columns has been iterated through.
  74.          x = 0;
  75.          // Update y to reflect how many pixels traversed from top to bottom.
  76.          y += 50;
  77.          
  78.         JFrame frame = new JFrame (rows + "x" + columns);
  79.         frame.setSize(rows*60, columns*60);
  80.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  81.         frame.add(board);
  82.         frame.getContentPane().setLayout(grid);
  83.         frame.pack();
  84.         frame.setVisible(true);
  85.        
  86.         board.setPreferredSize(new java.awt.Dimension(rows*54, columns*54));
  87.         board.setBorder(BorderFactory.createLineBorder(Color.BLACK));
  88.         board.setLayout(grid);
  89.         board.setVisible(true);
  90.                                              
  91.         this.repaint();              
  92.     }
  93.        
  94.     /**
  95.     * This method renders one large rectangle border.
  96.     * @param g The Graphics object use to render
  97.     */
  98.     @Override
  99.     public void paintComponent(java.awt.Graphics g) {
  100.        
  101.         // paint the underlying component
  102.         super.paintComponent(g);
  103.                              
  104.         // set the color of the outline        
  105.         g.setColor(Color.BLACK);
  106.        
  107.         // Set the size of board matrix.
  108.         // width is the number of columns times 50 pixels wide.
  109.         int width = columns*54;
  110.         //height is the number of rows in the board, times 50 pixels as well.
  111.         int height = rows*54;
  112.         g.drawRect(0, 0, width, height);              
  113.     }
  114.      
  115.      /*
  116.       *@param None
  117.       *@return b
  118.       */              
  119.      SudokuBase getBase() {
  120.         return b;
  121.     }
  122.    
  123.     public static void main(String[] args) {
  124.         // Instantiate SudokuStub to pass to our constructor.
  125.         SudokuStub stub = new SudokuStub(3, 3);
  126.  
  127.         SudokuBoard game = new SudokuBoard(stub);
  128.        
  129. //         javax.swing.JFrame win = new javax.swing.JFrame("Test 2x3");
  130. //         win.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
  131. //         win.add(new SudokuBoard(new SudokuStub(2, 3)));
  132. //         win.pack();
  133. //         win.setVisible(true);
  134.        
  135.     }
  136.    
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement