title: Java: draw a rectangular spiral with drawLine
body: <p>I'm working on a Java programming exercise where I'm supposed to draw a rectangular spiral with the drawLine method. Here's what I have so far, but the spiral comes up looking incomplete. </p>
  
  <pre><code>import java.awt.Graphics; 
  import javax.swing.JPanel;
  import javax.swing.JFrame;
  
  public class RectSpiral extends JPanel
  {
      public void paintComponent(Graphics g)
      {
          int width = getSize().width;
          int height = getSize().height;
  
          int widthCenter = width / 2;
          int heightCenter = height / 2;
  
          for (int i = 0; i &lt; 4 ; i++)
          {
              g.drawLine(widthCenter + (20 * i), heightCenter + (20 * i), widthCenter + (20 * i), heightCenter + 20 + (20 * i));
              g.drawLine(widthCenter + (20 * i), heightCenter + 20 + (20 * i), widthCenter - 20 - (20 * i), heightCenter + 20 + (20 * i));
              g.drawLine(widthCenter - 20 - (20 * i), heightCenter + 20 + (20 * i), widthCenter - 20 - (20 * i), heightCenter - 20 - (20 * i));
              g.drawLine(widthCenter - 20 - (20 * i), heightCenter - 20 - (20 * i), widthCenter + 20 + (20 * i), heightCenter - 20 - (20 * i));
          }
      }
  
      public static void main(String[] args)
      {
          RectSpiral panel = new RectSpiral();
  
          JFrame application = new JFrame();
  
          application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          application.add(panel);      
          application.setSize(300, 300); 
          application.setVisible(true);     
      }
  }
  </code></pre>
  
  <p>This is the output:</p>
  
  <p><img src="https://i.stack.imgur.com/k8qot.jpg" alt="enter image description here"></p>
  
  <p>I've tried adding a fifth line withing the for loop, but the result still comes up incomplete. 
  Any help is appreciated, thank you!</p>