gpt4 book ai didi

java - 试图让我的代码看起来像咖啡馆的墙壁幻觉

转载 作者:行者123 更新时间:2023-12-02 01:52:13 25 4
gpt4 key购买 nike

这是我所拥有的:
Here's what I have

这是我的代码:

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));
}
}
}

这就是我需要的样子。我觉得我已经尝试了一切。

cafe wall illusion

最佳答案

@drifter265 我想回答,但我也希望你学习,因为这似乎是一个专为教学设计的入门级项目。

因此,我不是直接提供答案,而是通过解释当前程序当前正在执行的操作来向您展示错误所在。

// 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));
}
}

这里的代码相对简单。

它当前从 0 增量循环 1,以获得您想要绘制的黑白方 block 的总数。 (在倍数*2之前停止,从 0 开始是正确的)

每次循环时,都会调用 row。

大致相当于

        row(g, size, 2, x + (offset * 0), y + (size * 0) + (2 * 0));
row(g, size, 2, x + (offset * 1), y + (size * 1) + (2 * 1));
row(g, size, 2, x + (offset * 2), y + (size * 2) + (2 * 2));
row(g, size, 2, x + (offset * 3), y + (size * 3) + (2 * 3));

(它创建的行数是黑色列的两倍)

您遇到的问题是您的偏移量总是在增长,而不是来回曲折。

where x = 0, and offset = 10
rowoffset = x + (offset * 0) = 0
rowoffset = x + (offset * 1) = 10
rowoffset = x + (offset * 2) = 20
rowoffset = x + (offset * 3) = 30

但是你想要的是

where x = 0, and offset = 10
rowoffset = 0; // where i == 0
rowoffset = 10 // where i == 1
rowoffset = 0 // where i == 2
rowoffset = 10 // where i == 3

实现分支行为的常见方法是使用 if 语句,这取决于要做出的决定。

因此,您可以不将 x+offset*i 传递给 row,而是在其中引入一个变量,该变量取决于 i 是奇数还是偶数。

计算整数是奇数还是偶数的常用方法是使用 remainder operator (%) ,传入数字2。(但是在两边使用负值时必须小心)

0%2 == 0
1%2 == 1
2%2 == 0
3%2 == 1
~~~
8%2 == 0
9%2 == 1

因此,您现在可以使用数学或 if 语句来制作锯齿形图案。

关于java - 试图让我的代码看起来像咖啡馆的墙壁幻觉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52808256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com