亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

試圖讓我的代碼看起來像咖啡館墻上的錯覺

試圖讓我的代碼看起來像咖啡館墻上的錯覺

智慧大石 2021-11-17 16:57:51
這是我所擁有的:這是我的代碼: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));        }    }}這就是我需要的樣子。我覺得我什么都試過了。
查看完整描述

2 回答

?
慕勒3428872

TA貢獻1848條經驗 獲得超6個贊

我也想讓你學習,因為這似乎是一個為教學設計的入門級項目。


因此,與其直接提供答案,不如通過解釋您當前的程序當前正在做什么來向您展示您的錯誤所在。


// 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,對于您要繪制的黑白方塊的總數。(在倍數之前停止*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 是奇數還是偶數。


計算整數是奇數還是偶數的常用方法是使用余數運算符 ( %),傳入數字 2。(但在任一側使用負值時必須小心)


0%2 == 0

1%2 == 1 

2%2 == 0

3%2 == 1

~~~

8%2 == 0

9%2 == 1

因此,您現在可以使用數學或 if 語句來使您的鋸齒形圖案看起來像圖案。


查看完整回答
反對 回復 2021-11-17
?
慕哥6287543

TA貢獻1831條經驗 獲得超10個贊

您不斷在網格方法中添加 x 參數。


如果只想每隔一行移動一次,可以使用如下模運算:


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

    }

}


查看完整回答
反對 回復 2021-11-17
  • 2 回答
  • 0 關注
  • 165 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號