title: Drawing a grid using squares in JFrame
body: <p>I'm pretty new to programming and I've just started studying algorithms, I wanted to create an algorithm that draws a grid as a challenge instead of using the GridLayout() method. For some reason my squares only start drawing a little below the beginning of the top of the window and the side of the window, and I did not find the reason why this happens.</p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  
  public class JFrameSquares extends JPanel {
  
      public void paintComponent(Graphics g)
      {
          super.paintComponent(g);
  
          int width = getWidth();
          int height = getHeight();
          int rows = width/30;
          int cols = height/30;
  
          for (int j = 0; j &lt; 30; j++)
          {
              for(int i = 0; i &lt; 30; i++)
              {
                  g.setColor(Color.red);
                  g.drawRect(i* rows,j* cols,i* rows,j* cols);
              }
          }
  
      }
  
      public static void main(String[] args) {
          JFrame window = new JFrame();
          window.setSize(380,380);
  
          JFrameSquares panel = new JFrameSquares();
          window.add(panel);
  
          window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          window.setVisible(true);
      }
  }
  </code></pre>