Using Graphics2D for drawing to PGraphics rendering context
in
Contributed Library Questions
•
1 year ago
Hello Processing Forum,
I have been experimenting for about a week with Processing for Information Visualization and I'm very impressed with the results after a few evening hours.
My aim is to combine a 2D or even 3D warehouse visualization (very individual) with small charts (very standard like bar charts, line charts, sparklines) that get the dataset from a JDBC database. Now I'm stuck with integrating the output from JFreeChart into the 3D (OPENGL) context.
I've already managed to integrate a chart as BufferedImage like in this code snippet:
Ok - I could generate a 5000 x 5000 px image and then scale it down.... hmmm...
So I'm wondering: Why at all transforming the JAVA2D objects into a BufferedImage instead of drawing directly into the graphics/rendering context of Processing? With perfect quality as vector graphic. How could I do that?
At the moment I'm experimenting with JAVA2D objects and Batik/SVG. Please see the extended code here:
Thx a lot!
I have been experimenting for about a week with Processing for Information Visualization and I'm very impressed with the results after a few evening hours.
My aim is to combine a 2D or even 3D warehouse visualization (very individual) with small charts (very standard like bar charts, line charts, sparklines) that get the dataset from a JDBC database. Now I'm stuck with integrating the output from JFreeChart into the 3D (OPENGL) context.
I've already managed to integrate a chart as BufferedImage like in this code snippet:
- //Add jcommon-1.0.17.jar and jfreechart-1.0.14.jar to files (folder 'code')
- JFreeChart chart;
PImage chartImage;
void setup(){
size(300, 300);
// create a dataset the chart is generated from
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("Processing", new Integer(75));
dataset.setValue("Other", new Integer(25));
// create the chart from dataset and adjust visualization
chart = ChartFactory.createPieChart(null, dataset, false, true, false);
chart.setBorderVisible(false);
chart.setBackgroundPaint(java.awt.Color.white) ;
chart.removeLegend();
chart.setAntiAlias(true);
chart.setBorderPaint(java.awt.Color.white);
PiePlot pp = (PiePlot) chart.getPlot();
pp.setShadowXOffset(0);
pp.setShadowYOffset(0);
pp.setLabelLinksVisible(false);
pp.setLabelGenerator(null);
pp.setBackgroundAlpha(0);
pp.setOutlineVisible(false);
// create image from chart
chartImage = new PImage(chart.createBufferedImage(300,300));
}
void draw(){
// display chart image on screen
image(chartImage, 0, 0);
}
Ok - I could generate a 5000 x 5000 px image and then scale it down.... hmmm...
So I'm wondering: Why at all transforming the JAVA2D objects into a BufferedImage instead of drawing directly into the graphics/rendering context of Processing? With perfect quality as vector graphic. How could I do that?
At the moment I'm experimenting with JAVA2D objects and Batik/SVG. Please see the extended code here:
- import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.svggen.SVGGraphics2D;
import org.w3c.dom.*;
import java.awt.geom.Rectangle2D;
JFreeChart chart;
PImage chartImage;
PShape s;
SVGGraphics2D svg;
PGraphics pg;
//PGraphicsJava2D pgj; - alternative for PGraphics?
void setup(){
size(300, 300);
// create a dataset the chart is generated from
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("Processing", new Integer(75));
dataset.setValue("Other", new Integer(25));
// create the chart from dataset
chart = ChartFactory.createPieChart(null, dataset, false, true, false);
chart.setBorderVisible(false);
chart.setBackgroundPaint(java.awt.Color.white) ;
chart.removeLegend();
chart.setAntiAlias(true);
chart.setBorderPaint(java.awt.Color.white);
PiePlot pp = (PiePlot) chart.getPlot();
pp.setShadowXOffset(0);
pp.setShadowYOffset(0);
//pp.setLabelPaint(java.awt.Color.red);
pp.setLabelLinksVisible(false);
pp.setLabelGenerator(null);
//pp.setSectionPaint(java.awt.Color.red);
// pp.setBaseSectionPaint(java.awt.Color.green);
//pp.setForegroundAlpha(0.5f);
pp.setBackgroundAlpha(0);
pp.setOutlineVisible(false);
- // original BufferedImage approach
// create image from chart
chartImage = new PImage(chart.createBufferedImage(300,300));
- // SVG approach
// Get a DOMImplementation and create an XML document
DOMImplementation domImpl =
GenericDOMImplementation.getDOMImplementation();
Document document = domImpl.createDocument(null, "svg", null);
// Create an instance of the SVG Generator
svg = new SVGGraphics2D(document);
// draw the chart in the SVG generator
chart.draw(svg, new Rectangle2D.Double(0, 0, 400, 300), null);
//use PShape - not possible
//s = svg;
- // JAVA2D approach
pg = createGraphics(80, 80, P3D);
}
void draw(){
- // original BufferedImage approach - SUCCESS
// display chart image on screen
image(chartImage, 0, 0); - // SVG approach - FAILED
//shape(s,0,0,30,30); - not possible to cast - //JAVA2D approach - FAILED
pg.beginDraw();
//chart.draw( pg, new Rectangle2D.Double(0, 0, 400, 300)); - not possible to cast
pg.endDraw();
image(pg,50,50);
}
Thx a lot!
1