tags:

views:

44

answers:

1

Hello, I'm trying to draw a map from a textfile onto a Bitmap, but the Bitmap is all black

First i load a textfile that looks like this (shortened):

0000000000 (200 width)
0111111110
0111100010
0000111110
0000111000
0000000000
(120 height)

Where "1" is ground and "0" is a wall, 1 should be white and 0 should be black.

Code:

public Map(String map) {
    this.map = map;
    init();
}

public void init() {
    mapArray = new int[WIDTH*HEIGHT];
    String[] splitMap = map.split("\n");
    int width = splitMap[0].length();
    int height = splitMap.length;
    int[] colors = new int[WIDTH * HEIGHT];
    for(int y = 0; y < height; y++) {
        for(int x = 0; x < width; x++) {
            int type = Integer.valueOf(splitMap[y].charAt(x));
            setType(x, y, type);
            if(type == WALL) {
                setColor(x, y, Color.rgb(0, 0, 0), colors);
            } else if(type == GROUND) {
                setColor(x, y, Color.rgb(255, 255, 255), colors);
            } else if(type == GOAL) {
                setColor(x, y, Color.rgb(255, 255, 255), colors);
            }
        }
    }
    bitmap = Bitmap.createBitmap(colors, WIDTH, HEIGHT, Config.ARGB_8888);
}

public void setColor(int x, int y, int color, int[] colors) {
    for(int y1 = 0; y1 < 4; y1++) {
        for(int x1 = 0; x1 < 4; x1++) {
            colors[(x + x1) + (y + y1) * WIDTH] = color;
        }
    }
}

public void setPixel(int x, int y, int color) {
    for(int y1 = 0; y1 < 4; y1++) {
        for(int x1 = 0; x1 < 4; x1++) {
            bitmap.setPixel(x + x1, y + y1, color);
        }
    }
}

public void setType(int x, int y, int type) {
    for(int y1 = 0; y1 < 4; y1++) {
        for(int x1 = 0; x1 < 4; x1++) {
            mapArray[(x + x1) + (y + y1) * WIDTH] = type;
        }
    }
}

public int getType(int x, int y) {
    return mapArray[x + y * WIDTH];
}

public void doDraw(Canvas canvas) {
    canvas.drawBitmap(bitmap, 0, 0, null);
}

private final static int WALL = 0;
private final static int GROUND = 1;
private final static int GOAL = 2;
private final static int WIDTH = 800;
private final static int HEIGHT = 480;
private int[] mapArray = null;
private String map = null;
public Bitmap bitmap;
A: 
int type = Integer.valueOf(splitMap[y].charAt(x));

That's the line that's causing the problem.

scala> Integer.valueOf('1')             
res3: java.lang.Integer = 49

scala> Integer.valueOf('0')
res4: java.lang.Integer = 48

The problem is that charAt gives you a char, which you then convert into an integer. What you really want to do is Integer.parseInt(splitMap[y].substring(x,x+1)

Integer.valueOf("0".substring(0,1))  
res7: java.lang.Integer = 0

This illustrates a lesson though - never leave a switch statement without providing a default; similarly never leave an if/else if/... without leaving an else. Even if you're expecting never to hit it, you should put some noticeable error message if you do; it will help you debug.

I82Much