0

I am creating a 2D platformer game and am trying to scale an image with a custom library I created. The code gives a NullPointerException when invoking dbi.getGraphics().

public void scale(int width, int height) {

        JFrame tempFrame = new JFrame();
        tempFrame.setSize(source.getWidth(null), source.getHeight(null));
        Image dbi = tempFrame.createImage(source.getWidth(null), source.getHeight(null));
        Graphics dbg = dbi.getGraphics(); // NullPointerException
        dbg.drawImage(source, 0, 0, width, height, null);
        source = dbi;

        //BufferedImage temp = (BufferedImage) source;
        //temp.getScaledInstance(width, height, Image.SCALE_DEFAULT);
        //source = temp;

}

I am using dbi.drawImage() to scale the image. I have tried source.getGraphics().drawImage(source,0,0,width,height,null);, but it doesn't work.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Galen Nare
  • 376
  • 2
  • 4
  • 18
  • Is dbi null, or is the NullPointerException being thrown from within the call to getGraphics()? Posting a few lines of the stack trace would help here. – joev Nov 12 '14 at 13:28
  • @Galen Nare why not you use the getScaledInstance(width, height, hint) method of class Image for Scaling your images?? – Muhammad Nov 12 '14 at 13:56
  • 1
    @Muhammad: Some perils are cited [here](http://stackoverflow.com/a/20892645/230513). – trashgod Nov 12 '14 at 17:27
  • @trashgod I have voted both of your answers because it worth it :) – Muhammad Nov 12 '14 at 19:04
  • @Muhammad because it causes a ClassCastException because a ToolkitImage is being used somewhere and I don't know where. I never create a ToolkitImage... – Galen Nare Nov 12 '14 at 20:19
  • If you're trying to create a platformer, the built-in Java Graphics libraries are a very poor choice. Have a look at libGDX. It comes with all manner of support for physics, lighting, particle effects, animations, and all the rest. Furthermore, you can use openGL to implement custom shader programs and all manner of low level graphics tinkering. – Scuba Steve Nov 12 '14 at 20:39
  • @ScubaSteve I don't really have a choice now, I've already made a lot of progress with what I have. It's a little too late. – Galen Nare Nov 13 '14 at 22:34
  • If it's for any kind of commercial release, I think you're crazy. Just on the record there. But good luck :D – Scuba Steve Nov 15 '14 at 04:13

2 Answers2

3

The frame has not been made visible at this point in your program; getGraphics() is invalid. Instead use BufferedImage#createGraphics(), as shown here. Also consider AffineTransformOp, seen here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

I found an answer to my own question that works.

int imageWidth  = source.getWidth();
        int imageHeight = source.getHeight();

        double scaleX = (double)width/imageWidth;
        double scaleY = (double)height/imageHeight;
        AffineTransform scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
        AffineTransformOp bilinearScaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR);

        source = bilinearScaleOp.filter(
            source,
            new BufferedImage(width, height, source.getType()));

I found it on another question from a few years ago.

How to improve the performance of g.drawImage() method for resizing images

Community
  • 1
  • 1
Galen Nare
  • 376
  • 2
  • 4
  • 18