BufferedImage Generating a Black Background

Ok, so I am making a game, and I am trying to change the original marker image by adding text to it, and I use the following code:

import javax.swing.ImageIcon; import javax.swing.Timer; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; public class HitMarker { public static final Image rangeHitMarker = new ImageIcon(HitMarker.class.getResource("rangeHitMarker.png")).getImage(); public static final Image magicHitMarker = new ImageIcon(HitMarker.class.getResource("magicHitMarker.png")).getImage(); public static final Image monsterHitMarker = new ImageIcon(HitMarker.class.getResource("monsterHitMarker.png")).getImage(); public static final Font font = new Font("Tahoma", Font.PLAIN, 10); public static final Color t = new Color(0,0,0,0); public Image hitMarker; public BufferedImage image; public String hit; public int attackStyle; public boolean rangeAttack; public int x; public int y; public Timer timer; public boolean remove; public HitMarker(int x, int y, int hit, int attackStyle){ this.hit = String.format("%d", hit); this.remove = false; this.x = x; this.y = y; this.attackStyle = attackStyle; this.hitMarker = getImage(); BufferedImage bi = new BufferedImage(35, 20, BufferedImage.TYPE_INT_RGB); Graphics2D g = bi.createGraphics(); g.drawImage(hitMarker, 0, 0, null); g.setFont(font); g.setColor(Color.WHITE); g.drawString(this.hit, 18, 13); g.dispose(); image = bi; timer = new Timer(800, new ActionListener(){ public void actionPerformed(ActionEvent e){ remove = true; timer.stop(); } } ); timer.setInitialDelay(800); timer.start(); } public HitMarker(int x, int y, int hit){ this.hit = String.format("%d", hit); this.remove = false; this.x = x; this.y = y; this.hitMarker = monsterHitMarker; BufferedImage bi = new BufferedImage(35, 20, BufferedImage.TYPE_INT_RGB); Graphics2D g = bi.createGraphics(); g.drawImage(hitMarker, 0, 0, null); g.setFont(font); g.setColor(Color.WHITE); g.drawString(this.hit, 18, 13); g.dispose(); image = bi; timer = new Timer(800, new ActionListener(){ public void actionPerformed(ActionEvent e){ remove = true; timer.stop(); } } ); timer.setInitialDelay(800); timer.start(); } public boolean isRangeAttack(){ return attackStyle == AttackStyleConstants.RANGE || attackStyle == AttackStyleConstants.RANGE_DEFENCE ? true : false; } public Image getImage(){ return isRangeAttack() ? rangeHitMarker : magicHitMarker; } } 

Particular attention is paid either to the constructor: And the error I am experiencing is that when I create a BufferedImage and draw an image on a buffered image, it automatically creates a black background, and I do not know why. I tried to research this topic, and some say that something needs to be changed regarding the AlphaComposite method and g.clearRect (), but none of them work. By the way, the image that I draw on the buffered image is 35x20 (this is the size of the buffered image) and has a transparent background. If someone tells me how to remove this black background, that would be very appreciated, thanks.

+7
source share
4 answers

Try BufferedImage.TYPE_INT_ARGB . This will make the areas transparent instead of black.

+16
source

You might want to try and save the alpha channel,

  BufferedImage bi = new BufferedImage(35, 20, BufferedImage.TYPE_INT_ARGB); 
+4
source

If you need a JPG with a white background, you need to draw an image like this:

 g.drawImage(hitMarker, 0, 0, Color.WHITE, null); 

This way you avoid the black background when switching from PNG to JPG .

+1
source

Use png instead of jpeg. Png is very suitable for transparency operations. Here is a simple piece of png export code,

  BufferedImage bImage = new BufferedImage(640, 480, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = (Graphics2D) bImage.getGraphics(); DrawingContext context = new DrawingContext(g2d); plot.draw(context); ByteArrayOutputStream baos = new ByteArrayOutputStream(); DrawableWriter wr = DrawableWriterFactory.getInstance().get("image/png"); wr.write(plot, baos, 640, 480); baos.flush(); baos.close(); InputStream inputStream = new ByteArrayInputStream(baos.toByteArray()); BufferedImage bufferedImage = ImageIO.read(inputStream); ImageIO.write(bufferedImage,"png",new File(outputFolder.getPath()+"/result.png")); 
0
source

All Articles