How to dump a Zest graph into an Image
If you are developing an Eclipse plug-in or RCP application which has something to do with graphs chances are you are using Zest. At one time you might want to add feature of exporting graph as an image. The one pattern I’ve found to do it (from Eclipse bugs) looks something like this:
// g is a zest graph object
Point size = new Point(g.getContents().getSize().width, g.getContents().getSize().height);
final Image image = new Image(null, size.x, size.y);
GC gc = new GC(image);
SWTGraphics swtGraphics = new SWTGraphics(gc);
g.getContents().paint(swtGraphics);
gc.copyArea(image, 0, 0);
gc.dispose();
This code works almost ok, but first of all has unnecessary call to gc.copyArea(). What hurts more is that it will dump only the part of graph that has positive coordinates. To get whole graph you have to move coordinate system of image.
Bounds size = g.getContents().getBounds();
final Image image = new Image(null, size.width, size.height);
GC gc = new GC(image);
SWTGraphics swtGraphics = new SWTGraphics(gc);
// moving coords system for the image.
swtGraphics.translate(-1 * size.x, -1 * size.y);
g.getContents().paint(swtGraphics);
gc.dispose();