How does this code that draws a stoplight with red, yellow and green lights work?
<p>I'm having a lot of trouble grasping the concept of this example in my text book. The idea is to draw a stoplight with red, yellow and green lights. I have a couple of questions. I'm having trouble figuring out what part of the code does what.</p>

<ol>
<li>Am I right to assume <code>cx</code> and <code>cy</code> are to figure out the center of the page?</li>
<li>Are <code>fx</code> and <code>fy</code> to figure out the center of the frame?</li>
<li>I don't know what <code>dy</code> does and why it's divided by 4 and not 3 for 3 lights and the <code>LAMP_RADIUS</code> totally confuses me. </li>
<li>On all three <code>add(createFilledCircle)</code> for red, yellow and green I don't understand how their position is calculated inside the stoplight frame. </li>
<li>In the method <code>createFilledCircle()</code> I don't understand <code>GOval circle = newGOval(x-r, y-r, 2 * r, 2 * r);</code>. I don't understand what <code>x-r</code> and <code>y-r</code> does and how that relates to position. </li>
</ol>

<pre class="lang-java prettyprint-override"><code>import acm. graphics.*;
import acm. program.*;
import java.awt.*;

public class DrawStoplight extends GraphicsProgram {

    public void run () {
        double cx = getWidth() / 2;
        double cy = getHeight() / 2; 
        double fx = cx - FRAME_WIDTH / 2; 
        double fy = cy- FRAME_HEIGHT / 2; 
        double dy = FRAME_HEIGHT / 4 + LAMP_RADIUS / 2; 
        GRect frame = new GRect(fx, fy, FRAME_WIDTH, FRAME_HEIGHT);
        frame.setFilled(true);
        frame.setColor(Color.GRAY);
        add(frame);
        add(createFilledCircle(cx, cy - dy, LAMP_RADIUS, Color.RED));
        add(createFilledCircle(cx, cy, LAMP_RADIUS, Color.YELLOW));
        add(createFilledCircle(cx, cy + dy, LAMP_RADIUS, Color.GREEN));
    }

    private GOval createFilledCircle(double x, double y, double r, Color color){
        GOval circle = new GOval(x-r, y-r, 2 * r, 2 * r)
        circle.setColor(color);
        circle.setFilled(true);
        return circle;
    }

    private static final double FRAME_WIDTH = 50; 
    private static final double FRAME_HEIGHT = 100; 
    private static final double LAMP_RADIUS = 10; 

}
</code></pre>
