2016-03-11 51 views
1

layout從彩色png佈局圖像讀取陣列中的圖案?

#FFFFFF = 0 = black background 
#FF0000 = 1 = corner, rotated through 0° https://i.imgur.com/8H5PL63.png 
#FF8000 = 2 = corner, rotated through 90° 
#FFFF00 = 3 = corner, rotated through 180° 
#80FF00 = 4 = corner, rotated through 270° 
#00FF00 = 5 = pipe, rotated through 0° https://i.imgur.com/UA2z1QZ.png 
#00FF80 = 6 = pipe, rotated through 90° 

所以我想讀的圖像,並自動填寫我的int[][] layout陣列0-6。完整的圖像是896(w) x 992(h),每個瓦片是32x32 pixels大。

每個瓷磚都有1px的邊框,所以顏色是30x30 pixels大。
enter image description here

這是我的Java代碼,到現在爲止(只有Grid.class):

public class Grid { 
private static int[][] layout; 

public Grid() { 
    layout = new int[][]{ //28x31 
      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//5 
      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//10 
      {0,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,0,0,0,0}, 
      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//30 
      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} 
    }; 
} 

}

+1

你可以也將數組初始化爲'layout = new int [28] [31];'沒有多少0,確切的問題是什麼? – skandigraun

回答

2

這裏是解決方案一步一步:

  1. 研究以下commands

    javax.imageio.ImageIO.read(new File("filename.png")); java.awt.image.BufferedImage.getRGB(int x, int y);

  2. 加載並讀取該圖片。最好的辦法是從每個第16個像素獲得一個顏色(具有32x32的圖塊 - 從瓦片中間讀取是安全的)。

  3. 比較一個顏色是否與你的模式匹配。
  4. 如果匹配,寫入您的layout[i][j]陣列。
  5. 重複此循環,直到您到達圖像的末端。 (不要忘了i++;j++;在一個週期的結束。
  6. 你有一個結果,只是通過28x31陣列循環是868個業務,所以不用擔心速度。
+0

這太好了!我搜索了這個'getRGB(int x,int y )'!!謝謝:) – 8m47x

+1

[看看我的結果:]](https://i.imgur.com/G54x8bi.png) – 8m47x

+0

哇啊,好男人!!! ;)) –