我对 Java 很陌生,发现自己在循环时遇到了一些麻烦。我将首先设计一个简单的小程序来建造房屋,为此我有以下代码:
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Polygon;
public class Houseref extends Applet
{
public void paint (Graphics page)
{
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.yellow);
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.blue);
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.blue);
page.fillRect (130, 150, 35, 40); // Door
page.setColor (Color.red);
page.fillOval (155, 170, 4, 4); // Door knob
}
}
现在我需要创建一个循环迭代 5 次,每次新房子必须有不同的颜色和不同的位置。我无法理解如何让小程序循环。任何帮助表示赞赏!