0

I am writing a code to make an image file of a chart appearing on a panel. For that purpose I create the buffered image of that and then use ImageIO.write(). It works but it only displays the panel(grey coloured panel) but does not show the chart present on that panel. What to do in this case?? Here is my code

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

Well i solved the problem .Anyone facing the same problem ,here is the solution

Use paintall function rather than just paint function

Xara
  • 8,178
  • 15
  • 50
  • 79
  • `NonFlickerPanel` Are you able to paint a `JComponent` such as a `JTree` to an image? If not, perhaps you should post an [SSCCE](http://sscce.org/) of your best effort. But you might start with [ComponentImageCapture](http://stackoverflow.com/a/5853992/418556) as an example that works for simple components. – Andrew Thompson Apr 12 '12 at 20:25
  • It appears to me that you don't have nearly enough information to allow us to be able to answer your question. I *think* that you'd need to use the Graphics object from your BufferedImage and pass it into the JPanel's `paint(...)` method and not `paintComponent(...)` so as to draw the JPanel's children and border to the BufferedImage. Are you doing this? – Hovercraft Full Of Eels Apr 12 '12 at 22:09
  • @HovercraftFullOfEels I am creating a Graphics object but i just get an image of a panel , not the image displayed on that panel :( – Xara Apr 13 '12 at 05:08

0 Answers0