title: Draw equilateral triangles using a nested for loop?
body: <p>I am trying to figure out how to use a nested for loop to produce this pattern below: </p>
  
  <p><a href="https://i.stack.imgur.com/DZFqI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DZFqI.png" alt="Equilateral Triangle Pattern"></a></p>
  
  <p>Thus far, I have</p>
  
  <pre><code>public class Triangle {
  
  public static final int SIZE = 600;
  public static final int INITIAL_X = 300;
  public static final int INITIAL_Y = 50;
  public static final int SIDE = 100;
  
  /**
   * 
   * 
   * @param args command line arguments
   */
  public static void main(String[] args) {
      Scanner console = new Scanner(System.in);
      DrawingPanel panel = new DrawingPanel(SIZE, SIZE);
      panel.setBackground(Color.WHITE);
      System.out.print("Enter number of rows (1-5): " );
      int row = console.nextInt();
      if(row &lt; 1) {
          row = 1;
      } else if(row &gt; 5) {
          row = 5;
      }
      System.out.print("Specify red value (0-255): ");
      int red = console.nextInt();
      if(red &lt; 0) {
          red = 0;
      } else if(red &gt; 255) {
          red = 255;
      System.out.print("Specify green value (0-255): ");
      int green = console.nextInt();
      if(green &lt; 0) {
          green = 0;
      } else if(green &gt; 255) {
          green = 255;
      }
      System.out.print("Specify blue value (0-255): ");
      int blue = console.nextInt();
      if(blue &lt; 0) {
          blue = 0;
      } else if(blue &gt; 255) {
          blue = 255;
      }
      Graphics g = panel.getGraphics(); //Initializes graphics panel.
      g.setColor(new Color(red, green, blue));
  
      for (int i = 1; i &lt;= row; i++) {
          for (int x = 1; x &lt;= row; x++) {
              int lx = sx - SIDE/2;
              int rx = sx + SIDE/2;
              int ly = (int) (sy + SIDE*Math.sqrt(3)/2);
              int ry = ly;
  
  
      System.out.println("\n*CLOSE the Drawing Panel to exit the program*");
  }       
  
  /**
   * Draws an equilateral triangle with topmost point at (x, y) with the
   * given side length and color.
   * 
   * @param g
   * @param color
   * @param x
   * @param y
   * @param sideLength
   */
  
  public static void drawTriangle(Graphics g, int sx, int sy, int rows) {
      g.drawLine(sx, sy, lx, ly);
      g.drawLine(sx, sy, rx, ry);
      g.drawLine(lx, ly, rx, ry);
  }
  </code></pre>
  
  <p>This code is definitely not finished. I only want to use the java.awt.* and java.util.* packages to solve this. My trouble is mainly with creating a nested for loop in the main method, but I believe I have a grasp on the scanner part of this assignment. I have used the equilateral triangle formulas below that are derived from the Pythagorean theorem. Each row is supposed to have that many triangles (for instance, the 5th row is supposed to have 5 triangles). The triangles are contiguous and the lower left/right points of each triangle form the uppermost points of the triangles in the row below. The (x, y) coordinates of the topmost point of the triangle in the first row must be (300, 50). Please let me know if anyone can help.</p>
  
  <p><a href="https://i.stack.imgur.com/ARuIv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ARuIv.png" alt="Equilateral Triangle formulas used to created pattern."></a></p>