简体   繁体   中英

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();
}

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.

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