gpt4 book ai didi

java - BufferedImage 的 getGraphics() 中出现空指针异常?

转载 作者:行者123 更新时间:2023-11-30 05:03:28 29 4
gpt4 key购买 nike

public void saveImage(String path){
BufferedImage image = (BufferedImage) createImage(500, 500);
Graphics gImage = image.getGraphics(); //<<<<<<<<--- exception
paint(gImage);
image = image.getSubimage(0,0,500,500);
try {
ImageIO.write(image, "png", new File(path+".png"));
}
catch (Exception e){}
}

问题出在哪里?

最佳答案

显然,方法 createImage(int, int) 返回 null。原因在documentation中有解释。 :

Returns:
an off-screen drawable image, which can be used for double buffering. The return value may be null if the component is not displayable. This will always happen if GraphicsEnvironment.isHeadless() returns true.

关于java - BufferedImage 的 getGraphics() 中出现空指针异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5893386/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com