making a new resized image at runtime

Before now I have had a scale for each image loaded that would scale the coordinate grid in the render method, but it occurred to me that this could be a drain of cpu power, so I was wondering how to save a new resized version of a particular image at runtime. TY for any help!

Scaling is free if you’re using OpenGL (which you should be if you’re making a game). :slight_smile:

If you’re using Java2D tough, you need to do some Graphics2D / Graphics operations:


// Takes some time! don't do this every frame!
public static BufferedImage createScaledVersion(int scalex, int scaley, BufferedImage source) {
    BufferedImage destination = new BufferedImage(source.getWidth() * scalex, source.getHeight() * scaley, BufferedImage.TYPE_INT_ARGB);
    Graphics g = destination.getGraphics(); // <- might be called getDrawGraphics() or somehow...
    g.drawImage(source, 0, 0, destination.getWidth(), destination.getHeight(), null);
    g.dispose();
    return destination;
}

Not sure if it works or even compiles. But it should give you an idea! :slight_smile:

still agree with davedes's advice to use OpenGL tough... He created some awesome tutorials to get you started, if you haven't already: https://github.com/mattdesl/lwjgl-basics/wiki He uses LWJGL, a wrapper for OpenGL.