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.