这个用红色,黄色和绿色灯点亮红绿灯的代码如何工作?

时间:2012-10-15 23:33:15

标签: java

我在教科书中掌握这个例子的概念时遇到了很多麻烦。我们的想法是用红色,黄色和绿色的灯光绘制一个红绿灯。我有一些问题。我无法弄清楚代码的哪些部分可以做什么。

  1. 我是否应该假设cxcy是要弄清楚页面的中心?
  2. fxfy是否可以找出框架的中心?
  3. 我不知道dy做了什么以及为什么它除以4而不是3为3灯,而LAMP_RADIUS完全让我感到困惑。
  4. 在红色,黄色和绿色的所有三个add(createFilledCircle)上我都不明白他们的位置是如何在红绿灯框架内计算的。
  5. createFilledCircle()方法中,我不明白GOval circle = newGOval(x-r, y-r, 2 * r, 2 * r);。我不明白x-ry-r的作用以及与位置的关系。
  6. 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; 
    
    }
    

2 个答案:

答案 0 :(得分:1)

  

我是否应该假设cxcy是要弄清楚页面的中心?

  

fxfy是否可以找出框架的中心?

不,它们是左上角的坐标

  

我不知道dy做了什么以及为什么它除以4而不是3为3灯而且LAMP_RADIUS完全让我困惑。

要在盒子内垂直放置三盏灯,你需要一个在中间,一个在1/4高度,一个在3/4高度 - 因此除以四。我不确定为什么LAMP_RADIUS会进入它。它似乎是我通常所说的“软糖因素”,使灯具间隔更宽,即看起来正确的数字,但没有任何正当理由为什么看起来正确... < / p>

  

在红色,黄色和绿色的所有三个add(createFilledCircle)上我都不明白他们的位置是如何在红绿灯框架内计算的。

它们只是垂直间隔dy

  

createFilledCircle()方法中,我不明白GOval circle = newGOval(x-r, y-r, 2 * r, 2 * r);。我不明白x-ry-r的作用以及与位置的关系。

GOval在由大小为(x - r, y - r)的坐标2r定义的框内放置一个圆圈 - 即以{{1}为中心的边长为2r的正方形}

答案 1 :(得分:1)

1. Am I right to assume cx and cy are to figure out the center of the page?

2. Are fx and fy to figure out the center of the frame?

不完全是。他们正在计算框架的左上角。它们从中心开始,并在每个方向上以“框架大小”的一半“备份”。

3. I don't know what dy does and why it's divided by 4 and not 3 for 3 lights and the LAMP_RADIUS totally confuses me. 

在代码中向下看。 dy是灯光之间的垂直距离。黄色光线正好在中心绘制,红色在上方dy,绿色在下方dy。除数为4,因为作者选择将红光的下边缘与框架顶部的框架高度的1/4对齐。同样,他选择将绿光的顶部与底部框架高度的1/4对齐。他本可以选择许多其他计算方法dy

4. On all three add(createFilledCircle) for red, yellow and green I don't understand how their position is calculated inside the stoplight frame. 

它们都具有相同的x坐标:框架的中心。如在3中所解释的那样计算y坐标。记住在屏幕坐标中,正方向是向下的,因此增加y会使光线更低。减少会使它更高。

5. In the method createFilledCircle() I don't understand GOval circle = newGOval(x-r, y-r, 2 * r, 2 * r);. I don't understand what x-r and y-r does and how that relates to position.

阅读newGOval的手册定义。它在矩形内部刻有椭圆形。参数是矩形的左上角,后跟宽度和高度。因此,如果(x,y)是中心,则给出一个具有对角线(x-r,y-r)到(x + r,y + r)的框。当你在其中刻上一个椭圆形时,你会得到一个以(x,y)为中心的圆圈。