1

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.

GRoutar
  • 1,311
  • 1
  • 15
  • 38
  • u may possibily [try instead](http://docs.oracle.com/javase/7/docs/api/java/awt/Image.html#getScaledInstance%28int,%20int,%20int%29), [answered here](http://stackoverflow.com/a/7293400/2575725) –  Apr 19 '14 at 03:43
  • 2
    You've only created a new empty BufferedImage - there's no code to copy and scale the old image. – Erwin Bolwidt Apr 19 '14 at 04:05
  • @Arvind See [The Perils of Image.getScaledInstance()](https://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html). Also, is it so hard to type **all 3** letters of the word 'you'? – Andrew Thompson Apr 19 '14 at 04:11
  • @ErwinBolwidt makes a good point. OTOH I would probably just scale the image when drawing it, using an affine transform (to resize it) & some rendering hints (to make it look pretty) on the `Graphics2D` object. – Andrew Thompson Apr 19 '14 at 05:54
  • BTW - *"..resize the images.."* Do these images contain transparency? If so, `BufferedImage.TYPE_INT_RGB` should be `BufferedImage.TYPE_INT_ARGB`. – Andrew Thompson Apr 19 '14 at 09:12

1 Answers1

1

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;
}
Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
  • 1
    Nice! +1 Actually my comment to the question, which was sitting on the screen for s long time before I submitted it, almost seems redundant now - except for the idea of resizing it when painting.. BTW - `g.drawImage(image, scaleTransform, null);` should better be `g.drawImage(image, scaleTransform, null); g.dispose();`.. – Andrew Thompson Apr 19 '14 at 05:57
  • Nice edit. :) You make me wish I could up-vote again. I asked the OP a question about transparency, and realized a more robust approach would be to use the `BufferedImage.TYPE_..` of the original (necessarily buffered) image. – Andrew Thompson Apr 19 '14 at 09:16
  • It worked. but the background is black. Can you tell me how to avoid it? To clear my question, the background should be transparent but it showed black. Help please – AdolfJames Urian Dec 03 '20 at 10:00
  • `BufferedImage.TYPE_INT_RGB` doesn't have an alpha channel, so it doesn't have transparency. Use `BufferedImage.TYPE_INT_ARGB` to add an alpha channel and support transparency. – Erwin Bolwidt Dec 03 '20 at 12:30