title: Is there a way to determine the exact amount of lines you want to draw on a page using for loops?
body: <pre><code>//**************************************************************************
  // PP 6.11
  //
  // Design and implement a program that draws 20 horizontal, evenly spaced
  // parallel lines of random length.
  //**************************************************************************
  
  
  import javax.swing.*;
  import java.awt.*;
  import java.util.*;
  
  public class PP6_11
  {
      public static void main (String[] args)
      {
          JFrame frame = new JFrame ("Lines");
          frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  
          LinesPanel panel = new LinesPanel();
  
          frame.getContentPane().add(panel);
          frame.pack();
          frame.setVisible(true);
      }
  }
  
  class LinesPanel extends JPanel
  {
      private final int WIDTH = 400,HEIGHT = 300, LENGTH = WIDTH / 2;
      private final int SPACE = HEIGHT / 20, NUM_LINES = 20;
      private Random generator;
  </code></pre>
  
  <p>I have completed the assignment and it works just fine. When compiled and run, the code draws 20 lines because I use the "SPACE" variable. I would like to know if there is any way I could tell the program how many lines I would like for it to draw just by using the "NUM_LINES" variable. </p>
  
  <pre><code>    //-----------------------------------------------------------------------
      // Sets up the drawing panel.
      //-----------------------------------------------------------------------
  
      public LinesPanel()
      {
          generator = new Random();
  
          setBackground (Color.black);
          setPreferredSize (new Dimension (WIDTH, HEIGHT));
      }
  
      //-----------------------------------------------------------------------
      // Paints evenly spaced Horizontal lines of random length.
      // lines that are half the width are highlighted with a re color.
      //-----------------------------------------------------------------------
  
      public void paintComponent (Graphics page)
      {
  </code></pre>
  
  <p>Every time I try to use the NUM_LINES = 20 and SPACE = 20 variables in the for loop, it only draws a few lines. Here's the for loop I was using before "for(int i = 0; i &lt;= NUM_LINES; i += SPACE)"  </p>
  
  <pre><code>        for (int i = 0; i &lt;= HEIGHT; i += SPACE)
          {
              int y = generator.nextInt(WIDTH) + 1;
  
              if (y &lt;= LENGTH)
              {
                  page.setColor(Color.red);
                  page.drawLine(0,i,y,i);
              }
              else
              {
                  page.setColor(Color.blue);
                  page.drawLine (0,i,y,i);
              }
          }
  
  
      }
  
  
  
  }
  </code></pre>
  
  <p>Is there a way to determine how many lines I draw AND evenly space it or is the way I've done it the best way to do it?</p>