javacanvasbufferbufferstrategy

Better performance for Java Canvas Buffered images


I am using Java Canvas and have a draw method, which is called about 20 times a second. It iterates through an array of images and draws a scaledInstance of them depending on window size:

g.drawImage(img.getScaledInstance(
                (short)Math.round(rect.width * yVector),
                (short)Math.round(rect.height * yVector),
                Image.SCALE_SMOOTH),
        (short)Math.round(rect.x * xVector),
        (short)Math.round(rect.y * yVector), null);

Performance is really bad. I am using a buffer strategy and this is how the images, which are BufferedImage, are declared:

try {
    image = ImageIO.read(new File("resources/drawable/" + src));
    img = (BufferedImage)image.getImage();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Solution

  • The performance issues came from getScaledInstance. It scaled the image 20 times/second, which is not performant. Our application has now fixed positions and sizes, but runs with a very stable, high fps rate.