user1946564
user1946564

Reputation: 47

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.

Upvotes: 1

Views: 7000

Answers (1)

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.

Upvotes: 3

Related Questions