这是我所拥有的:
这是我的代码:
import java.awt.*;
public class CafeWall {
public static void main(String[] args) {
DrawingPanel panel = new DrawingPanel(650, 400);
panel.setBackground(Color.GRAY);
Graphics g = panel.getGraphics();
// rows
row(g, 20, 4, 0, 0);
row(g, 30, 5, 50, 70);
// grids
grid(g, 25, 4, 10, 150, 0);
grid(g, 25, 3, 250, 200, 10);
grid(g, 20, 5, 425, 180, 10);
grid(g, 35, 2, 400, 20, 35);
}
// size is the pixel width/height of a square.
// multiples is the number of black/white pairs to draw.
// x,y are the screen position of the top left corner.
public static void row(Graphics g, int size, int multiples, int x, int y) {
for (int i = 0; i < multiples; i++) {
g.setColor(Color.BLACK);
g.fillRect(x + size * 2 * i, y, size, size);
g.setColor(Color.WHITE);
g.fillRect(x + size + size * 2 * i, y, size, size);
g.setColor(Color.BLUE);
g.drawLine(x + size * 2 * i, y, x + size + size * 2 * i, y + size);
g.drawLine(x + size + size * 2 * i, y, x + size * 2 * i, y + size);
}
}
// size is the pixel width/height of a square.
// multiples is the number of black/white pairs to draw.
// x,y are the screen position of the top left corner.
// offset is the amount to offset by.
public static void grid(Graphics g, int size, int multiples, int x, int y, int offset) {
for (int i = 0; i < multiples * 2; i++) {
row(g, size, multiples, x + (offset * i), y + (size * i) + (2 * i));
}
}
}
这就是我需要的样子。我觉得我什么都试过了。
慕勒3428872
慕哥6287543
相关分类