繁体   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