Zentdayn
Zentdayn

Reputation: 62

Java applet 2d graphics drawing issue when drawing twice (black parts)

So i had a console game i made, and i thought in making it 2d. So far so good everything was fine. I have movement and other things working fine. And the drawing was fine,until i had this idea.

( my "map" is a Tile[][] ) I thought in creating a class that would represent a layer then a playable map would be a Layer[] and i would draw the layers over each others and maybe store some on memory for faster drawing.

I thought this was a 'good'/'okay' idea.(i would appreciate if someone would tell me if it is a good idea or not)

The problem is when i use 2 Layers the second layer always messes the first one. The code i use to draw the 'map' looks like this:

    for (TileLayer layer : layers)
        g.drawImage(layer.getImage(), 0, 0, null);

the layer.getImage() is correct (pretty sure, works with just one layer). The problem is that when i draw 2 layers the second one blacks out everything in the first layer. I have the layer draw code like this:

    BufferedImage img = new BufferedImage(tiles.length * Tile.TILE_SIZE,
                                          tiles[0].length*Tile.TILE_SIZE, 
                                          ColorSpace.TYPE_RGB);

    for (int x = 0; x < tiles.length; x++) {
        for (int y = 0; y < tiles[0].length; y++) {
            //System.out.println("ID:" + tiles[x][y].getId());
                img.getGraphics().drawImage(tiles[x][y].getImage(),
                                        x * Tile.TILE_SIZE,
                                        y * Tile.TILE_SIZE,
                                        null);
        }
    }

Its a java applet i was overriding 'paint', i have changed that. I tried using a transparent image to represent 'air' ( thought this should work probably messed it up). tried adding a condition to prevent drawing when it shouldn't.

I might have made something that works in a wrong way. Help on a proper way to do this would be nice. (or tell me where I might have it wrong)

Upvotes: 0

Views: 320

Answers (1)

Neil Coffey
Neil Coffey

Reputation: 21815

In the example, you appear to be using the wrong constant as the BufferedImage type.

If you want your BufferedImage to allow transparency, set the last parameter to BufferedImage.TYPE_INT_ARGB (or one of the type constants on BufferedImage that has "A" in the last bit).

Upvotes: 1

Related Questions