简体   繁体   中英

How do you insert your own graphics in Java 7

I am working on a game but I want to put my own graphics into Java. I am using Eclipse and Java 7. I was wondering of a library that can take care of that and help you use your own graphics.

Edit: What my goal is, is to draw an image, then put in into the program as an object.

Taking and manipulating existing images can be done with the Java Image class.

First, declare an instance of the class ( BufferedImage is a subclass of Image ):

private BufferedImage img = null;

Then, you load the image like this:

try
{
    img = ImageIO.read( new File("MyPicture.jpg" ));
}
catch ( IOException exc )
{
    //TODO: Handle exception.
}

Finally, in the paint(Graphics) method of your code, you display the image like this:

g.drawImage( img, x, y, this );

Don't forget to write some code to handle the Exception. These things help you find and solve bugs more easily.

The official tutorial is here .

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