下面的代码出现在我的书中关于方法的章节中。我对几件事有点困惑。
- 当我相信该
run()
方法正在调用该createFilledCircle
方法时,我的理解是否正确? - 方法是
run()
接收者还是createFilledCircle
发送者? - 对于这三个
add(createFilledCircles...red,yellow and green);
,程序员如何知道参数中允许哪些信息?(x location, y location, width of figure, height of figure)
中使用的格式是add(createFilledCircle)
?
import acm.program.*;
import acm.graphics.*;
import java.awt.*;
public class StopLight extends ConsoleProgram {
public void run() {
double cy = getWidth() / 2 ;
double cx= 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(trye);
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 * y );
circle.setFilled(true);
circle.setColor(color);
return circle;
}
private static final double FRAME_WIDTH = 50;
private static final double FRAME_HEIGHT = 100;
private static final LAMP_RADIUS = 10;
}