首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >试图让我的代码看起来像咖啡厅墙的幻觉

试图让我的代码看起来像咖啡厅墙的幻觉
EN

Stack Overflow用户
提问于 2018-10-15 00:22:25
回答 1查看 2.4K关注 0票数 0

我现在拥有的是:

这是我的密码:

代码语言:javascript
复制
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));
        }
    }
}

我需要它看起来像这样。我觉得我什么都试过了。

EN

回答 1

Stack Overflow用户

发布于 2018-10-15 00:47:41

您经常在网格方法中添加x参数。

如果只想每隔一行移动一次,可以使用如下的模块化操作:

代码语言:javascript
复制
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 % 2), y + (size * i) + (2 * i));
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52808256

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档