2011-03-09 35 views
3

我有一個尺寸爲[20] [20]的數組,填充值爲0的& 1。我想繪製一張類似於天氣圖像的圖表。其中1的表示與一些顏色和零,沒有活動的活動......什麼是我需要開始密謀在java中繪圖?

本質的東西謝謝 截拳道

+0

0和1的?你的意思是布爾值? – dacwe 2011-03-09 07:46:14

+0

整數值.... – 2011-03-09 07:48:40

+0

更新了我的答案(用int矩陣和顏色)。 – dacwe 2011-03-09 07:56:02

回答

5

的代碼(下圖)是一個基本的例子,你是什麼想做。它會產生這樣的圖像:

screenshot

public static void main(String[] args) { 
    JFrame frame = new JFrame("Test"); 

    final int[][] map = new int[10][10]; 
    Random r = new Random(321); 
    for (int i = 0; i < map.length; i++) 
     for (int j = 0; j < map[0].length; j++) 
      map[i][j] = r.nextBoolean() ? r.nextInt() : 0; 

    frame.add(new JComponent() { 
     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 

      int w = getWidth()/map.length; 
      int h = getHeight()/map[0].length; 

      for (int i = 0; i < map.length; i++) { 
       for (int j = 0; j < map[0].length; j++) { 
        if (map[i][j] != 0) { 
         g.setColor(new Color(map[i][j])); 
         g.fillRect(i * w, j * h, w, h); 
        } 
       } 
      } 
     } 
    }); 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(400, 400); 
    frame.setVisible(true); 
}