简体   繁体   中英

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);

}

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

You can use for example photoshop or gimp(free), where you can apply layers. You can do a transparent layer and put your hero to this layer. If your picture with hero has a background color, then you can select (in mentioned programs) only your hero, without background, and after selection you can delete the background. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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