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?

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).

I also want to be able to use this function repeatedly to keep creating rectangles.

How do I do create this function?

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).

I'd advice against trying to have a second component to performing the filling. Simply provide a method in you grid class that supplies the cell's x/y position (in grid terms) and fill the cell within the paintComponent method

This might sound stupid but w/e i'm new to Java. How do I use Graphics.fillRect(int,int.int,int)? Right now this is how to would like to use it: function drawRectangle(int x1, int y1, int x2, int y2) { Graphics.fillRect(int,int.int,int) } is this possible? Does the program automatically add the rectangle to the frame?
–
Veda SadhakApr 8 '13 at 3:06

No. fillRect is a method of Graphics, you need a reference to the Graphics context. All painting should be done within the context of a paint method (preferably paintComponent). In your Grid class I would provide method called something like fillGrid(int x, int y) which tells you what cell to fill. In your paintComponent method, I would then determine the rectangle bounds of this cell and fill using fillRect (ps I updated the answer ;))
–
MadProgrammerApr 8 '13 at 3:10

Ok so just to test out the fillRect I have added public void paint(Graphics g, int x, int y) { g.fillRect(x, y, 10, 10); } in the Grid Class. How do I use this function?
–
Veda SadhakApr 8 '13 at 3:16