簡體   English   中英

Java更優雅的方式編寫帶有圖像的if語句

[英]Java more elegant way to write if statements with images

有沒有更優雅/更簡短/更有條理的方式來編寫這段代碼?

for (int i = 0; i < SCREENSIZE; i++) {
        for (int j = 0; j < SCREENSIZE; j++) {
            if (map[y + i][x + j] == '@')
                g.drawImage(item, j * TILESIZE,i * TILESIZE, null);
            else if (map[y + i][x + j] == ' ')
                g.drawImage(ground, j * TILESIZE,i * TILESIZE, null);
            else if (map[y + i][x + j] == 'i')
                g.drawImage(bush, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == '~')
                g.drawImage(ocean, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == '=')
                g.drawImage(fence, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == '#')
                g.drawImage(grass, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == 'Y')
                g.drawImage(townsPerson, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == '/')
                g.drawImage(house01, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == '¯')
                g.drawImage(house02, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == '\\')
                g.drawImage(house03, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == '[')
                g.drawImage(house04, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == 'n')
                g.drawImage(house05, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == '_')
                g.drawImage(house06, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == ']')
                g.drawImage(house07, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == '`')
                g.drawImage(cground, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == 'O')
                g.drawImage(boulder, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == 'Ÿ')
                g.drawImage(alien, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == '.')
                g.drawImage(tree01, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == 'T')
                g.drawImage(tree02, j * TILESIZE, i * TILESIZE, null);
        }
    }

第一個改進可能是使用switch / case結構,但是在您的情況下,一個簡單的map( Map<Char,Image> )會更好。

更進一步,您可以使用枚舉代替字符來標識對象,這將幫助您避免輸入錯誤,但是至少您應該使用字符常量,例如

public static final char MAP_ITEM = '@';
public static final char MAP_GROUND = ' ';

等等。

在某個地方(在構造函數中?),將Map保存為成員變量:

images = new HashMap<Character, Image>();
images.put('@', item);
images.put(' ', ground);

然后,您的繪圖將如下所示:

for (int i = 0; i < SCREENSIZE; i++) {
    for (int j = 0; j < SCREENSIZE; j++) {
        g.drawImage(images.get(map[y+i][x+j]), j * TILESIZE, i * TILESIZE, null)
    }
}

由於您基於字符,因此可以使用switch語句。 您可以做的另一件事是,由於在每種情況下都使用g.drawImage(SOMETHING, j * TILESIZE, i * TILESIZE, null) ,因此您可以在切換后提取g.drawImage(SOMETHING, j * TILESIZE, i * TILESIZE, null)相同的所有內容,並只分配一些變量並用它來改變

例如:

Object graphic;
switch (map[y + i][x + j]) {
case '@': 
    graphic = item;
    break;
case '#':
    graphic = grass;
    break;
// etc....
}
g.drawImage(graphic, j * TILESIZE, i * TILESIZE, null);

使用開關/大小寫(不是映射),因為編譯器會知道要打開的確切char值集,因此有更多優化空間:

...
switch(map[y+i][x+j]) {
    case: '@': image = item; break;
    case: ' ': image = ground; break;
    ...
}
g.drawImage(image, j * TILESIZE, i * TILESIZE, null);
...

您可以使用帶有char值的開關,這樣可能會更清晰。 同樣,第二,第三和第四參數始終相同,因此可以在開關中設置一個var並在外部調用該方法。 一些偽代碼:

for() {
  for() {
     Image obj;
     switch (map[y+i][x +j]) {
       case '@':
         Obj = item;
         break;
     // other cases
       default:
       //default or error handling
   }
   g.drawImage(obj, j * TILESIZE, i * TILESIZE, null);
 }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM