开发者

Java more elegant way to write if statements with images

Is there a more elegant/shorter/organized way of writing this bit of code?

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')
      开发者_C百科          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);
        }
    }


The first improvement could be to use a switch/case structure, but in your case, a simple map (Map<Char,Image>) will be even better.

Going even further, you could use an enum instead of characters to identify the objects, which will help you avoid typos, but at the very least you should be using character constants, like

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

and so on.


Somewhere (in the constructor?), save a Map as a member variable:

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

Then, your drawing will look like:

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


Since you're basing off a character, you can use a switch statement. The other thing you can do is, since you're using g.drawImage(SOMETHING, j * TILESIZE, i * TILESIZE, null) in every case, you can extract everything that's the same to after the switch and just assign some variable and use that for what changes

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


use a switch/case (not a map) because the compiler would have more room to optimize since it knows the exact set of char values you are switching on:

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


You can use a switch with a char value so that might be clearer. Also the 2nd, 3rd and forth parameters are always the same so maybe set a var in the switch and call the method outside. A little pseudo code:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜