@SuppressWarnings("unchecked") @Override protected void paintFigure(final Graphics graphics) { if (gc == null || gc.isDisposed()) { return; } Rectangle clip = graphics.getClip(new Rectangle()); graphics.setClip(getBounds()); ArrayList<IFigure> children = new ArrayList<IFigure>(getChildren()); Collections.sort( children, new Comparator<IFigure>() { public int compare(final IFigure o1, final IFigure o2) { Integer z1 = ObjectFigure.Z0; Integer z2 = ObjectFigure.Z0; if (o1 instanceof ObjectFigure) { z1 = ((ObjectFigure) o1).getZ(); } if (o2 instanceof ObjectFigure) { z2 = ((ObjectFigure) o2).getZ(); } return z1.compareTo(z2); } }); for (final Object object : children) { ((IFigure) object).paint(graphics); } graphics.setClip(clip); }
/** * Export an image to SVG file format. * * @param file * @param rootFigure * @throws IOException */ public void exportToSVG(File file, IFigure rootFigure) throws IOException { Rectangle bounds = rootFigure.getBounds(); GraphicsSVG graphics = GraphicsSVG.getInstance(bounds.getTranslated(bounds.getLocation().negate())); graphics.translate(bounds.getLocation().negate()); rootFigure.paint(graphics); // graphics.getSVGGraphics2D().stream(new BufferedWriter(new OutputStreamWriter(new // FileOutputStream(file)))); // FIXME: SVG export is not working any more due to API restrictions of GMF }
/** * Returns the bytes of an encoded image for the specified IFigure in the specified format. * * @param figure the Figure to create an image for. * @param format one of SWT.IMAGE_BMP, SWT.IMAGE_BMP_RLE, SWT.IMAGE_GIF SWT.IMAGE_ICO, * SWT.IMAGE_JPEG, or SWT.IMAGE_PNG * @return the bytes of an encoded image for the specified Figure */ private byte[] createImage(final IFigure figure, final int format) { final Device device = viewer.getControl().getDisplay(); final Rectangle r = figure.getBounds(); final ByteArrayOutputStream result = new ByteArrayOutputStream(); Image image = null; GC gc = null; Graphics g = null; try { image = new Image(device, r.width, r.height); gc = new GC(image); g = new SWTGraphics(gc); g.translate(r.x * -1, r.y * -1); figure.paint(g); final ImageLoader imageLoader = new ImageLoader(); imageLoader.data = new ImageData[] {image.getImageData()}; imageLoader.save(result, format); } catch (Exception e) { // the image size may not exceed 16M, svg requires less space for graphs than png and jpeg e.printStackTrace(); } finally { if (g != null) { g.dispose(); } if (gc != null) { gc.dispose(); } if (image != null) { image.dispose(); } } return result.toByteArray(); }