Transparency on maps...

Discussion in 'Plugin Development' started by HamOmlet, Feb 19, 2012.

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

    HamOmlet

    If I try rendering a transparent image on a map, the background shows up brown. I've heard it's possible to specify a palette color that, if used, will cause the renderer to display a different color instead; transparent in this case.

    How would I go about doing this?
     
  2. I've had the same problem when I tried to place the image on the map. I created my own drawer for images to exclude drawing the transparent pixels.

    Edit: This is the code I used.
    Code:
    private void drawImage(MapCanvas canvas, Image img) {
        int width = img.getWidth(null);
        int height = img.getHeight(null);
       
        int x0 = (128 / 2) - (width / 2);
        int y0 = (128 / 2) - (height / 2);
       
        int[] pixels = new int[width * height];
        PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, pixels, 0, width);
     
        try {
            pg.grabPixels();
            int tel = 0;
            byte color;
            if (pixels.length > 0) {
                for (int y = 0; y < height; y++) {
                    for (int x = 0; x < width; x++) {
                        int c = pixels[tel];
                        int red = (c & 0x00ff0000) >> 16;
                        int green = (c & 0x0000ff00) >> 8;
                        int blue = c & 0x000000ff;
                        
                        // Don't know if this is the right way to check if the pixel is
                        // transparent, but it worked for me. You can uncomment
                        // the below if part to print the first pixels color to the console.
                        //if(x == 0 && y == 0) {
                        //    System.out.print("  c: " + c);
                        //}
                        if (c != -16777216) {
                            color = MapPalette.matchColor(red, green, blue);
                            canvas.setPixel(x0 + x, y0 + y, color);
                        }
                        tel++;
                    }
                }
            }
        } catch (InterruptedException e) {
        }
    }
    
     
    desht and fromgate like this.
  3. why catching an InterruptedException if it is never thrown?
     
  4. Offline

    HamOmlet

    A HUGE thank you for that code - I honestly would have never been able to figure that out.

    Also, I've sent you a PM. ;)
     
  5. It can be thrown by "pg.grabPixels();"

    And no problem! Just help someone else when in need :)
     
  6. Where is the javadoc of the pixelgrabber?
    EDIT: also how can you edit an MapCanvas and an Image on the same thread, you should acces MapCanvas on the Server Thread, and Image (because it swing) on the event dispatch thread
     
  7. Offline

    desht

    Image is an AWT class, not a Swing class. There's nothing to stop you using it independently of the usual Swing event dispatcher; the Bukkit Map API uses it, for example.
     
  8. Offline

    desht

    Blocking shouldn't be an issue when the PixelGrabber is using a static Image object (probably a BufferedImage?) like dadaemon is using, although using this form of the method might be worth considering just be extra-safe. Would need some consideration on what to do if the method timed out, though - simplest option would be just to log a warning.

    I think a long-term block would only be a risk where the PixelGrabber is using an ImageProvider object, which isn't the case here. An ImageProvider could be doing all sorts of work behind the scenes to provide the image data.
     
  9. First of all, it's old code I wrote. I'm not using this anymore (or at least not yet.)

    Second of all. Why only comment on stuff and never give a good alternative? If you want to make a point give me an example on how to do it right!

    desht Thanks for replying. Your posts are a lot more helpful!
     
Thread Status:
Not open for further replies.

Share This Page