Hello Java Gaming Community.
I heard from Sun that this is the place to post about Java2D performance questions.
I am working on the Java Instructional Gaming Engine. We are trying to speed up the performance on our spritesheet loading code. Basically we have an XML file and Image pair that are loaded at the beginning of games. Each animation has to copy out all of the frames out of the main image.
From my testing I have found that the larger the main image is, the longer it takes to load. While the obvious solution might be just to not use big images, it is bound to happen when students are using the engine.
I have extracted essentially what we are doing into the code below. We are doing a little more than this with setting frame properties etc. but this is basically it.
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
public class LoadTest {
Frame frame;
URL sheetURL;
BufferedImage sheetImage;
public static void main(String[] args) {
//System.setProperty("sun.java2d.opengl", "True");
new LoadTest();
}
public LoadTest()
{
long st = System.nanoTime();
frame = new Frame();
sheetURL = ClassLoader.getSystemResource("bugs.png");
try {
sheetImage = ImageIO.read(new BufferedInputStream(sheetURL
.openStream()));
} catch (IOException e) {
sheetImage = null;
System.err.println("IO Exception while loading image '"
+ sheetURL.toString() + "'");
return;
}
for(int i=0; i<10; ++i)
load(i);
long t = ((System.nanoTime()-st)/(1000000));
System.out.println("Total Load Test took " + t + " ms");
}
public boolean load(int i)
{
int width = 20;
int height = 20;
int top = i * height;
int left = 0;
int rows = 1;
int columns = 10;
long st = System.nanoTime();
BufferedImage[] bframes = new BufferedImage[rows * columns];
for (int y = 0; y < rows; ++y) {
for (int x = 0; x < columns; ++x) {
bframes[y * (columns) + x] = createImageResource(
sheetImage, Transparency.BITMASK, width,
height, left + width * x, top + height * y);
}
}
long t = ((System.nanoTime()-st)/(1000000));
System.out.println("Created Image List with " + (rows*columns) + " elements in " +
t + " ms for resource ");
return false;
}
protected BufferedImage createImageResource(final BufferedImage originalImg,
final int transparency, final int w, final int h,
final int xoffset, final int yoffset) {
BufferedImage r = drawImage(originalImg, transparency, w, h, -xoffset,
-yoffset);
return r;
}
protected BufferedImage drawImage(final Image i, final int transparency, final int w, final int h,
final int xoffset, final int yoffset)
{
GraphicsConfiguration gc = frame.getGraphicsConfiguration();
BufferedImage image = gc.createCompatibleImage(w, h, transparency);
image.getGraphics().drawImage(i, xoffset, yoffset, null);
return image;
}
}
I have attached the images to this post. Here are my run times for each.
bugs.png - Total Load Test took 1206 ms
bugs_large.png Total Load Test took 17638 ms
Do you guys have any suggestions on how we could speed this up?
Thanks,