[Code/Method] Enabling transparency in maps

Discussion in 'Resources' started by Norbo11, Sep 20, 2012.

Thread Status:
Not open for further replies.
  1. Offline


    Ever wanted to draw images with transparent pixels on a map?

    Currently if you do that using the .drawImage method of MapCanvas, your transparent pixels will simply be replaced by yellow pixels which are supposed to be the "paper" of the default map texture.

    However, by utilizing MapCanvas's .setPixel function and a PixelGrabber, you can draw the image by simply setting the non-transparent pixels yourself.

    public void drawImageWithTransparency(MapCanvas canvas, BufferedImage img, int posX, int posY)
            try {
                int height = img.getHeight();
                int width = img.getWidth();
                int i = 0;
                int[] pixels = new int[width * height];
                PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, pixels, 0, width);
                for (int y = 0; y < height; y++)
                    for (int x = 0; x < width; x++)
                        if (pixels[i] != 16777215)
                            Color c = new Color(img.getRGB(x, y));
                            int red = c.getRed();
                            int green = c.getGreen();
                            int blue = c.getBlue();
                            canvas.setPixel(posX + x, posY + y, MapPalette.matchColor(red, green, blue));
            } catch (InterruptedException e) {
    So now instead of using the .drawImage method of MapCanvas, you use this custom made method (whenever the image you are drawing contains transparent pixels, of course).



    mapCanvas.drawImage(35, 29, image.png);

    drawImageWithTransparency(mapCanvas, image.png, 35, 29);

    P.S. I want to give some credit to dadaemon for posting original code here. His method didn't work for me which led me to make my own variation.

    P.P.S. I may release a full tutorial on general usage of the Map API soon.

    P.P.P.S. I hope this helped.
    WarmakerT and codename_B like this.
  2. Offline


    "P.P.S. I may release a full tutorial on general usage of the Map API soon." I can't wait for that :D
  3. Offline


    Great! I am going to test it by myself!
Thread Status:
Not open for further replies.

Share This Page