2014-07-06 86 views
-4

我是一名初學者,使用Java &我必須製作一個java程序,它從尺寸爲347 * 697(數組的值範圍從2200到4200)的二維數組中輸入數據。我必須這樣做,即每增加100次範圍內都有一組特定的顏色(例如2200-2300色(214,217,223)&等)。我從來沒有做過任何事情.. &這是我發現的程序有點類似...任何人都可以給我想法如何轉換它 我知道它太多要求,但明天我必須給一個PPT。 &當每個選項關閉時,我來到這裏。請傢伙,我需要幫助嚴重Java GUI程序將二維數組值轉換爲圖形

import javax.swing.*; 
import java.awt.*; 
import java.util.Random; 

public class Map extends JPanel { 

public static final Color CITY = new Color(214,217,223); 
public static final Color DESERT = new Color(255,204,102); 
public static final Color DIRT_ROAD = new Color(153,102,0); 
public static final Color FOREST = new Color(0,102,0); 
public static final Color HILLS = new Color(51,153,0); 
public static final Color LAKE = new Color(0,153,153); 
public static final Color MOUNTAINS = new Color(102,102,255); 
public static final Color OCEAN = new Color(0,0,153); 
public static final Color PAVED_ROAD = new Color(51,51,0); 
public static final Color PLAINS = new Color(102,153,0); 

public static final Color[] TERRAIN = { 
    CITY, 
    DESERT, 
    DIRT_ROAD, 
    FOREST, 
    HILLS, 
    LAKE, 
    MOUNTAINS, 
    OCEAN, 
    PAVED_ROAD, 
    PLAINS 
}; 

public static final int NUM_ROWS = 215; 
public static final int NUM_COLS = 300; 

public static final int PREFERRED_GRID_SIZE_PIXELS = 10; 

// In reality you will probably want a class here to represent a map tile, 
// which will include things like dimensions, color, properties in the 
// game world. Keeping simple just to illustrate. 
private final Color[][] terrainGrid; 

public Map(){ 
    this.terrainGrid = new Color[NUM_ROWS][NUM_COLS]; 
    Random r = new Random(); 
    // Randomize the terrain 
    for (int i = 0; i < NUM_ROWS; i++) { 
     for (int j = 0; j < NUM_COLS; j++) { 
      int randomTerrainIndex = r.nextInt(TERRAIN.length); 
      Color randomColor = TERRAIN[randomTerrainIndex]; 
      this.terrainGrid[i][j] = randomColor; 
     } 
    } 
    int preferredWidth = NUM_COLS * PREFERRED_GRID_SIZE_PIXELS; 
    int preferredHeight = NUM_ROWS * PREFERRED_GRID_SIZE_PIXELS; 
    setPreferredSize(new Dimension(preferredWidth, preferredHeight)); 
} 

@Override 
public void paintComponent(Graphics g) { 
    // Important to call super class method 
    super.paintComponent(g); 
    // Clear the board 
    g.clearRect(0, 0, getWidth(), getHeight()); 
    // Draw the grid 
    int rectWidth = getWidth()/NUM_COLS; 
    int rectHeight = getHeight()/NUM_ROWS; 

    for (int i = 0; i < NUM_ROWS; i++) { 
     for (int j = 0; j < NUM_COLS; j++) { 
      // Upper left corner of this terrain rect 
      int x = i * rectWidth; 
      int y = j * rectHeight; 
      Color terrainColor = terrainGrid[i][j]; 
      g.setColor(terrainColor); 
      g.fillRect(x, y, rectWidth, rectHeight); 
     } 
    } 
} 

public static void main(String[] args) { 
    // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      JFrame frame = new JFrame("Game"); 
      Map map = new Map(); 
      frame.add(map); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.pack(); 
      frame.setVisible(true); 
     } 
    }); 
} 

}

+1

這個問題似乎是脫離主題,因爲它主要是工作或家庭作業轉儲。 –

+0

@HovercraftFullOfEels我只需要邏輯我怎樣才能使用不同的顏色爲陣列中的不同值...我不想讓你解決它的傢伙..Trust我真的很重要,&我只需要一點幫助 – Shirish

+1

信任對我們來說,展現你努力的成果對我們很重要。這不是一個爲你做作業的網站。 –

回答

0

創建你想要的圖像的每個像素的RGB值的byte[]byte[]長度將爲width * height * 3(每個像素3個值)。有關如何從byte[]創建BufferedImage的示例,請參閱this

另一種替代方法是直接爲每個像素創建一個BufferedImage和set rgb值。在這種情況下,您必須將所有3個rgb值轉換爲一個值爲int的值。

+0

第二種方法你建議,我可以通過使用for循環定義rgb值的範圍值? – Shirish

+0

您必須爲每個像素設置特定的rgb值。這可能會在寬度和高度的嵌套for循環內完成。 –

+0

感謝您的幫助。現在我知道我必須做什麼。再次感謝 :) – Shirish