0

I have an app made with Swing and JFreeChart and I'm exporting a ChartPanel object to an Image (BufferedImage to be precise). I expected the added overlay to be a "part" of the panel, and be exported with the panel, but that is not the case. Everything exports properly apart from the overlay. How do I make it include the overlay or export it into Image itself so I could at least try to slap them together somehow?

Simplified code:

    CrosshairOverlay crosshairOverlay = new CrosshairOverlay();
    //overlay configuration here
    chartPanel.addOverlay(crosshairOverlay);

Code for exporting the panel:

    int width = 450;
    int height = 350;
    JFreeChart chart = chartPanel.getChart();
    BufferedImage image = chart.createBufferedImage(width, height);

For reference, here's a screenshot of CrosshairOverlayDemo1:

CrosshairOverlayDemo1

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I know nothing about JFreeChart or how a CrosshairOverlay works. Also know nothing about the createBufferedImage(...) method. You could trying using the [Screen Image](https://tips4java.wordpress.com/2008/10/13/screen-image/) class. It is a class to create an image of any Swing component. If that doesn't work then JFreeChart must do something strange when painting the overlay, so I would suggest the other option is to use the `Robot.createScreenCapture(...)` method. This method is slower but it takes the pixels right from the screen so it should capture the overlay. – camickr Oct 13 '19 at 18:22
  • The `createBufferedImage()` method renders _just_ the chart. A [*screenshot*](http://meta.stackoverflow.com/questions/99734/how-do-i-create-a-screenshot-to-illustrate-a-post), added above, works. Try either of @camickr's suggestions. – trashgod Oct 14 '19 at 02:59

0 Answers0