0

I'm using JFreeChart inside of a ChartPanel with SWT_AWT bridging. What I want to do is use the SWT to handle the printing of the chart. Is there a way to take the chartpanel and draw it onto the printer GC from SWT?

JFreeChart chart = createXYChart(createDataset(v2, v1));

Composite comp = new Composite(parent, SWT.NONE | SWT.EMBEDDED);
chart.removeLegend();

//ChartComposite c = new ChartComposite(parent, SWT.NONE | SWT.EMBEDDED, chart, true);
Frame frame = SWT_AWT.new_Frame(comp);
ChartPanel panel = new ChartPanel(chart);
panel.setMaximumDrawHeight(10000);
panel.setMinimumDrawHeight(0);
panel.setMaximumDrawWidth(10000);
panel.setMinimumDrawWidth(0);
panel.setPopupMenu(null);

frame.add(panel);


Shell shell  = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
PrintDialog dialog = new PrintDialog(shell);

try {
    //PrinterData printerData = Printer.getDefaultPrinterData();
    PrinterData printerData = dialog.open();
    if (printerData != null) {
        Printer printer = new Printer(printerData);
        if (printer.startJob("Text")) {
            GC gc = new GC(printer);

            if (printer.startPage()) {
                gc.drawString("HDSDFSDFSDFSDFDS", 0, 0);
                //WANT TO PRINT CHART SOMEHOW HERE
                printer.endPage();
            }
            gc.dispose();
            printer.endJob();
        }
        printer.dispose();
        System.out.println("Print job done.");
    }
} catch (Exception exception) {
    exception.printStackTrace();
}
4

1 回答 1

1

我从未使用过SWT_AWT,但我将 JFreeChart 与 SWT 一起使用(使用ChartComposite),我可以使用下面的代码将图表捕获到图像中。它可以帮助你:

ChartComposite chartComposite = ...;
Image image = new Image(chartComposite.getDisplay(), chartComposite.getBounds());
GC gc = new GC(image);
chartComposite.print(gc);
gc.dispose();
于 2012-06-01T06:59:46.570 回答