Here’s some partial code that shows how to load an image and then iterate over the pixels within it.
TerrainTile2 is a simple class storing an id, symbol, color, and description.
The only other gotcha is the load happens asynchronously.
public SurfaceLayer() {
URL url = getClass().getResource("top.gif");
image = Toolkit.getDefaultToolkit().getImage(url);
terrainTileCache.add(new TerrainTile2(
new Integer(0), "default x", Color.GREEN, "."));
terrainTileCache.add(new TerrainTile2(
new Integer(1), "water 1", new Color(0, 0, 64), "~")); // ~
terrainTileCache.add(new TerrainTile2(
new Integer(2), "water 2", new Color(0, 0, 128), "~"));
terrainTileCache.add(new TerrainTile2(
new Integer(3), "water 3", new Color(0, 0, 255), "~")); // ~
terrainTileCache.add(new TerrainTile2(
new Integer(4), "brown 1", new Color(64, 64, 0), "."));
terrainTileCache.add(new TerrainTile2(
new Integer(5), "brown 2", new Color(128, 128, 0), "."));
terrainTileCache.add(new TerrainTile2(
new Integer(6), "brown 3", new Color(255, 255, 0), "."));
terrainTileCache.add(new TerrainTile2(
new Integer(7), "tree 1", new Color(0, 64, 0), "t"));
terrainTileCache.add(new TerrainTile2(
new Integer(8), "tree 2", new Color(0, 128, 0), "t"));
terrainTileCache.add(new TerrainTile2(
new Integer(9), "tree 3", new Color(0, 255, 0), "t"));
terrainTileCache.add(new TerrainTile2(
new Integer(10), "mountain peak 1", new Color(64, 64, 64), "^"));
terrainTileCache.add(new TerrainTile2(
new Integer(11), "mountain peak 2", new Color(128, 128, 128), "^"));
terrainTileCache.add(new TerrainTile2(
new Integer(12), "mountain peak 3", new Color(255, 255, 255), "^"));
terrain[0][0] = terrainTileCache.get(0);
elevation[0][0] = 0;
}
public void load() {
height = image.getHeight(null);
width = image.getWidth(null);
if (height < 0 || width < 0) {
System.out.println("image not loaded yet, try again");
return;
}
// Create empty BufferedImage, sized to Image
BufferedImage buffImage =
new BufferedImage(
width,
height,
BufferedImage.TYPE_INT_ARGB);
// Draw Image into BufferedImage
Graphics g = buffImage.getGraphics();
g.drawImage(image, 0, 0, null);
terrain = new TerrainTile2[width][height];
elevation = new int[width][height];
for (int a = 0; a < height; a++) {
for (int b = 0; b < width; b++) {
int i = b;
int j = height - a - 1;
int rgba = buffImage.getRGB(b, a);
int red = (rgba >> 16) & 0xff;
//int green = (rgba >> 8) & 0xff;
//int blue = rgba & 0xff;
if (red <= 10) {
terrain[i][j] = terrainTileCache.get(1);
} else if (red <= 25) {
terrain[i][j] = terrainTileCache.get(2);
} else if (red <= 50) {
terrain[i][j] = terrainTileCache.get(3);
} else if (red <= 75) {
terrain[i][j] = terrainTileCache.get(4);
} else if (red <= 100) {
terrain[i][j] = terrainTileCache.get(5);
} else if (red <= 125) {
terrain[i][j] = terrainTileCache.get(6);
} else if (red <= 150) {
terrain[i][j] = terrainTileCache.get(7);
} else if (red <= 175) {
terrain[i][j] = terrainTileCache.get(8);
} else if (red <= 200) {
terrain[i][j] = terrainTileCache.get(9);
} else if (red <= 225) {
terrain[i][j] = terrainTileCache.get(10);
} else if (red <= 250) {
terrain[i][j] = terrainTileCache.get(11);
} else {
terrain[i][j] = terrainTileCache.get(12);
}
elevation[i][j] = red;
}
}
}