简体   繁体   English

如何制作透明缓冲图像

[英]How do I make Transparent BufferedImage

I am making an program which presents different versions of the electoral colleges.我正在制作一个展示不同版本的选举团的程序。 Each state has a unique color that way I can divide them and make a new BufferedImage out of them.每个 state 都有一种独特的颜色,这样我就可以将它们分开并从中制作一个新的 BufferedImage。 I need that BufferedImage to be transparent that way I can reconstruct the US without overlapping a black rectangle on the states.我需要 BufferedImage 是透明的,这样我就可以在不与各州重叠黑色矩形的情况下重建美国。 I tried making each pixel transparent yet it still appears black.我尝试使每个像素透明,但它仍然显示为黑色。

Here is just the part which makes the black pixels transparent.这只是使黑色像素透明的部分。 (dr is a Graphics2D and state is the BufferedImage) (dr 是 Graphics2D,state 是 BufferedImage)

for (int u = 0; u < state.getHeight(); u++) {

        for (int o = 0; o < state.getWidth(); o++) {
            
            Color rgbValue = new Color(state.getRGB(o, u));
            int red = rgbValue.getRed();
            int green = rgbValue.getGreen();
            int blue = rgbValue.getBlue();

            if (red == 0 && green == 0 && blue == 0) {
                dr.setColor(new Color(0, 0, 0, 0));
                dr.fillRect(o, u, 1, 1);
            }
        }
    }

If nothing is drawn in a BufferedImage, the pixel becomes "null" which the BufferedImage turns into no information therefore black.如果在 BufferedImage 中未绘制任何内容,则像素变为“空”,而 BufferedImage 将变为无信息,因此为黑色。 You have to draw something over those pixels and then in a new loop make those transparent.你必须在这些像素上绘制一些东西,然后在一个新的循环中使它们透明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM