Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:将画布保存到图像文件会生成一个空白图像_Java_Awt_Bufferedimage - Fatal编程技术网

Java:将画布保存到图像文件会生成一个空白图像

Java:将画布保存到图像文件会生成一个空白图像,java,awt,bufferedimage,Java,Awt,Bufferedimage,我有一个类扩展了Canvas,并实现了以下方法。问题是,每当我调用exportImage,我得到的都是一张空白的白色图像。图像上应该有图纸 /** * Paint the graphics */ public void paint(Graphics g) { rows = sim.sp.getRows(); columns = sim.sp.getColumns(); createBufferStrategy(1); // Use a bufferstra

我有一个类扩展了
Canvas
,并实现了以下方法。问题是,每当我调用
exportImage
,我得到的都是一张空白的白色图像。图像上应该有图纸

/**
  * Paint the graphics
  */
public void paint(Graphics g) {
    rows = sim.sp.getRows();
    columns = sim.sp.getColumns();
    createBufferStrategy(1);
    // Use a bufferstrategy to remove that annoying flickering of the display
    // when rendering
    bf = getBufferStrategy();
    g = null;
    try{
        g = bf.getDrawGraphics();
        render(g);
    } finally {
        g.dispose();
    }
    bf.show();
    Toolkit.getDefaultToolkit().sync();    
}

/**
 * Render the cells in the frame with a neat border around each cell
 * @param g
 */
private Graphics render(Graphics g) {
    // Paint the simulation onto the graphics...

}

/**
  * Export the the display area to a file
  * @param imageName the image to save the file to
  */
public void exportImage(String imageName) {
    BufferedImage image = new  BufferedImage(getWidth(), getHeight(),BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = image.createGraphics();
    paintAll(graphics);
    graphics.dispose();
    try {
        System.out.println("Exporting image: "+imageName);
        FileOutputStream out = new FileOutputStream(imageName);
        ImageIO.write(image, "png", out);
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }    
}

我简化了你的
paint
方法,使之与下面的方法一样简单,导出工作正常。但是,我建议您重写
paintComponent
,而不是
paint

public void paint(Graphics g) {
    g.setColor(Color.BLUE);
    g.drawRect(10, 10, getWidth() - 20, getHeight() - 20);
}

尝试使用
paint
方法,而不是
paintAll

public void exportImage(String imageName) {
    BufferedImage image = new  BufferedImage(getWidth(), getHeight(),BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = image.createGraphics();
    paint(graphics);
    graphics.dispose();
    try {
        System.out.println("Exporting image: "+imageName);
        FileOutputStream out = new FileOutputStream(imageName);
        ImageIO.write(image, "png", out);
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }    

}

我在
exportImage
方法中使用了
render
而不是
paint
,将图形打印到图像中。似乎我使用的
缓冲策略有问题。

要更快地获得更好的帮助,请发布一条。当我使用
绘制
时,保存的图像完全是黑色的,而不是画布中的图像。