2016-10-11 22 views
0
import java.awt.*; 

public class CafeWall { 
    public static final int mortar = 2; 

    public static void main(String[] args) { 
     DrawingPanel panel = new DrawingPanel(650, 400); 
     panel.setBackground(Color.GRAY); 
     Graphics g = panel.getGraphics(); 
     row(4, 20, 0, 0, g); 
     row(4, 30, 50, 70, g); 
     grid(0, 4, 25, 10, 150, g); 
     grid(10, 3, 25, 250, 200, g); 
     grid(10, 5, 20, 425, 180, g); 
     grid(35, 2, 35, 400, 20, g); 


    } 
    //This method will produce the two individual rows in CafeWall 
    public static void row(int amount, int size, int x, int y, Graphics g) { 
     for(int squares = 0; squares < amount; squares++){ 
     g.setColor(Color.BLACK); 
     g.fillRect(x+2*squares*size, y, size, size); 
     g.setColor(Color.WHITE); 
     g.fillRect(x+2*squares*size+size, y, size, size); 
     g.setColor(Color.BLUE); 
     g.drawLine(x+2*size*squares, y, x+2*squares*size+size, y+size); 
     g.drawLine(x+2*size*squares, y+size, x+2*size*squares+size, y); 
     } 
    } 
    //This method will produce the grids using the method row 
    public static void grid(int indent, int amount, int size, int x, int y, Graphics g) { 
     for(int rows=0; rows<amount*2; rows++){ 
     row(amount, size, x+indent, y+rows*(size*mortar), g); 

     } 
    } 
} 

CafeWall Illusion的Java參數圖形

在這個項目中我試圖爲網吧牆面錯覺編碼。我不能使用if語句。我已經接近完成了。最後一部分,在方法網格中,編碼x和y值給我一些問題。它看起來像y軸是正確的。但我的x軸不起作用。我認爲所發生的事情是,所有的行都相互重疊。但是我對如何編寫我的x變量感到茫然。每隔一行需要按給定量縮進。例如,中間底部網格需要每隔一行縮進10。但我不能做x +縮進,因爲每行縮進。有沒有提示?

回答

0

由於我沒有DrawingPanel類,因此我無法對其進行測試。我的想法是,在grid()一次畫兩行,只縮進其中一個:

for (int rows = 0; rows < amount * 2; rows += 2) { 
     row(amount, size, x, y + rows * (size * mortar), g); 
     row(amount, size, x + indent, y + (rows + 1) * (size * mortar), g); 
    } 
+0

我按照你的建議做了。它似乎更簡單一些。有正確數量的行之前跳過它們的原因嗎?如行 – user3471031

+0

之間有空格如果我正確理解最後一個問題,我認爲你的'迫擊炮'這樣做。 –