我有一个类可以扩展一个面板,在上面我可以画出一些线条
((Graphics2D) getGraphics).drawLine(x, y, x, y);
线条在面板上正确绘制,但是当我运行以下代码以保存图像(我在互联网上找到)时,我保存的面板没有线条
/**
* Save the Panel as image with the name and the type in parameters
*
* @param name name of the file
* @param type type of the file
*/
public void saveImage(String name,String type) {
BufferedImage image = new BufferedImage(getWidth(),getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
paint(g2);
try{
ImageIO.write(image, type, new File(name+"."+type));
} catch (Exception e) {
e.printStackTrace();
}
}