Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » How to draw IFigure into Image
How to draw IFigure into Image [message #684898] Thu, 16 June 2011 12:54 Go to next message
Jan Krakora is currently offline Jan KrakoraFriend
Messages: 477
Registered: December 2009
Location: Prague
Senior Member
Hi guys,

I have a draw2D graph represented by it's root IFigure. This graph is drawn on a FigureCanvas.
This canvas is resizable, so users can resize it in both directions.
Now I need to save this graph as an PNG image. I can save it this way:

IFigure graph = ....my graph
Image img = new Image(Display.getDefault(), graph.getBounds().width, graph.getBounds().height);
final GC imageGC = new GC(img);
Display.getDefault().syncExec(new Runnable() {

	public void run() {
	    graph.paint(new SWTGraphics(imageGC));
	}
});
imageGC.dispose();


But it always draw the actual size of the graph IFigure. I would like to draw it every time in my specific size.
How can I do that? For example 300 x 300.

Re: How to draw IFigure into Image [message #686604 is a reply to message #684898] Wed, 22 June 2011 15:25 Go to previous messageGo to next message
h1055071 is currently offline h1055071Friend
Messages: 335
Registered: July 2009
Senior Member
How about this:

    public Image createScaledImage(GraphicalViewer graphicalViewer, double scale) {
        IFigure figure = ((FreeformGraphicalRootEditPart)graphicalViewer.getRootEditPart()).getLayer(LayerConstants.PRINTABLE_LAYERS);
        Rectangle extents = figure.getBounds().getCopy();
        
        Image image = new Image(Display.getDefault(), (int)(extents.width * scale), (int)(extents.height * scale) );
        GC gc = new GC(image);
        SWTGraphics swtGraphics = new SWTGraphics(gc);
        Graphics graphics = swtGraphics;
        
        // If scaled, then scale now
        if(scale != 1) {
            graphics = new ScaledGraphics(swtGraphics);
            graphics.scale(scale);
        }
        
        // Compensate for negative co-ordinates
        graphics.translate(extents.x * -1, extents.y * -1);

        // Paint onto graphics
        figure.paint(graphics);
        
        // Dispose
        gc.dispose();
        graphics.dispose();
        if(swtGraphics != graphics) {
            swtGraphics.dispose();
        }
        
        return image;
    }

[Updated on: Wed, 22 June 2011 15:26]

Report message to a moderator

Re: How to draw IFigure into Image [message #689294 is a reply to message #686604] Mon, 27 June 2011 11:23 Go to previous message
Jan Krakora is currently offline Jan KrakoraFriend
Messages: 477
Registered: December 2009
Location: Prague
Senior Member
Thans Phill, but this solution also scale all fonts which is something I don't need. Never mind, i just create all the figure tree one more time with the size I need.
Previous Topic:Drag and drop
Next Topic:Resizing LabelEditParts
Goto Forum:
  


Current Time: Fri Oct 22 04:41:06 GMT 2021

Powered by FUDForum. Page generated in 0.01805 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top