title: How to Randomly pick a color from an array of colors in Java?
body: <p>I'm learning Java and programming for the first time. I'm using the <code>BlueJ</code> environment.</p>
  
  <p>I'm writing a program to draw polygons. I want to change color after every line has been drawn. To do this, I thought I would create an array of colors, then use a pseudo-random number generator to generate a value that would be used as the index to get a color from the array. I've included a snippet of code from the constructor method, as I've been told I need to create the array inside the constructor for this to work.</p>
  
  <p>It should be noted that I've imported java.awt.Color, along with java.util.Random. I've also included</p>
  
  <pre><code>private Color[] colors;
  </code></pre>
  
  <p>as a field.</p>
  
  <pre><code>public PictureMaker()
  {
      world = new TurtleWorld(500, 500, "Picture Maker");
      fred = new Turtle();
      //Allocate the size of the array
      colors = new Color[7];
  
      //Initialize the values of the array
      colors[0] = new Color(Color.red);
      colors[1] = new Color(Color.blue);
      colors[2] = new Color(Color.yellow);
      colors[3] = new Color(Color.green);
      colors[4] = new Color(Color.black);
      colors[5] = new Color(Color.pink);
      colors[6] = new Color(Color.orange);
  }
  </code></pre>
  
  <p>Below is the code from the actual method that will be called to draw the polygon.</p>
  
  <pre><code>/**
   * This method takes sides (int) as input and draws a polygon with that number of sides.
   */
  public void drawPolygon(int sides)
  {
      world.dropIn(fred);
      fred.penDown();
      int angle = 360/sides;
      int linesDrawn = 0;
  
  
      while(linesDrawn &lt; sides)
      {
          Random r = new Random();
          int minColor = 0;
          int maxColor = 10;
          int R = r.nextInt(maxColor-minColor) + minColor;
          fred.right(angle);
          fred.forward(60);
          linesDrawn += 1;
  
      }
  }
  </code></pre>
  
  <p>I haven't included anything to get the color from the array yet, as I can't get the array to work.</p>
  
  <p>I'm getting an error message <code>Incompatible types: java.awt.Color cannot be converted to int</code>.</p>
  
  <p>I'm not sure why it doesn't work.</p>
  
  <p>I hope this makes sense.</p>
  
  <p>Please let me know if you need clarification, I'm new to using this site.</p>