Scalling things up!

Pre-note: I have looked for this topic before, and have not found one matching my needs, so please keep that in mind if there is a similar thread. Also, execute any punctuation issues, I am typing this on my phone.

Hello,

I am looking to scale up my java game. Currently, my game is 800 by 800 pixels, and drawn by using the graphics object (and by invoking the drawImage method).

I am looking for a really easy way to upscale my images, please note that I am drawing many different images at the same time.

I have tried upscaling my self, but it hasn’t worked. I troed upscaling each thing drawn by the amount of pixels above the original 800, but that ended up with some weird rendering issues. Is there a way to create a buffered images, and add other Images to that? If so, then I could simply use the two integer spots in the method to expand the image through the screen hight and width, witch are both 800 pixels.

A second note: I want to upscale the images, not change the aspect ratio. So keep in mind I don’t want to expand the camera range etc.

Could you guide me with upscaling, particularly by using Java’s Swing?

Thanks!

  • Jaks

If your paint() looked like this:


public void paint(Graphics g) {
    g.drawStuff();
}

Change it to this:


BufferedImage buffer = new BufferedImage(800, 800, /* whatever you need */);

public void paint(Graphics g) {
    Graphics bg = buffer.getGraphics();
    bg.drawStuff();
    bg.dispose();

    Graphics2D gg = (Graphics2D) g;
    gg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); // alternatively a different interpolation type, experiment with it

    gg.drawImage(buffer, 0, 0, getWidth(), getHeight(), null); // stretch buffer to fit (getWidth(), getHeight())
}

This method simply renders everything to a buffer, and then re-sizes the whole buffer on render. Yes, it incurs the penalty of an extra draw, but it shouldn’t be too bad.
Since this is manual double buffering as well, you can setDoubleBuffering(false); on whatever it is you are rendering to, don’t need to waste more render time.

Thank you!

I have a quick question though: What is the point of disposing the buffer graphics, and what’s the point of changing the rendering hint. Also why do you make a new graphics just toto draw, could we of used the original g?

You have to create the new graphics or else you aren’t drawing to the buffer, and the rendering hint isn’t strictly required, but it changes the image scaling algorithm used, the default is Nearest Neighbor, which can look quite awful in most situations. Again, experiment with the three different interpolation values to see what works best.

EDIT: we dispose the bg because we created it, and you should always dispose resources you acquired yourself, it’s done each frame because I find that you can get weird issues with out of sync buffers if you try to cache the buffer’s graphics between frames. It’s not too expensive to my knowledge, so just getGraphics() and dispose() each frame.