title: Creating a draw rectangle (filled with black color) function in Java for a grid
body: <p>I have created a grid in my program. Below is the code used to create the grid.</p>
  
  <pre><code>import java.awt.Graphics;
  
  import javax.swing.JComponent;
  import javax.swing.JFrame;
  
  class Grid extends JComponent {
      public void paint(Graphics g) {
          g.drawRect (10, 10, 800, 500);    
  
          for (int i = 10; i &lt;= 800; i+= 10)
              g.drawLine (i, 10, i, 510);
  
          for (int i = 10; i &lt;= 500; i+= 10)
              g.drawLine (10, i, 810, i);
      }
  }
  
  public class CoreControl {
  
      public static void main(String[] a) {
          JFrame window = new JFrame();
          window.setSize(840,560);
          window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          window.getContentPane().add(new Grid());
          window.setVisible(true);
      }
  
  }
  </code></pre>
  
  <p>What I want to do is to create a function which will draw a rectangle (filled with black color) based on the coordinates that I give it. Basically I want to populate certain cells of the grid with black color and my idea is to draw black filled rectangles on the cell coordinates. How do I make this function?</p>
  
  <p>I tried making another class called drawRectangle and called it in the main function like so window.getContentPane().add(new drawRectangle()); however that did not work (only drawRectangle shows up and not the grid).</p>
  
  <p>I also want to be able to use this function repeatedly to keep creating rectangles. </p>
  
  <p>How do I do create this function?</p>
  
  <p>Also if you know a better way that I should structure this program please let me know (I am new to Java so I am open to any suggestions).</p>