问题java:不要将面板保存在图像中

时间:2019-03-13 17:09:11

标签: java

我有一个类可以扩展一个面板,在上面我可以画出一些线条

((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();
    }
}

0 个答案:

没有答案