Question

Ok so I'm designing a maze related game and I am now handling the GUI.

Considering it will be a NxN dimension, I have to resize the images (content of the labyrinth) according to the screen size, so it will remain untouched regardless of the maze size.

I used this piece of code:

public static BufferedImage resizeImage(Image image, int width, int height) {
       BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
       return bufferedImage;
    }

There is an issue though. The new resized image is now black (completely black) even though it is correctly resized (dimensions wise).

What am I doing wrong? Thanks in advance.

Was it helpful?

Solution

Here's how you can add in the code to draw a scaled version of your image on the new bitmap that you created:

public static BufferedImage resizeImage(Image image, int width, int height) {
    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bufferedImage.createGraphics();

    // Increase quality if needed at the expense of speed
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

    AffineTransform scaleTransform = AffineTransform.getScaleInstance(
            width / (double) image.getWidth(null), height / (double) image.getHeight(null));
    g.drawImage(image, scaleTransform, null);

    // Release resources
    g.dispose();

    return bufferedImage;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top