Search code examples
java2d-games

Loading an image for a game in java


I want to make a 2-D game in Java.It's a very simple one. The problem is that I get a strange rectangle over my hero, because I don't know how to ignore the white space within the picture I have drawn my self.

/// loading the image///
private ImageIcon x= new ImageIcon("path");
private Image hero=x.getImage();

//=============

//=========drowing====


public void paint(Graphics g){

     Graphics2D g2d=(Graphics2D)g;
     g2d.drawImage(background,0,0,null);
     g2d.drawImage(hero,100,100,null);

}

Solution

  • This doesn't really have anything to do with Java. Like @WOUNDEDStevenJones already mentioned, you simply need to save the image as a PNG file with a transparent background.

    There are plenty of ways to do this, and google is your friend. I personally use GIMP, and here is a related question that explains how to do this: https://graphicdesign.stackexchange.com/questions/5446/making-the-background-of-an-image-transparent-in-gimp

    And here is the direct GIMP tutorial: http://docs.gimp.org/en/gimp-using-web-transparency.html