问题描述:

I'm trying to create GRAL graphs and export them as JPGs.

Not sure why, but the color of the 'Inset' areas (outside the border) seems to be defaulting to black. This is particularly confusing, as the docs seem to indicate that insets default to being white.

How can I change the color of the Inset?

Here is how I'm exporting the jpg

private void getJpg(BarPlot plot) {

BufferedImage bImage = new BufferedImage(800, 600, BufferedImage.TYPE_INT_ARGB);

Graphics2D g2d = (Graphics2D) bImage.getGraphics();

DrawingContext context = new DrawingContext(g2d);

plot.draw(context);

DrawableWriter wr = DrawableWriterFactory.getInstance().get("image/jpeg");

try {

wr.write(plot, new FileOutputStream("/path/out.jpg"), 800, 600);

} catch (IOException err) {

System.out.println(err);

}

}

Here is my current result (using white text on black bg for now, but want black text on white bg). current output

网友答案:

I figured it out! (with the help of a co-worker)

The Inset area defaults to being transparent black (RGBA(0,0,0,0)). Because of this, when the graph is exported as JPEG (which does not support transparency) it drops the alpha value and renders it all as black. By exporting as a png, I was able to preserve transparency and the graphs now look good when embedded into a document with a white background.

private static void getPng(XYPlot plot, String fileName) {
    BufferedImage bImage = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = (Graphics2D) bImage.getGraphics();
    DrawingContext context = new DrawingContext(g2d);
    plot.draw(context);
    DrawableWriter wr = DrawableWriterFactory.getInstance().get("image/png");
    try {
        wr.write(plot, new FileOutputStream(fileName + ".png"), 800, 600);
    } catch (IOException err) {
        System.out.println(err);
    }
}
相关阅读:
Top