-2

I'm Currently making a Game, and I need to set a couple Pixels of a BufferedImage (loaded using ImageIO.read) to be transparent in the fastest, best way.

I can't really find any other topic with this question, and If I do the answer Doesn't really help/fit What I need.

Thanks :)

Zambonie
  • 1
  • 5
  • possible duplicate of http://stackoverflow.com/questions/5672697/java-filling-a-bufferedimage-with-transparent-pixels?rq=1 – SeniorJD Aug 14 '14 at 15:30
  • Thats accutly what I was looking at a lot before I posted this, It didnt help (too) much. – Zambonie Aug 14 '14 at 15:35

3 Answers3

2

Use Color(red, green, blue, alpha) with values 0-255. Where alpha is the opacity.

Buffed image being of type with an Alpha channel (RGBA, BGRA)

Color halfTransparant = new Color(0x76, 0x54, 0x32, 128);

With setRGB on arrays this still is not fast, you might access the raster data. But why using dynamically generated images in time critical situations.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

The smart way is to create your image with correct alpha from the start (using an image format with transparency, e.g. PNG and your favorite imaging application e.g. GIMP).

Otherwise you can directly alter pixels in the BufferedImage returned by ImageIO using the BufferedImage API: setRGB(int ARGB) and its bulk manipulation cousins in the BufferedImage's Raster.

Durandal
  • 19,919
  • 4
  • 36
  • 70
0

One way that should work with all image types (as long as they support alpha), is very fast, and will not disable hardware acceleration of the image is:

BufferedImage image = ...; // From somewhere

Graphics2D g = destination.createGraphics();
try {
    g.setComposite(AlphaComposite.Clear);
    g.fillRect(x, y, w, h); // Area to make transparent
}
finally {
    g.dispose();
}
Harald K
  • 26,314
  • 7
  • 65
  • 111