2017-07-18 79 views
0

我一直在試圖爲破磚遊戲生成一個隨機彩色框的網格。然而,鑑於這些代碼,顏色不斷變化。我希望他們隨機設置並保持這種狀態。如何用java中的randm顏色填充二維數組?

for(int i = 0; i < map.length; i++) { 
     for(int j = 0; j < map [0].length; j++) { 
      if(map[i][j] > 0) { //make brick if greater than 0, else don't 

       int color = (int) (Math.random() * 256); 
       g.setColor(new Color(color, color, color)); 
       g.fillRect(j * brickWidth + 80, i * brickHeight + 50, brickWidth, brickHeight); 


       g.setStroke(new BasicStroke(3)); 
       g.setColor(Color.black); 
       g.drawRect(j * brickWidth + 80, i * brickHeight + 50, brickWidth, brickHeight); 
      } 
     } 
    } 
+0

在構造函數中創建一次2d數組,並在繪製時只讀取它。 – QBrute

回答

0

每次您的組件需要調整大小或移動,repaint()方法被調用來更新它的狀態。所以,如果你在下面的例子中生成paintComponent的顏色,如:

public class MyComponent extends JComponent { 
    @Override 
    protected void paintComponent(Graphics g) { 
     // generate colors and draw grid 
    } 
} 

然後顏色會改變,導致repaint呼叫resize事件或其他事件,因爲repaint調用paintComponent方法。如果你想顯示相同的顏色,只需移動產生,顏色碼出這個方法的:

public class MyComponent extends JComponent { 

    private final Color[][] gridColors = randomGridColors(5, 5); 

    private Color[][] randomGridColors(int rows, int columns) { 
     Color[][] gridColors = new Color[rows][columns]; 
     for (int i = 0; i < rows; i++) { 
      for (int j = 0; j < columns; j++) { 
       gridColors [i][j] = randomColor(); 
      } 
     } 
    } 

    private Color randomColor() { 
     int rgbValue = (int) (Math.random() * 256); 
     return new Color(rgbValue, rgbValue, rgbValue); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     // draw grid 
    } 
} 
0

您目前創建隨機顏色每次你畫你的磚,它可以每秒多次的時間。 創建一些磚類,併爲每個磚塊生成一次顏色。 類似這樣的:

public class TryThis { 

private static final Logger LOG = Logger.getLogger(TryThis.class.getName()); 

public static void main(String[] args) { 
    SwingComponent panel = new SwingComponent(); 
    JFrame frame = new JFrame("try me"); 
    frame.setSize(800, 600); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setContentPane(panel); 
    frame.setVisible(true); 
} 

static class SwingComponent extends JPanel { 

    int W = 10; 
    int H = 10; 
    int recwidth = 20; 
    int recheight = 10; 

    Brick[][] bricks = new Brick[H][W]; 

    public SwingComponent() { 
     super(); 
     for (int y = 0; y < H; y++) { 
      for (int x = 0; x < W; x++) { 
       bricks[y][x] = new Brick(createRandomColor(), new Rectangle(x * recwidth, y * recheight, recwidth, 
        recheight)); 
      } 
     } 
    } 

    @Override 
    public void paint(Graphics g) { 
     for (int y = 0; y < H; y++) { 
      for (int x = 0; x < W; x++) { 
       bricks[y][x].draw(g); 
      } 
     } 
    } 

    Color createRandomColor() { 
     return new Color((int) (Math.random() * 256), (int) (Math.random() * 256), (int) (Math.random() * 256)); 
    } 
} 

static class Brick { 

    Color col; 
    Rectangle rec; 

    public Brick(Color col, Rectangle rec) { 
     this.col = col; 
     this.rec = rec; 
    } 

    public void draw(Graphics g) { 
     g.setColor(col); 
     g.fillRect(rec.x, rec.y, rec.width, rec.height); 
    } 
} 
}