创建面板图像只会创建面板图像,而不是面板上显示的内容

时间:2012-04-12 18:33:24

标签: java swing

我正在编写一个代码,以便在面板上显示图表的图像文件。为此,我创建了它的缓冲图像,然后使用ImageIO.write()。它可以工作,但它只显示面板(灰色面板),但不显示该面板上的图表。在这种情况下该怎么办?这是我的代码

com.objectplanet.chart.NonFlickerPanel p = 
    new com.objectplanet.chart.NonFlickerPanel(new BorderLayout());
p.add("Center", chart); // this statements adds the chart in the center of the panel
ChartPanel.add("Center", p);
ChartPanel.setSize(500, 200);
ChartPanel.show();
 int w = ChartPanel.getWidth();
int h =  ChartPanel.getHeight();
  BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
  Graphics2D g = bi.createGraphics();
  ChartPanel.paint(g);
  ChartPanel.printAll(g);
  File f = new File("D:\\image.png");
    try {
            // png is an image format (like gif or jpg)
            ImageIO.write(bi, "png", f);
    } catch (IOException ex) {
        ex.printStackTrace();
    }

我解决了这个问题。面对同样问题的人,这就是解决方案

使用paintall函数而不仅仅是绘制函数

0 个答案:

没有答案