The Coder's Handbook
Images
IMAGES
For more detailed information, look at the Image and Graphics class in the Slick 2D Javadoc
Importing an Image to Eclipse
Create a folder inside your project called res
This is short for resources
At the same level as src and libs
Copy an image to this folder
You'll find your life is easier if you always use the same file format
Mr. M recommends using .png
Creating and Using an Image
You'll need to create an Image object then draw that image using Graphics
Image capybara;
public void init()
{
capybara = new Image("res/capybara.png");
g.drawImage(capybara, 200, 130);
}
Scaling an Image
You can resize Images in Slick2D
Keep in mind that with big images this can be a costly operation.
You don't want to scale images every frame, but rather once when you load them
For example, you might use this in the init() method or an object's constructor
splash = new Image("res/splash.png");
splashScaled = splash.getScaledCopy(800, 600);
EFFICIENCY AND OBJECTS
Hard Drive vs. RAM
Loading an image file is very slow - you're taking a file from your computer's hard disk (or flash drive) and putting it into RAM. We want to avoid doing it every time an object is rendered or created - ideally, we load our images once at the start of the game.
An Example
In this example, imagine we have two classes: a game state called Game and a Capybara class.
Class Game
public void init()
{
Capybara.init();
}
Note that unlike our usual update and render methods, we're making the Capybara's init method static. This is because all Capybaras share an image.
Make sure you reference it in a static way in Game, as shown above.
Class Capybara
private static Image capy;
public static void init()
{
capy = new Image("res/capy.png");
}
public void render(Graphics g)
{
g.drawImage(capy, x, y);
}
RESOURCES
The New Boston - Images