我需要帮助解决一些代码。我正在上我的第一个 Java 课程,所以它非常基础。本质上,我需要使用 switch 语句来绘制一系列 3 栋房屋,但每栋房屋都绘制在不同的位置和不同的墙壁颜色。下面是我一直在玩的代码。
public class Neighborhood extends Applet {
public void paint (Graphics page) {
int xOffset = 50;
int yOffset = 0;
Color houseColor = Color.yellow;
switch (houseColor) {
case 0: houseColor = Color.red;
case 1: houseColor = Color.blue;
case 2: houseColor = Color.green;
Polygon poly = new Polygon(); // Roof Polygon
poly.addPoint (50,90);
poly.addPoint (150, 50);
poly.addPoint (250, 90);
page.setColor (new Color(218,165,32)); // Custom brown color
page.fillPolygon (poly);
page.setColor (Color.black);
page.drawLine (50, 90, 150, 50); // Roof outline
page.drawLine (150, 50, 250, 90);
page.setColor (Color.green);
page.fillRect (50, 90, 200, 100); // House base with houseColor
page.setColor (Color.black);
page.drawRect (50, 90, 200, 100); // House outline
page.setColor (Color.black);
page.fillRect (75, 110, 30, 25); // Window 1
page.fillRect (190, 110, 30, 25); // Window 2
page.setColor (Color.white);
page.drawLine (75, 123, 105, 123); // Window Frame 1
page.drawLine (89, 110, 89, 134);
page.fillRect (70, 110, 5, 25); // Shutter 1
page.fillRect (105, 110, 5, 25); // Shutter 2
page.drawLine (75+115, 123, 105+115, 123); // Window Frame 2
page.drawLine (89+115, 110, 89+115, 134);
page.fillRect (70+115, 110, 5, 25); // Shutter 3
page.fillRect (105+115, 110, 5, 25); // Shutter 4
page.setColor (Color.red);
page.fillRect (130, 150, 35, 40); // Door
page.setColor (Color.blue);
page.fillOval (155, 170, 4, 4); // Door knob
}
}}
它相当简单,我不需要它来交互或做任何疯狂的事情。我已经将这段代码更改了大约一百万次,试图让它工作,但我意识到是时候让更好的眼睛来看看了。任何帮助表示赞赏。谢谢。