有我的代码。我尝试分析数据并创建图表并保存到本地驱动程序。该程序现在运行良好。但是我发现jpg文件只能在程序全部完成后保存到本地驱动器。但是,数据太多,需要创建的图表太多。整个程序可能会运行数小时。我只是想知道我可以让它实时保存jpg吗?所以我可以查看它刚刚创建的所有图表。或者,如果我需要在程序完成之前停止程序,我不必放弃所有图表。
谢谢
protected void paintComponent(Graphics g) {
System.out.println("go");
super.paintComponent(g);
BufferedImage jpg = new BufferedImage(1040, 400,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = jpg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
double total=0;
// Draw ordinate.
g2.draw(new Line2D.Double(SPACE, SPACE, SPACE, h-SPACE));
// Draw abcissa.
g2.draw(new Line2D.Double(SPACE, h/2, w-SPACE, h/2));
double xInc = (double)(w - 2*SPACE)/(data.size()-1);
double scale = (double)(h/2 - SPACE*3)/getMax();
// Mark data points.
g2.setPaint(Color.red);
for(int i = 0; i < data.size(); i++) {
double x = SPACE + i*xInc;
double y;
if(data.get(i)!=0){
y= h/2 - SPACE - scale*data.get(i);
total = total + data.get(i);
}else{
y = h/2;
}
g2.fill(new Ellipse2D.Double(x, y-1, 4, 4));
}
try {
ImageIO.write(jpg, "JPEG", new File("111\\"+total/data.size() + ".jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}