Vita_Harvey

Region_B

Jan 27th, 2019
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.36 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.      
  68.          for (int col = 0; col < columns; col++) {
  69.          
  70.             // make new cell, add to "region" JPanel
  71.             Cell cell = new Cell();
  72.            
  73.             region.add(cell);              
  74.             // Update x to reflect how many pixels traversed from left to right.
  75.             //x += 50;
  76.          }
  77.       }
  78.                  
  79.       // Reset x once a row of columns has been iterated through.
  80.       //x = 0;
  81.       // Update y to reflect how many pixels traversed from top to bottom.
  82.       //y += 50;            
  83.    }
  84.    
  85.    /**
  86.      * This method renders one "region" of size == (x, y)
  87.      * @param g The Graphics object use to render
  88.      */
  89.     @Override
  90.     public void paintComponent(java.awt.Graphics g) {
  91.        
  92.         // paint the underlying component
  93.         super.paintComponent(g);
  94.                              
  95.         // set the color of the outline        
  96.         g.setColor(Color.BLACK);
  97.        
  98.         g.drawRect(0, 0, 50 * rows, 50 * columns);
  99.         region.setPreferredSize(new Dimension(x, y));
  100.         region.setBorder(BorderFactory.createLineBorder(Color.BLACK));
  101.         region.setLayout(grid);
  102.         region.setVisible(true);        
  103.     }
  104.    
  105.    //  public static void main(String[] args) {
  106. //    
  107. //         Region r = new Region(new SudokuBase b);
  108. //     }
  109. }
Add Comment
Please, Sign In to add comment