2011-10-14 60 views
0

我在java代码中有几个类和函数。 其中一个功能是使用图形对象绘制图形。 我要救这个图jpg格式,所以我所做的:将图形保存到缓冲图像

public void paintComponent(Graphics graphics) { 
Graphics g1=null; 
double scale = ((double) ySize)/histogram.getMaxFrequency(band); 
if (histColor != null) 
    graphics.setColor(histColor); 
for (int x = 0; x < 256; ++x) { 
    int y = (int) Math.round(scale*histogram.getFrequency(band, x)); 
    if (y > 0) 
    graphics.drawLine(x+xOrigin, yOrigin, x+xOrigin, yOrigin-y); 
} 
FileOutputStream fileopstream; 
try { 
    fileopstream = new FileOutputStream("C:\\Users\\spider\\Desktop\\Study\\Computer Vision\\programs\\histogram1.jpg"); 
JPEGImageEncoder output=JPEGCodec.createJPEGEncoder(fileopstream); 
BufferedImage image= new BufferedImage(100,100,BufferedImage.TYPE_BYTE_BINARY); 
//WritableRaster raster=image.getRaster(); 
g1=image.createGraphics(); 
g1=graphics.create(); 
//g1.dispose(); 
//graphics.drawImage(image, 0, 0, 100, 100, null, null); 
//output.encode(image); 
// Save as JPEG 
File file = new File("newimage.jpg"); 
ImageIO.write(image, "jpg", file); 
} catch (FileNotFoundException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (ImageFormatException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
drawAxis(graphics, xOrigin, yOrigin+1); 

}

,但它无法正常工作。存储的图像是空白图像,而不是我可以在屏幕上看到的图形。 你能请帮助我

非常感谢

回答