试图让我的代码看起来像咖啡馆墙上的错觉

这是我所拥有的:

http://img1.mukewang.com/6194c43b0001b0fc06530406.jpg

这是我的代码:


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

        }

    }

}

这就是我需要的样子。我觉得我什么都试过了。

http://img3.mukewang.com/6194c44c00011fc805740412.jpg

智慧大石
浏览 140回答 2
2回答

慕勒3428872

我也想让你学习,因为这似乎是一个为教学设计的入门级项目。因此,与其直接提供答案,不如通过解释您当前的程序当前正在做什么来向您展示您的错误所在。// 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) {&nbsp; &nbsp; for (int i = 0; i < multiples * 2; i++) {&nbsp; &nbsp; &nbsp; &nbsp; row(g, size, multiples, x + (offset * i), y + (size * i) + (2 * i));&nbsp; &nbsp; }}这里的代码相对简单。它当前从 0 增量循环 1,对于您要绘制的黑白方块的总数。(在倍数之前停止*2,从0开始是正确的)每次循环时,它都会调用 row.它大致相当于&nbsp; &nbsp; &nbsp; &nbsp; row(g, size, 2, x + (offset * 0), y + (size * 0) + (2 * 0));&nbsp; &nbsp; &nbsp; &nbsp; row(g, size, 2, x + (offset * 1), y + (size * 1) + (2 * 1));&nbsp; &nbsp; &nbsp; &nbsp; row(g, size, 2, x + (offset * 2), y + (size * 2) + (2 * 2));&nbsp; &nbsp; &nbsp; &nbsp; row(g, size, 2, x + (offset * 3), y + (size * 3) + (2 * 3));(它创建的行数是黑色列的两倍)您遇到的问题是您的偏移量总是在增长,而不是来回曲折。where x = 0, and offset = 10&nbsp; &nbsp; &nbsp; &nbsp; rowoffset = x + (offset * 0) = 0&nbsp; &nbsp; &nbsp; &nbsp; rowoffset = x + (offset * 1) = 10&nbsp; &nbsp; &nbsp; &nbsp; rowoffset = x + (offset * 2) = 20&nbsp; &nbsp; &nbsp; &nbsp; rowoffset = x + (offset * 3) = 30但你想要的是where x = 0, and offset = 10&nbsp; &nbsp; &nbsp; &nbsp; rowoffset = 0; // where i == 0&nbsp; &nbsp; &nbsp; &nbsp; rowoffset = 10 // where i == 1&nbsp; &nbsp; &nbsp; &nbsp; rowoffset = 0&nbsp; // where i == 2&nbsp; &nbsp; &nbsp; &nbsp; rowoffset = 10 // where i == 3实现分支行为的常用方法(取决于要做出的决定)是使用 if 语句。因此x+offset*i,您可以在那里引入一个变量,而不是传递给 row,这取决于 i 是奇数还是偶数。计算整数是奇数还是偶数的常用方法是使用余数运算符 ( %),传入数字 2。(但在任一侧使用负值时必须小心)0%2 == 01%2 == 1&nbsp;2%2 == 03%2 == 1~~~8%2 == 09%2 == 1因此,您现在可以使用数学或 if 语句来使您的锯齿形图案看起来像图案。

慕哥6287543

您不断在网格方法中添加 x 参数。如果只想每隔一行移动一次,可以使用如下模运算:public static void grid(Graphics g, int size, int multiples, int x, int y, int offset) {&nbsp; &nbsp; for (int i = 0; i < multiples * 2; i++) {&nbsp; &nbsp; &nbsp; &nbsp; row(g, size, multiples, x + offset * (i % 2), y + (size * i) + (2 * i));&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java