title: Some help on a Java program to draw a rainbow
body: <p>I'll be candid about this; it is a homework assignment, but can someone guide me in the right direction and explain to me how some parts of the code are supposed to work? The directions are below the code and the questions.</p>
  
  <p>This is my code so far:</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Graphics;
  import java.awt.Container;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  
  public class Rainbow extends JPanel
  {
    // Declare skyColor:
       private final Color skyColor = Color.CYAN;
  
    public Rainbow()
    {
      setBackground(skyColor);
    } 
    // Draws the rainbow.
    public void paintComponent(Graphics g)
    {
  super.paintComponent(g);
  int width = getWidth();    
  int height = getHeight();
  
  // Declare and initialize local int variables xCenter, yCenter
  // that represent the center of the rainbow rings:
  int xCenter = width/2;
  int yCenter = (height * 3) /4;
  
  // Declare and initialize the radius of the large semicircle:
    int largeRadius = width/4;
  
  g.setColor(Color.RED);
  
  // Draw the large semicircle:
   g.fillArc(xCenter,yCenter,largeRadius,height,0,180);
  // Declare and initialize the radii of the small and medium
  // semicircles and draw them:
  int smallRadius = height/4;
   g.setColor(Color.MAGENTA);
  
   g.fillArc(xCenter,yCenter,width,height,0,180);
   int mediumRadius = (int) Math.sqrt(smallRadius * largeRadius);
   g.setColor(Color.GREEN);
   g.fillArc(xCenter,yCenter,width,height,0,180);
  
  
  // Calculate the radius of the innermost (sky-color) semicircle
  // so that the width of the middle (green) ring is the
  // arithmetic mean of the widths of the red and magenta rings:
  
  
  // Draw the sky-color semicircle:
   g.fillArc(xCenter,yCenter,width,height,0,180);
    }
  
    public static void main(String[] args)
    {
  JFrame w = new JFrame("Rainbow");
  w.setBounds(300, 300, 300, 200);
  w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  Container c = w.getContentPane();
  c.add(new Rainbow());
  w.setVisible(true);
     }
  }
  </code></pre>
  
  <p>My questions:  How exactly does fillArc work; I understand what goes in the parameter, but what must one do so each arc differs from one another?
  How does one set one color for each arc?  I tried doing so, and I ended up with the color listed closest the end showing up and overriding the others.
  I'll probably have more as a I continue to code.</p>
  
  <p>These were the directions:</p>
  
  <p>![enter image description here][1]</p>
  
  <p>The “rainbow” is made of four overlapping semicircles. The outer ring is red (Color.RED), the middle one is green (Color.GREEN), and the inner ring has the magenta color (Color.MAGENTA). The innermost semicircle has the same color as the background.</p>
  
  <p>Follow the instructions below and fill in the blanks in Rainbow.java.</p>
  
  <ol>
  <li><p>Start the Rainbow project.</p></li>
  <li><p>Add a complete comment header with your name before the class declaration at the top of the file.</p></li>
  <li><p>Add to the Rainbow class a declaration of a private final field skyColor of the type Color, initialized to Color.CYAN (the color of the sky). In Rainbow’s constructor, set the window’s background to skyColor rather than Color.WHITE.</p></li>
  <li><p>In the paint method, declare local integer variables xCenter and yCenter that represent the coordinates of the center of the rings. Initialize them to 1/2 width and 3/4 height (down) of the content pane, respectively. (Recall that the origin of graphics coordinates in Java is at the upper left corner of the content pane with the y-axis pointing down.) Do not plug in fixed numbers from the window’s dimensions.</p></li>
  <li><p>Declare a local variable largeRadius that represents the radius of the largest (red) semicircle and initialize it to 1/4 of width.</p></li>
  <li><p>A method call g.fillArc(x, y, size, size, from, degrees) (with all integer arguments) draws a sector of a circle. x and y are the coordinates of the upper left corner of the rectangle (in this case a square) into which the oval is (logically) inscribed; size is the side of the square (and the diameter of the circle); from is the starting point of the arc in degrees (with 0 at the easternmost point of the horizontal diameter), and degrees (a positive number) is the measure of the arc, going counterclockwise. Add a statement to the paint method to draw the largest (red) semicircle. Test your program.</p></li>
  <li><p>Add statements to display the medium (green) and small (magenta) semicircles. The radius of the magenta semicircle should be 1/4 of height. The radius of the green one should be the geometric mean (the square root of the product) of the radius of the red semicircle and the radius of the magenta semicircle, rounded to the nearest integer. (A call to Math.sqrt(x) returns the value of square root of x, a double.) Retest your program.</p></li>
  <li><p>Add statements to display the innermost semicircle of the background (“sky”) color to complete the rainbow. Use the skyColor constant for this semicircle’s color. Choose the radius of the sky-color semicircle in such a way that the width of the middle (green) ring is the arithmetic mean of the widths of the red and magenta rings.</p></li>
  <li><p>Test your program.</p></li>
  <li><p>Submit your completed program and run output. Your run output (the rainbow picture) can be included by capturing the screen output (Alt-PrintScrn), pasting it into a graphics program (such as MS Paint) and then saving the image to your Eclipse project directory.</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Graphics;
  import java.awt.Container;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  
  public class Rainbow extends JPanel
  {
    //Declare skyColor:
      private final Color skyColor = Color.CYAN;
  
    public Rainbow()
  {
  setBackground(skyColor);
  }
  
   // Draws the rainbow.
  public void paintComponent(Graphics g)
   {
  super.paintComponent(g);
  int width = getWidth();    
  int height = getHeight();
  
  // Declare and initialize local int variables xCenter, yCenter
  // that represent the center of the rainbow rings:
  int xCenter = width/2;
  int yCenter = (height * 3) /4;
  
  // Declare and initialize the radius of the large semicircle:
    int largeRadius = width/4;
  
  g.setColor(Color.RED);
  
  // Draw the large semicircle:
   g.fillArc(xCenter - largeRadius,yCenter - largeRadius   ,largeRadius,largeRadius,0,180);
  // Declare and initialize the radii of the small and medium
  //semicircles and draw them:
   int smallRadius = height/4;
   int mediumRadius = (int) Math.sqrt(smallRadius * largeRadius);
   g.setColor(Color.GREEN);
   g.fillArc(xCenter-(largeRadius+mediumRadius)/2,yCenter-          (largeRadius+mediumRadius)/2,mediumRadius,mediumRadius,0,180);
   g.setColor(Color.MAGENTA);
   g.fillArc(xCenter-(largeRadius+smallRadius)/2,yCenter-(largeRadius+smallRadius)/2,smallRadius,smallRadius,0,180);
  
  
  
  
  // Calculate the radius of the innermost (sky-color) semicircle
  // so that the width of the middle (green) ring is the
  // arithmetic mean of the widths of the red and magenta rings:
     int skyRadius = (int)((2 * Math.sqrt(smallRadius * largeRadius)) - width/4);
  
  // Draw the sky-color semicircle:
   g.setColor(skyColor);
   g.fillArc(xCenter-skyRadius,yCenter-skyRadius,skyRadius,skyRadius,0,180);
  
   }
  
   public static void main(String[] args)
   {
  JFrame w = new JFrame("Rainbow");
  w.setBounds(300, 300, 300, 200);
  w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  Container c = w.getContentPane();
  c.add(new Rainbow());
  w.setVisible(true);
   }
  }
  </code></pre></li>
  </ol>