2011-03-08 187 views
1

是否有更优雅/更短/有组织的方式来编写这段代码?Java更优雅的方式来编写if语句与图像

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); 
     } 
    } 
+6

'地图砖;',添加所有那些地图的'。把(“O”,巨石)',使用映射的查找表? – LumpN 2011-03-08 21:26:00

回答

1

某处(?在构造函数),一个Map保存为一个成员变量:

​​

然后,您的绘图看起来像:

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) 
    } 
} 
+0

我试着把代码放到我的编译器中,并声明我的HashMap是这样的:'HashMap images;'。在声明和使用'images.put'时,它给了我警告。另外,当使用'images = new HashMap ();'时,会出现一个错误,它表示在此标记(字符带下划线)之后预期的尺寸? – Bob 2011-03-08 21:52:48

+0

声明它参数化如下:'HashMap '。由于Java的*有趣*类型系统以及它允许​​您用作模板参数,所以还必须使用大写'c'的'Char'。 – 2011-03-08 23:19:33

+0

对不起,它是'字符'而不是'字符'。 – 2011-03-08 23:25:00

5

第一个改进可能是使用一个开关/箱结构,但在你的情况下,一个简单的地图(Map<Char,Image>)会更好。

变本加厉,你可以使用一个枚举而不是字符识别的对象,这将帮助你避免错别字,但最起码​​你应该使用字符常量,像

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

等上。

0

既然你基础断字符,您可以使用switch声明。 你可以做的另一件事是,因为你在任何情况下使用g.drawImage(SOMETHING, j * TILESIZE, i * TILESIZE, null),您可以提取一切交换机后是相同的,以公正分配一些变量,并使用了什么样的变化

EX:

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); 
0

使用一个开关/箱(不是地图),因为编译器将有更多的空间优化,因为它知道char值的精确设定你接通:

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

另外:最快的解决方案是,如果你确定你永远不会获得除你正在测试的值以外的值,那么你可以创建一个**数组** **,大小为255的tile ** array ['@'] = item; ',然后用**单行**替换所有代码:'g.drawImage(tile_array [map [y + i] [x + j]],j * TILESIZE,i * TILESIZE,null);' – necromancer 2011-03-08 21:36:01

0

您可以使用一个开关一个char值如此t可能会更清晰。第二,第三和第四个参数总是相同的,所以可以在开关中设置一个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); 
} 
}