Advertisement
Vita_Harvey

Region_A

Jan 27th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.34 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 Jan. 26, 2019
  10.  */
  11.  
  12. /**
  13.  * Designs a JPanel-type object representing a "region" of the Sudoku board.
  14.  * Is used to regulate the shading schemata of the final SudokuBoard object.
  15.  */
  16. public class Region extends JPanel {
  17.    
  18.     // A field to hold a Cell instance while populating Region.
  19.     private Cell cell;
  20.    
  21.    // Our class fields.
  22.     private int rows;
  23.     private int columns;
  24.     private int size;
  25.    
  26.     private int x;
  27.     private int y;
  28.    
  29.     private SudokuBase b;
  30.    
  31.     private JPanel region;
  32.    
  33.     // Determine the layout scheme of the region.
  34.     // Left package private so can be accessed by others in package,
  35.     // since each component ("region") of the "board" object will have same
  36.     // layout.
  37.     GridLayout grid;
  38.    
  39.    /** Our class constructor.  Designs a Region object of Cells, which will in turn be
  40.     * added to the board to make the overarching schemata of our SudokuBoard.    
  41.     * @param Cell cell.
  42.     * @return None.
  43.     */
  44.    public Region(SudokuBase b) {
  45.    
  46.       // Gets the number of rows and columns from the SudokuBase/Stub object.
  47.       rows = b.getRows();
  48.       columns = b.getColumns();
  49.       size = b.getSize();
  50.      
  51.       grid = new GridLayout(this.rows, this.columns, 0, 0);
  52.          
  53.       // Instantiate b, our SudokuStub/Base/Core object.
  54.       this.b = new SudokuStub(rows, columns);
  55.        
  56.       // Instantiate our JPanel, "region".  This is the fundamental
  57.       // object defining the Board's "regions".
  58.       region = new JPanel(grid);
  59.        
  60.       // Creates our overall element, one big JPanel, onto which we place our 2D array of cells,
  61.       // "region".  (Do I even need this?)  
  62.       JPanel regionBack = new JPanel(grid);
  63.        
  64.       // Iterate thru loops and populate "region" with cells made using
  65.       // our Cell helper class.        
  66.       for (int row = 0; row < rows; row++) {
  67.          for (int col = 0; col < columns; col++) {
  68.             // make new cell, add to "region" JPanel
  69.             Cell cell = new Cell();
  70.             //region[row][col] = cell;
  71.             region.add(cell);              
  72.             // Update x to reflect how many pixels traversed from left to right.
  73.             x += 50;
  74.          }
  75.       }            
  76.       // Reset x once a row of columns has been iterated through.
  77.       x = 0;
  78.       // Update y to reflect how many pixels traversed from top to bottom.
  79.       y += 50;            
  80.    }
  81.    
  82.    /**
  83.      * This method renders one "region" of size == (x, y)
  84.      * @param g The Graphics object use to render
  85.      */
  86.     @Override
  87.     public void paintComponent(java.awt.Graphics g) {
  88.        
  89.         // paint the underlying component
  90.         super.paintComponent(g);
  91.                              
  92.         // set the color of the outline        
  93.         g.setColor(Color.BLACK);
  94.        
  95.         g.drawRect(0, 0, 50 * rows, 50 * columns);
  96.         region.setPreferredSize(new Dimension(x, y));
  97.         region.setBorder(BorderFactory.createLineBorder(Color.BLACK));
  98.         region.setLayout(grid);
  99.         region.setVisible(true);        
  100.     }
  101.    
  102.     public static void main(String[] args) {
  103.    
  104.         Region r = new Region(new SudokuBase b);
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement