The problem I'm working on is to draw a brick wall where each row of bricks is offset from the row above and below. I'm trying to follow examples in the text which use loops and conditions but I've really gotten confused. I'm not quite sure I know how to set this up and how to set the frame size to display everything that has been drawn.

When I run this I just get a small window showing a partial brick and if I expand the window manually the column of bricks extends below but it looks like they go on infinitely. If anyone can help this is what I've stumbled through so far.

Code:
public class BrickWall 
{ 
        //--------------------------------------- 
        // Creates the main frame of the program 
        //--------------------------------------- 
        public static void main(String[] args) 
          { 
             JFrame frame = new JFrame ("Brick Wall"); 
             frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
					 
	     BrickWallPanel panel = new BrickWallPanel();
                 
             frame.getContentPane().add(panel); 
             frame.pack(); 
             frame.setVisible(true); 
          } 
}
Code:
import javax.swing.JPanel;
import java.awt.*;

public class BrickWallPanel extends JPanel
{
   private final int NUM_BRICKS = 25;
   private final int LENGTH_BRICK = 50; // max length of a brick
   private final int WIDTH_BRICK = 20; // max width of a brick
   private int length, width; // length and width of the frame

   //--------------------------- 
   //Sets up the drawing panel
   //---------------------------
	public BrickWallPanel()
	{ 
	   length = LENGTH_BRICK; // assign length of brick
	   width = WIDTH_BRICK; // assign width of brick
		
	   setPreferredSize (new Dimension (length, width)); 
           setBackground (Color.darkGray);
	}
	
	//------------------------------------------------------
	// draws the bricks across the frame and down the frame 
	//------------------------------------------------------
	
	public void paintComponent (Graphics page) 
	{ 
	   super.paintComponent(page);
		int x, y; // x and y of the brick
		
		x = 0; // initial x of the first brick
		y = 0; // initial y of the first brick 
		
		for (int countY = 0; countY <= width; countY++) // draws the columns
		{ 
		   for (int countX = 0; countX <= length; countX++) // draws the rows
			{
                                if (x % 2 == 0)
				{
				page.setColor (Color.red);
				page.fillRect (x/2, y, LENGTH_BRICK, WIDTH_BRICK);
				
				x = x + 52;
					
			
			        x = 0;
			        y = y + 22;
				}
				
			   else
				 
			   page.setColor (Color.red); 
			   page.fillRect (x, y, LENGTH_BRICK, WIDTH_BRICK);
				
			       x = x + 52;
			}	
			
			       x = 0;
			       y = y + 22;
	      }
	}
}