# JAVA: How do I use BufferedImage to properly render a PNG with Transparency?

I've been around the internet for about 5 hours and found no solution to my issue, so hopefully someone will help me. So' I've been trying to make a simple game. In previous oportunities I've used JLabel to render my sprites (Player, enemies, misc) but that takes a lot of single files just to make simple animations, so I decided to use spritesheets, which have served me right on C++ (SFML) before. Tried to use JLabels also to achieve this, but with no success, aparently there's no way to select a single tile on the sheet properly, BUT transparency works just fine. Searching aroud the web I found out that there's Graphics2D API, so I decided to give it a shot, using BufferedImage (as many have recommended), but this is where it all went to hell.

This is my actual sheet:

You can see it is actually transparent, I used BufferedImage and getSubimage(...) to render it on screen, but all I get is this:

This is my code:

public class GameScreen extends Screen
{
private JFrame gameFrame;
private BufferedImage background;
private BufferedImage charSprite = null;
private BufferedImage enemySprite;
private BufferedImage spellSprite;
private Character character;
private Enemy enemy;

public GameScreen(JFrame mainFrame)
{
initComponents();
this.setBounds(0, 0, 500, 500);
this.setVisible(true);
gameFrame = mainFrame;
}

@Override
public void refresh()
{

}

@Override
public void paint(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(background, 0, 0, null);
g.fillRect(0, 0, 320, 320);
g2d.drawImage(charSprite.getSubimage(0, 0, 60, 60), 60, 60, null);
}

public void initComponents()
{
try
{

URL url = this.getClass().getResource("/images/char_sheet.png");
url = this.getClass().getResource("/images/arena.png");
url = this.getClass().getResource("/images/enemy.png");
url = this.getClass().getResource("/images/projectile.png");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(gameFrame, "Fatal Error\n" + e.toString() + "\n" + e.getLocalizedMessage(),
"Warning", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}


}

It's a simple class, that extends an interface which (for now) only has the refresh() method, I decided to have the rendering and updating in that single class, for this is just a practice. Can you spot what I'm doing wrong? Or have ane advice or tips? It would be highly appreciated, I've been freaking about about this for the last minutes. Thanks in advance.

EDIT: Forgot to mention something, the screen class implements JPanel, to work as a Canvas.

• I would consider dropping AWT/Swing for game design; it's ancient and is known for using 100% cpu when running your game. You need to be learning LWJGL, or forget about Java completely and use C# and OpenTK. – Krythic Apr 9 '17 at 4:40
• Inevitably, your next question is going to be "How do I scale this to fit my game window." You will learn very quickly that you can't, or at least not with enough performance for games. And after you realize that you WILL be switching over to LWJGL. Just trying to save you a headache. – Krythic Apr 9 '17 at 4:43

Very strangely, it appears that your picture is not transparent.

I copied your code, and tried it with your image, and the background was white. Then I tried it with a png I found on my computer and it did work. So I put your picture into photoshop, and there it was – a white background.

Here's a transparent version if you need :

• But I used the same image to render it on a JLabel and it did rendered with transparent background. That's odd... – Andres Rincon Jul 10 '16 at 21:44
• Yeah, very weird; must be some sort of different type of png. What program did you use to save your sprites? – KingDolphin Jul 10 '16 at 23:39
• GraphicsGale. Used it before to make sprites with transparency and worked everytime, first time using BufferedImage tho. I checked on Krita and the transparency is also there. – Andres Rincon Jul 11 '16 at 2:08
• Just fixed the issue. As I said, I opened the images on Krita and they had the transparency, but I had to resave them. Maybe GraphicsGale doesn't output the right type of PNG, which is weird, because I can see it transparent on my PC, on imgur, and pretty much everywhere. Thanks for the idea mate! – Andres Rincon Jul 11 '16 at 2:52

You need to call the method setOpaque(false), because by default a JPanel is not set transparent, it is set to opaque=true and thats the problem. I just solved mine with this method call.

JPanel panel = new JPanel();
//now if you want your panel to be transparent you just call this method
panel.setOpaque(false); //be sure to check if it's a png file else it won't work no matter what u do in your code