Java 类org.eclipse.draw2d.SWTGraphics 实例源码

项目:NEXCORE-UML-Modeler    文件:PrintGraphicalViewerOperation.java   
/**
 * 실제 프린트에서 출력될 용지 1장의 크기를 반환한다.
 * 
 * @return
 */
public Rectangle getClipRect() {

    GC gc = new GC(getPrinter(), SWT.LEFT_TO_RIGHT);
    PrinterGraphics graphics = new PrinterGraphics(new SWTGraphics(gc), printer);
    setupGraphicsForPage(graphics);

    IFigure figure = getPrintSource();
    setupPrinterGraphicsFor(graphics, figure);
    Rectangle clipRect = new Rectangle();
    graphics.pushState();       
    int x = previewBounds.x, y = previewBounds.y;
    graphics.translate(-x, -y);
    graphics.getClip(clipRect);
    return clipRect;        
}
项目:seg.jUCMNav    文件:ExportImageGIF.java   
/**
 * Given the IFigure, save it to a file.
 * 
 * GIFs can only be saved with an 8 bit color depth.
 * 
 * @see seg.jUCMNav.extensionpoints.IUseCaseMapExport#export(org.eclipse.draw2d.IFigure, java.io.FileOutputStream)
 */
public void export(IFigure pane, FileOutputStream fos) {
    // generate image
    Image image = new Image(Display.getCurrent(), pane.getSize().width, pane.getSize().height);

    GC gc = new GC(image);
    SWTGraphics graphics = new SWTGraphics(gc);
    // if the bounds are in the negative x/y, we don't see them without a
    // translation
    graphics.translate(-pane.getBounds().x, -pane.getBounds().y);
    pane.paint(graphics);

    ImageLoader loader = new ImageLoader();
    loader.data = new ImageData[] { downSample(image) };
    loader.save(fos, getType());

    gc.dispose();
    image.dispose();
}
项目:seg.jUCMNav    文件:ExportImage.java   
/**
 * Given the IFigure, save it to a file.
 * 
 * @see seg.jUCMNav.extensionpoints.IUseCaseMapExport#export(org.eclipse.draw2d.IFigure, java.io.FileOutputStream)
 */
public void export(IFigure pane, FileOutputStream fos) {
    // generate image
    Image image = new Image(Display.getCurrent(), pane.getSize().width, pane.getSize().height);
    GC gc = new GC(image);
    SWTGraphics graphics = new SWTGraphics(gc);
    // if the bounds are in the negative x/y, we don't see them without a translation
    graphics.translate(-pane.getBounds().x, -pane.getBounds().y);
    pane.paint(graphics);

    ImageLoader loader = new ImageLoader();
    loader.data = new ImageData[] { ReportUtils.cropImage(image.getImageData()) };
    // loader.data = new ImageData[] { image.getImageData() };
    loader.save(fos, getType());

    gc.dispose();
    image.dispose();
}
项目:olca-app    文件:SaveImageAction.java   
@Override
public void run() {
    if (file == null)
        return;
    log.trace("export product graph as image: {}", file);
    ScalableRootEditPart editPart = (ScalableRootEditPart) editor.getGraphicalViewer().getRootEditPart();
    IFigure rootFigure = editPart.getLayer(LayerConstants.PRINTABLE_LAYERS);
    Rectangle bounds = rootFigure.getBounds();
    Image img = new Image(null, bounds.width, bounds.height);
    GC imageGC = new GC(img);
    Graphics graphics = new SWTGraphics(imageGC);
    rootFigure.paint(graphics);
    ImageLoader imgLoader = new ImageLoader();
    imgLoader.data = new ImageData[] { img.getImageData() };
    imgLoader.save(file.getAbsolutePath(), SWT.IMAGE_PNG);
}
项目:Open_Source_ECOA_Toolset_AS5    文件:ImageSaveUtil.java   
private static void saveEditorContentsAsImage(IEditorPart editorPart, GraphicalViewer viewer, String saveFilePath, int format) {

        ScalableFreeformRootEditPart rootEditPart = (ScalableFreeformRootEditPart) viewer.getEditPartRegistry().get(LayerManager.ID);
        IFigure rootFigure = ((LayerManager) rootEditPart).getLayer(LayerConstants.PRINTABLE_LAYERS);// rootEditPart.getFigure();
        Rectangle rootFigureBounds = rootFigure.getBounds();

        Control figureCanvas = viewer.getControl();
        GC figureCanvasGC = new GC(figureCanvas);

        Image img = new Image(null, rootFigureBounds.width, rootFigureBounds.height);
        GC imageGC = new GC(img);
        imageGC.setBackground(figureCanvasGC.getBackground());
        imageGC.setForeground(figureCanvasGC.getForeground());
        imageGC.setFont(figureCanvasGC.getFont());
        imageGC.setLineStyle(figureCanvasGC.getLineStyle());
        imageGC.setLineWidth(figureCanvasGC.getLineWidth());
        imageGC.setXORMode(figureCanvasGC.getXORMode());
        Graphics imgGraphics = new SWTGraphics(imageGC);

        rootFigure.paint(imgGraphics);

        ImageData[] imgData = new ImageData[1];
        imgData[0] = img.getImageData();

        ImageLoader imgLoader = new ImageLoader();
        imgLoader.data = imgData;
        imgLoader.save(saveFilePath, format);

        figureCanvasGC.dispose();
        imageGC.dispose();
        img.dispose();
    }
项目:pandionj    文件:RuntimeViewer.java   
void saveToImageFile() {
    Dimension size = rootFig.getPreferredSize();
    Image image = new Image(Display.getDefault(), size.width, size.height);
    GC gc = new GC(image);
    SWTGraphics graphics = new SWTGraphics(gc);
    rootFig.paint(graphics);
    Clipboard clipboard = new Clipboard(Display.getDefault());
    clipboard.setContents(new Object[]{image.getImageData()}, new Transfer[]{ ImageTransfer.getInstance()}); 
    image.dispose();
    gc.dispose();
}
项目:ForgedUI-Eclipse    文件:ChildElementResizableEditPolicy.java   
public GhostImageFigure(IFigure source, int alpha) {
    this.alpha = alpha;

    RGB transparency = new RGB(1, 1, 1);
    Rectangle sourceFigureRelativePrecisionBounds = new PrecisionRectangle(
            source.getBounds().getCopy());

    Image offscreenImage = new Image(Display.getCurrent(),
            sourceFigureRelativePrecisionBounds.width,
            sourceFigureRelativePrecisionBounds.height);

    Color transparentColor = new Color(null, transparency);
    GC gc = new GC(offscreenImage);
    SWTGraphics swtGraphics = new SWTGraphics(gc);
    swtGraphics.setBackgroundColor(transparentColor);
    swtGraphics.translate(-sourceFigureRelativePrecisionBounds.x,
            -sourceFigureRelativePrecisionBounds.y);
    swtGraphics.fillRectangle(sourceFigureRelativePrecisionBounds);
    source.paint(swtGraphics);

    ghostImageData = offscreenImage.getImageData();
    ghostImageData.transparentPixel = ghostImageData.palette
            .getPixel(transparency);

    transparentColor.dispose();
    offscreenImage.dispose();
    swtGraphics.dispose();
    gc.dispose();
}
项目:NEXCORE-UML-Modeler    文件:AttachementEditPart.java   
/**
 * @see nexcore.tool.uml.ui.core.diagram.edit.part.AbstractDiagramConnectionEditPart#createFigure()
 */
@Override
protected IFigure createFigure() {
    PolylineConnection connection = new PolylineConnection() {
        @Override
        public void paintFigure(Graphics graphics) {
            graphics.setAntialias(SWT.ON);
            super.paintFigure(graphics);
        }
    };
    connection.setLineStyle(SWTGraphics.LINE_DASH);
    connection.setForegroundColor(Display.getCurrent().getSystemColor(SWT.COLOR_GRAY));
    return connection;
}
项目:NEXCORE-UML-Modeler    文件:TitleBarBorder.java   
/**
 * getRotatedLabelImage
 *  
 * @param figure
 * @param rect
 * @return Image
 */
private Image getRotatedLabelImage(IFigure figure, Rectangle rect) {
    // 가로로 된 타이틀 사각형
    Rectangle temp = rect.getCopy().transpose();
    temp.x = temp.y = 0;

    // 이미지를 생성하고 이미지에 대한 GC를 생성한다.
    PaletteData palette = new PaletteData(0xFF, 0xFF00, 0xFF0000);

    ImageData imageData = new ImageData(temp.width, temp.height, 24, palette);
    Image image = new Image(Display.getCurrent(), imageData);
    GC gc = new GC(image);
    Graphics g = new SWTGraphics(gc);
    drawTitleBar(figure, g, temp);
    gc.dispose();

    ImageData data1 = image.getImageData();
    ImageData data2 = new ImageData(data1.height, data1.width, data1.depth, data1.palette);
    for (int x1 = 0; x1 < data1.width; x1++) {
        for (int y1 = 0; y1 < data1.height; y1++) {
            int x2 = y1;
            int y2 = -x1 + data1.width - 1;
            int pixel = data1.getPixel(x1, y1);
            data2.setPixel(x2, y2, pixel);
        }
    }

    Image result = new Image(Display.getCurrent(), data2);
    image.dispose();
    return result;
}
项目:scouter    文件:SingleSourceHelperImpl.java   
@Override
protected Image getInternalXYGraphSnapShot(XYGraph xyGraph) {
    Rectangle bounds = xyGraph.getBounds();
    Image image = new Image(null, bounds.width + 6, bounds.height + 6);
    GC gc = new GC(image);
    SWTGraphics graphics = new SWTGraphics(gc);
    graphics.translate(-bounds.x + 3, -bounds.y + 3);
    graphics.setForegroundColor(xyGraph.getForegroundColor());
    graphics.setBackgroundColor(xyGraph.getBackgroundColor());
    xyGraph.paint(graphics);
    gc.dispose();
    return image;
}
项目:PDFReporter-Studio    文件:Thumbnail.java   
/**
 * Starts this updater. This method initializes all the necessary resources and puts this {@link Runnable} on the
 * asynch queue. If this updater is not active or is already running, this method just returns.
 */
public void start() {
    if (!isActive() || isRunning())
        return;

    isRunning = true;
    setDirty(false);
    resetTileValues();

    if (!targetSize.equals(thumbnailImageSize)) {
        resetThumbnailImage();
    }

    if (targetSize.isEmpty())
        return;
    Rectangle e = getSourceRectangle();
    BufferedImage offsceeenBufferedImage = new BufferedImage(e.width, e.height, 2);
    Graphics2D graphics2d = offsceeenBufferedImage.createGraphics();

    thumbnailGC = new GC(thumbnailImage, sourceFigure.isMirrored() ? SWT.RIGHT_TO_LEFT : SWT.NONE);
    thumbnailGraphics = new J2DScaledGraphics(new SWTGraphics(thumbnailGC), thumbnailGC, graphics2d);
    thumbnailGraphics.scale(getScaleX());

    thumbnailGraphics.translate(e.getLocation().negate());

    Color color = sourceFigure.getForegroundColor();
    if (color != null)
        thumbnailGraphics.setForegroundColor(color);
    color = sourceFigure.getBackgroundColor();
    if (color != null)
        thumbnailGraphics.setBackgroundColor(color);
    thumbnailGraphics.setFont(sourceFigure.getFont());

    setScales(targetSize.width / (float) e.width, targetSize.height / (float) e.height);

    Display.getCurrent().asyncExec(this);
}
项目:erflute    文件:Activator.java   
@Override
public void run() {
    GC figureCanvasGC = null;
    GC imageGC = null;
    try {
        final ScalableFreeformRootEditPart rootEditPart = (ScalableFreeformRootEditPart) viewer.getRootEditPart();
        rootEditPart.refresh();
        final IFigure rootFigure = ((LayerManager) rootEditPart).getLayer(LayerConstants.PRINTABLE_LAYERS);
        final EditPart editPart = viewer.getContents();
        editPart.refresh();
        final ERDiagram diagram = (ERDiagram) editPart.getModel();
        final Rectangle rootFigureBounds = ExportToImageAction.getBounds(diagram, rootEditPart, rootFigure.getBounds());
        final Control figureCanvas = viewer.getControl();
        figureCanvasGC = new GC(figureCanvas);

        img = new Image(Display.getCurrent(), rootFigureBounds.width + 20, rootFigureBounds.height + 20);
        imageGC = new GC(img);
        imageGC.setBackground(figureCanvasGC.getBackground());
        imageGC.setForeground(figureCanvasGC.getForeground());
        imageGC.setFont(figureCanvasGC.getFont());
        imageGC.setLineStyle(figureCanvasGC.getLineStyle());
        imageGC.setLineWidth(figureCanvasGC.getLineWidth());
        imageGC.setAntialias(SWT.OFF);

        final Graphics imgGraphics = new SWTGraphics(imageGC);
        imgGraphics.setBackgroundColor(figureCanvas.getBackground());
        imgGraphics.fillRectangle(0, 0, rootFigureBounds.width + 20, rootFigureBounds.height + 20);
        imgGraphics.translate(ExportToImageAction.translateX(rootFigureBounds.x),
                ExportToImageAction.translateY(rootFigureBounds.y));
        rootFigure.paint(imgGraphics);
    } finally {
        if (figureCanvasGC != null) {
            figureCanvasGC.dispose();
        }
        if (imageGC != null) {
            imageGC.dispose();
        }
    }
}
项目:gef-gwt    文件:Thumbnail.java   
/**
 * Starts this updater. This method initializes all the necessary
 * resources and puts this {@link Runnable} on the asynch queue. If this
 * updater is not active or is already running, this method just
 * returns.
 */
public void start() {
    if (!isActive() || isRunning())
        return;

    isRunning = true;
    setDirty(false);
    resetTileValues();

    if (!targetSize.equals(thumbnailImageSize)) {
        resetThumbnailImage();
    }

    if (targetSize.isEmpty())
        return;

    thumbnailGC = new GC(thumbnailImage,
            sourceFigure.isMirrored() ? SWT.RIGHT_TO_LEFT : SWT.NONE);
    thumbnailGraphics = new ScaledGraphics(new SWTGraphics(thumbnailGC));
    thumbnailGraphics.scale(getScaleX());
    thumbnailGraphics.translate(getSourceRectangle().getLocation()
            .negate());

    Color color = sourceFigure.getForegroundColor();
    if (color != null)
        thumbnailGraphics.setForegroundColor(color);
    color = sourceFigure.getBackgroundColor();
    if (color != null)
        thumbnailGraphics.setBackgroundColor(color);
    thumbnailGraphics.setFont(sourceFigure.getFont());

    setScales(targetSize.width / (float) getSourceRectangle().width,
            targetSize.height / (float) getSourceRectangle().height);

    Display.getCurrent().asyncExec(this);
}
项目:billie    文件:Draw2DViewer.java   
private void saveImg(Panel content){
    Image image = new Image(Display.getCurrent(), content.getBounds().width, content.getBounds().height);
    GC gc = new GC(image);
    SWTGraphics graphics = new SWTGraphics(gc);
    content.paint(graphics);
    ImageLoader save = new ImageLoader();
    save.data = new ImageData[] {image.getImageData()};
    save.save(snapShotParams.saveImgName, snapShotParams.imgType);
    image.dispose();
    gc.dispose();

}
项目:collabthings.swt    文件:MapView.java   
public MapView(Composite parent) {
    super(parent, SWT.NONE);
    setLayout(new GridLayout(1, false));

    Composite cupper = new CTComposite(this, SWT.BORDER);
    cupper.setLayout(new GridLayout(1, false));
    cupper.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
            1));

    new CTLabel(cupper, SWT.NONE);

    Composite composite = new CTComposite(this, SWT.NONE);
    composite.setLayout(new GridLayout(1, false));
    composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1,
            1));

    Canvas c = new Canvas(composite, SWT.NONE);
    c.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

    composite.addControlListener(new ControlAdapter() {
        @Override
        public void controlResized(ControlEvent arg0) {
            c.setSize(composite.getSize().x, composite.getSize().y);
        }
    });
    c.setLayout(new GridLayout(1, false));

    graph = new Graph(c, SWT.NONE);
    graph.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

    c.addPaintListener((e) -> {
        GC gc = e.gc;
        graph.getContents().paint(new SWTGraphics(gc));
    });
}
项目:seg.jUCMNav    文件:CopyAction.java   
public static ImageData buildScreenshot(IWorkbenchPart part) {
    ImageData screenshot = null;

    if (part instanceof UCMNavMultiPageEditor) {
        UCMNavMultiPageEditor multi = (UCMNavMultiPageEditor) part;
        if (multi.getCurrentPage() != null) {
            UrnEditor editor = (UrnEditor) multi.getCurrentPage();
            LayeredPane pane = ((URNRootEditPart) (editor.getGraphicalViewer().getRootEditPart())).getScaledLayers();

            IFigure figure = pane;
            int w = figure.getSize().width;
            int h = figure.getSize().height;
            Image image = new Image(Display.getDefault(), w, h);

            GC gc = new GC(image);
            SWTGraphics graphics = new SWTGraphics(gc);
            graphics.translate(-pane.getBounds().x, -pane.getBounds().y);
            figure.paint(graphics);

            // TODO: Improve crop to make use of current selection.
            screenshot = ReportUtils.cropImage(image.getImageData());

            graphics.dispose();
            gc.dispose();
        }
    }
    return screenshot;
}
项目:olca-app    文件:SankeyImageAction.java   
private Image createImage() {
    try {
        ScalableRootEditPart root = (ScalableRootEditPart) sankeyDiagram.getGraphicalViewer().getRootEditPart();
        IFigure figure = root.getLayer(LayerConstants.PRINTABLE_LAYERS);
        Rectangle bounds = figure.getBounds();
        Image img = new Image(null, bounds.width, bounds.height);
        GC imageGC = new GC(img);
        Graphics graphics = new SWTGraphics(imageGC);
        figure.paint(graphics);
        return img;
    } catch (Exception e) {
        log.error("Could not create image", e);
        return null;
    }
}
项目:d-case_editor    文件:DcaseThumbnailEx.java   
/**
 * Starts this updater.  This method initializes all the necessary resources
 * and puts this {@link Runnable} on the asynch queue.  If this updater is
 * not active or is already running, this method just returns.
 */
public void start() {
    if (!isActive() || isRunning())
        return;

    isRunning = true;
    setDirty(false);        
    resetTileValues();

    if (!targetSize.equals(thumbnailImageSize)) {
        resetThumbnailImage();
    }

    if (targetSize.isEmpty())
        return;

    thumbnailGC = new GC(thumbnailImage, 
            sourceFigure.isMirrored() ? SWT.RIGHT_TO_LEFT : SWT.NONE);
    thumbnailGraphics = new ScaledGraphics(new SWTGraphics(thumbnailGC));
    thumbnailGraphics.scale(getScaleX());
    thumbnailGraphics.translate(getSourceRectangle().getLocation().negate());

    Color color = sourceFigure.getForegroundColor();
    if (color != null)
        thumbnailGraphics.setForegroundColor(color);
    color = sourceFigure.getBackgroundColor();
    if (color != null)
        thumbnailGraphics.setBackgroundColor(color);
    thumbnailGraphics.setFont(sourceFigure.getFont());

    setScales(targetSize.width / (float)getSourceRectangle().width,
             targetSize.height / (float)getSourceRectangle().height);

    Display.getCurrent().asyncExec(this);
}
项目:pandionj    文件:RuntimeViewer.java   
void copyToClipBoard() {
    Dimension size = rootFig.getPreferredSize();
    Image image = new Image(Display.getDefault(), size.width, size.height);
    GC gc = new GC(image);
    SWTGraphics graphics = new SWTGraphics(gc);
    rootFig.paint(graphics);
    Clipboard clipboard = new Clipboard(Display.getDefault());
    clipboard.setContents(new Object[]{image.getImageData()}, new Transfer[]{ ImageTransfer.getInstance()}); 
    image.dispose();
    gc.dispose();
}
项目:subclipse    文件:SaveImageDialog.java   
protected void okPressed() {
        settings.put(LAST_OUTPUT, fileTypeCombo.getSelectionIndex());

        BusyIndicator.showWhile(Display.getDefault(), new Runnable() {
            public void run() {
                GraphicalViewer viewer = editor.getViewer();
                ScalableRootEditPart rootEditPart = (ScalableRootEditPart)viewer.getEditPartRegistry().get(LayerManager.ID);
                IFigure rootFigure = ((LayerManager)rootEditPart).getLayer(LayerConstants.PRINTABLE_LAYERS);
                Rectangle rootFigureBounds = rootFigure.getBounds();    
                Control figureCanvas = viewer.getControl();
                GC figureCanvasGC = new GC(figureCanvas);
                Image img = new Image(null, rootFigureBounds.width, rootFigureBounds.height);
                GC imageGC = new GC(img);
                imageGC.setBackground(figureCanvasGC.getBackground());
                imageGC.setForeground(figureCanvasGC.getForeground());
                imageGC.setFont(figureCanvasGC.getFont());
                imageGC.setLineStyle(figureCanvasGC.getLineStyle());
                imageGC.setLineWidth(figureCanvasGC.getLineWidth());
//              imageGC.setXORMode(figureCanvasGC.getXORMode());
                Graphics imgGraphics = new SWTGraphics(imageGC);

                rootFigure.paint(imgGraphics);

                ImageData[] imgData = new ImageData[1];
                imgData[0] = img.getImageData();

                ImageLoader imgLoader = new ImageLoader();
                imgLoader.data = imgData;
                String extension;
                if (fileTypeCombo.getText().equals("JPEG")) extension = "jpg";
                else extension = fileTypeCombo.getText().toLowerCase();
                String fileName = null;
                if (!fileText.getText().trim().endsWith("." + extension))
                    fileName = fileText.getText().trim() + "." + extension;
                else
                    fileName = fileText.getText().trim();
                int imageType = SWT.IMAGE_BMP;
                switch (fileTypeCombo.getSelectionIndex()) {
                case BMP:
                    imageType = SWT.IMAGE_BMP;
                    break;
                case JPEG:
                    imageType = SWT.IMAGE_JPEG;
                    break;  
                case PNG:
                    imageType = SWT.IMAGE_PNG;
                    break;                  
                default:
                    break;
                }               
                imgLoader.save(fileName, imageType);

                figureCanvasGC.dispose();
                imageGC.dispose();
                img.dispose();
            }       
        });     

        super.okPressed();
    }
项目:ermasterr    文件:ImageInfo.java   
public static ImageInfo createImage(final GraphicalViewer viewer, final int format, final String path) {
    GC figureCanvasGC = null;
    GC imageGC = null;

    try {
        final ScalableFreeformRootEditPart rootEditPart = (ScalableFreeformRootEditPart) viewer.getRootEditPart();
        rootEditPart.refresh();
        final IFigure rootFigure = ((LayerManager) rootEditPart).getLayer(LayerConstants.PRINTABLE_LAYERS);

        final EditPart editPart = viewer.getContents();
        editPart.refresh();
        final ERDiagram diagram = (ERDiagram) editPart.getModel();

        final Rectangle rootFigureBounds = getBounds(diagram, rootEditPart, rootFigure.getBounds());

        final Control figureCanvas = viewer.getControl();
        figureCanvasGC = new GC(figureCanvas);

        final Image img = new Image(Display.getCurrent(), rootFigureBounds.width + 20, rootFigureBounds.height + 20);
        imageGC = new GC(img);

        imageGC.setBackground(figureCanvasGC.getBackground());
        imageGC.setForeground(figureCanvasGC.getForeground());
        imageGC.setFont(figureCanvasGC.getFont());
        imageGC.setLineStyle(figureCanvasGC.getLineStyle());
        imageGC.setLineWidth(figureCanvasGC.getLineWidth());
        imageGC.setAntialias(SWT.OFF);
        // imageGC.setInterpolation(SWT.HIGH);

        final Graphics imgGraphics = new SWTGraphics(imageGC);
        imgGraphics.setBackgroundColor(figureCanvas.getBackground());
        imgGraphics.fillRectangle(0, 0, rootFigureBounds.width + 20, rootFigureBounds.height + 20);

        final int translateX = translateX(rootFigureBounds.x);
        final int translateY = translateY(rootFigureBounds.y);

        imgGraphics.translate(translateX, translateY);

        rootFigure.paint(imgGraphics);

        final Map<TableView, Location> tableLoacationMap = getTableLocationMap(rootEditPart, translateX, translateY, diagram);

        final ImageInfo imageInfo = new ImageInfo(img, tableLoacationMap);
        imageInfo.setFormat(format);
        imageInfo.setPath(path);

        return imageInfo;

    } finally {
        if (figureCanvasGC != null) {
            figureCanvasGC.dispose();
        }
        if (imageGC != null) {
            imageGC.dispose();
        }
    }
}
项目:ermaster-k    文件:ImageInfo.java   
public static ImageInfo createImage(GraphicalViewer viewer, int format,
        String path) {
    GC figureCanvasGC = null;
    GC imageGC = null;

    try {
        ScalableFreeformRootEditPart rootEditPart = (ScalableFreeformRootEditPart) viewer
                .getRootEditPart();
        rootEditPart.refresh();
        IFigure rootFigure = ((LayerManager) rootEditPart)
                .getLayer(LayerConstants.PRINTABLE_LAYERS);

        EditPart editPart = viewer.getContents();
        editPart.refresh();
        ERDiagram diagram = (ERDiagram) editPart.getModel();

        Rectangle rootFigureBounds = getBounds(diagram, rootEditPart,
                rootFigure.getBounds());

        Control figureCanvas = viewer.getControl();
        figureCanvasGC = new GC(figureCanvas);

        Image img = new Image(Display.getCurrent(),
                rootFigureBounds.width + 20, rootFigureBounds.height + 20);
        imageGC = new GC(img);

        imageGC.setBackground(figureCanvasGC.getBackground());
        imageGC.setForeground(figureCanvasGC.getForeground());
        imageGC.setFont(figureCanvasGC.getFont());
        imageGC.setLineStyle(figureCanvasGC.getLineStyle());
        imageGC.setLineWidth(figureCanvasGC.getLineWidth());
        imageGC.setAntialias(SWT.OFF);
        // imageGC.setInterpolation(SWT.HIGH);

        Graphics imgGraphics = new SWTGraphics(imageGC);
        imgGraphics.setBackgroundColor(figureCanvas.getBackground());
        imgGraphics.fillRectangle(0, 0, rootFigureBounds.width + 20,
                rootFigureBounds.height + 20);

        int translateX = translateX(rootFigureBounds.x);
        int translateY = translateY(rootFigureBounds.y);

        imgGraphics.translate(translateX, translateY);

        rootFigure.paint(imgGraphics);

        Map<TableView, Location> tableLoacationMap = getTableLocationMap(
                rootEditPart, translateX, translateY, diagram);

        ImageInfo imageInfo = new ImageInfo(img, tableLoacationMap);
        imageInfo.setFormat(format);
        imageInfo.setPath(path);

        return imageInfo;

    } finally {
        if (figureCanvasGC != null) {
            figureCanvasGC.dispose();
        }
        if (imageGC != null) {
            imageGC.dispose();
        }
    }
}
项目:ForgedUI-Eclipse    文件:ImageSaveUtil.java   
private static void saveEditorContentsAsImage(IEditorPart editorPart,
        GraphicalViewer viewer, String saveFilePath, int format) {
    /*
     * 1. First get the figure whose visuals we want to save as image. So we
     * would like to save the rooteditpart which actually hosts all the
     * printable layers.
     * 
     * NOTE: ScalableRootEditPart manages layers and is registered
     * graphicalviewer's editpartregistry with the key LayerManager.ID ...
     * well that is because ScalableRootEditPart manages all layers that are
     * hosted on a FigureCanvas. Many layers exist for doing different
     * things
     */
    Map editPartRegistry = viewer.getEditPartRegistry();
    LayerManager rootEditPart = (LayerManager) editPartRegistry.get(LayerManager.ID);
    IFigure rootFigure = rootEditPart.getLayer(LayerConstants.PRINTABLE_LAYERS);

    for (Object obj : editPartRegistry.values()) {
        if (obj instanceof ScreenEditPart){
            rootFigure = ((ScreenEditPart)obj).getFigure();
            break;
        }
    }
    Rectangle rootFigureBounds = rootFigure.getBounds();

    /*
     * 2. Now we want to get the GC associated with the control on which all
     * figures are painted by SWTGraphics. For that first get the SWT
     * Control associated with the viewer on which the rooteditpart is set
     * as contents
     */
    Control figureCanvas = viewer.getControl();
    GC figureCanvasGC = new GC(figureCanvas);

    /*
     * 3. Create a new Graphics for an Image onto which we want to paint
     * rootFigure
     */
    Image img = new Image(null, rootFigureBounds.width,
            rootFigureBounds.height);
    GC imageGC = new GC(img);
    imageGC.setBackground(figureCanvasGC.getBackground());
    imageGC.setForeground(figureCanvasGC.getForeground());
    imageGC.setFont(figureCanvasGC.getFont());
    imageGC.setLineStyle(figureCanvasGC.getLineStyle());
    imageGC.setLineWidth(figureCanvasGC.getLineWidth());
    imageGC.setXORMode(figureCanvasGC.getXORMode());
    Graphics imgGraphics = new SWTGraphics(imageGC);

    /*
     * 4. Draw rootFigure onto image. After that image will be ready for
     * save
     */
    rootFigure.paint(imgGraphics);

    /* 5. Save image */
    ImageData[] imgData = new ImageData[1];
    imgData[0] = img.getImageData();

    ImageLoader imgLoader = new ImageLoader();
    imgLoader.data = imgData;
    imgLoader.save(saveFilePath, format);

    /* release OS resources */
    figureCanvasGC.dispose();
    imageGC.dispose();
    img.dispose();
}
项目:ForgedUI-Eclipse    文件:Drawer.java   
public static Image getGradientRectangle(PrecisionRectangle tempRect, RGB bottomRGB, RGB topRGB,
        float internalRadius, int maxGradientHeight, int roundedPlace,boolean drawLeftSide,boolean drawRightSide){
    Color bottomColor = new Color(null, bottomRGB);
    Color topColor = new Color(null, topRGB);

    PrecisionRectangle gradientRect = tempRect.getPreciseCopy();
    //gradientRect.height = tempRect.height / 2;
    if (gradientRect.height == 0) gradientRect.height = 1;
    if (gradientRect.height > maxGradientHeight )
        gradientRect.setHeight(maxGradientHeight);

    Image gradientImage = new Image(Display.getCurrent(), gradientRect.width, gradientRect.height);
    GC gc = new GC(gradientImage);
    SWTGraphics swtGraphics = new SWTGraphics(gc);
    swtGraphics.setForegroundColor(topColor);
    swtGraphics.setBackgroundColor(bottomColor);
    swtGraphics.translate((float)-tempRect.preciseX,
            (float)-tempRect.preciseY);

    swtGraphics.fillGradient(gradientRect, true);

    //using this color allows us to remove black arch at the corners
    Color transparent = (roundedPlace == SWT.TOP) ? topColor : bottomColor;
    RGB transparentRGB = transparent.getRGB();
    swtGraphics.setBackgroundColor(transparent);

    Path transparentCorners = new Path(Display.getCurrent());
    transparentCorners.addRectangle(gradientRect.x, gradientRect.y, gradientRect.width, gradientRect.height);
    switch (roundedPlace) {
    case SWT.TOP:
        transparentCorners.addPath(getTopRoundedRectangle(gradientRect, internalRadius,drawLeftSide,drawRightSide));
        break;
    case SWT.BOTTOM:
        transparentCorners.addPath(getBottomRoundedRectangle(gradientRect, internalRadius,drawLeftSide,drawRightSide));
        break;
    default:
        transparentCorners.addPath(getRoundedRectangle(gradientRect, internalRadius,drawLeftSide,drawRightSide));
    }

    swtGraphics.fillPath(transparentCorners);
    //for debug reason
    /*switch (roundedPlace) {
    case SWT.TOP:
        swtGraphics.drawPath(getTopRoundedRectangle(gradientRect, internalRadius));
        break;
    case SWT.BOTTOM:
        swtGraphics.drawPath(getBottomRoundedRectangle(gradientRect, internalRadius));
        break;
    default:
        swtGraphics.drawPath(getRoundedRectangle(gradientRect, internalRadius));
    }*/


    ImageData iData = gradientImage.getImageData();
    iData.transparentPixel = iData.palette.getPixel(transparentRGB);

    swtGraphics.dispose();
    gc.dispose();
    gradientImage.dispose();
    bottomColor.dispose();
    topColor.dispose();

    return new Image(Display.getCurrent(), iData);
}
项目:SecureBPMN    文件:ImageExportMarshaller.java   
private void marshallImage() {
  try {

    // Retrieve GraphicalViewer from the save handler
    final GraphicalViewer graphicalViewer = ActivitiDiagramEditor.getActiveGraphicalViewer();

    if (graphicalViewer == null || graphicalViewer.getEditPartRegistry() == null)
      return;
    final ScalableFreeformRootEditPart rootEditPart = (ScalableFreeformRootEditPart) graphicalViewer.getEditPartRegistry().get(LayerManager.ID);
    final IFigure rootFigure = ((LayerManager) rootEditPart).getLayer(LayerConstants.PRINTABLE_LAYERS);
    final IFigure gridFigure = ((LayerManager) rootEditPart).getLayer(LayerConstants.GRID_LAYER);
    final Rectangle rootFigureBounds = rootFigure.getBounds();

    final boolean toggleRequired = gridFigure.isShowing();

    final Display display = Display.getDefault();

    final Image img = new Image(display, rootFigureBounds.width, rootFigureBounds.height);
    final GC imageGC = new GC(img);
    final SWTGraphics grap = new SWTGraphics(imageGC);

    // Access UI thread from runnable to print the canvas to the image
    display.syncExec(new Runnable() {

      public void run() {
        if (toggleRequired) {
          // Disable any grids temporarily
          gridFigure.setVisible(false);
        }
        // Deselect any selections
        graphicalViewer.deselectAll();
        rootFigure.paint(grap);
      }
    });

    ImageLoader imgLoader = new ImageLoader();
    imgLoader.data = new ImageData[] { img.getImageData() };

    ByteArrayOutputStream baos = new ByteArrayOutputStream(imgLoader.data.length);

    imgLoader.save(baos, SWT.IMAGE_PNG);

    imageGC.dispose();
    img.dispose();

    // Access UI thread from runnable
    display.syncExec(new Runnable() {

      public void run() {
        if (toggleRequired) {
          // Re-enable any grids
          gridFigure.setVisible(true);
        }
      }
    });

    final byte[] bytes = baos.toByteArray();
    final ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    saveResource(getRelativeURIForDiagram(diagram, FILENAME_PATTERN), bais, this.monitor);

  } catch (Exception e) {
    e.printStackTrace();
    addProblemToDiagram(diagram, "An exception occurred while creating the image: " + e.getCause(), null);
  }
}
项目:erflute    文件:ExportToImageAction.java   
public static Image createImage(GraphicalViewer viewer) {
    Image img = null;
    GC figureCanvasGC = null;
    GC imageGC = null;
    try {
        final ScalableFreeformRootEditPart rootEditPart =
                (ScalableFreeformRootEditPart) viewer.getEditPartRegistry().get(LayerManager.ID);
        rootEditPart.refresh();
        final IFigure rootFigure = ((LayerManager) rootEditPart).getLayer(LayerConstants.PRINTABLE_LAYERS);

        final Object diagram = extractDiagram(viewer);
        final Rectangle rootFigureBounds = getBounds(diagram, rootEditPart, rootFigure.getBounds());

        final Control figureCanvas = viewer.getControl();
        figureCanvasGC = new GC(figureCanvas);

        img = new Image(Display.getCurrent(), rootFigureBounds.width + 20, rootFigureBounds.height + 20);
        imageGC = new GC(img);

        imageGC.setBackground(figureCanvasGC.getBackground());
        imageGC.setForeground(figureCanvasGC.getForeground());
        imageGC.setFont(figureCanvasGC.getFont());
        imageGC.setLineStyle(figureCanvasGC.getLineStyle());
        imageGC.setLineWidth(figureCanvasGC.getLineWidth());
        imageGC.setAntialias(SWT.OFF);
        // imageGC.setInterpolation(SWT.HIGH);

        final Graphics imgGraphics = new SWTGraphics(imageGC);
        imgGraphics.setBackgroundColor(figureCanvas.getBackground());
        imgGraphics.fillRectangle(0, 0, rootFigureBounds.width + 20, rootFigureBounds.height + 20);

        imgGraphics.translate(translateX(rootFigureBounds.x), translateY(rootFigureBounds.y));

        rootFigure.paint(imgGraphics);

        return img;

    } finally {
        if (figureCanvasGC != null) {
            figureCanvasGC.dispose();
        }
        if (imageGC != null) {
            imageGC.dispose();
        }
    }
}
项目:ermaster-nhit    文件:ImageInfo.java   
public static ImageInfo createImage(GraphicalViewer viewer, int format,
        String path) {
    GC figureCanvasGC = null;
    GC imageGC = null;

    try {
        ScalableFreeformRootEditPart rootEditPart = (ScalableFreeformRootEditPart) viewer
                .getRootEditPart();
        rootEditPart.refresh();
        IFigure rootFigure = ((LayerManager) rootEditPart)
                .getLayer(LayerConstants.PRINTABLE_LAYERS);

        EditPart editPart = viewer.getContents();
        editPart.refresh();
        ERDiagram diagram = (ERDiagram) editPart.getModel();

        Rectangle rootFigureBounds = getBounds(diagram, rootEditPart,
                rootFigure.getBounds());

        Control figureCanvas = viewer.getControl();
        figureCanvasGC = new GC(figureCanvas);

        Image img = new Image(Display.getCurrent(),
                rootFigureBounds.width + 20, rootFigureBounds.height + 20);
        imageGC = new GC(img);

        imageGC.setBackground(figureCanvasGC.getBackground());
        imageGC.setForeground(figureCanvasGC.getForeground());
        imageGC.setFont(figureCanvasGC.getFont());
        imageGC.setLineStyle(figureCanvasGC.getLineStyle());
        imageGC.setLineWidth(figureCanvasGC.getLineWidth());
        imageGC.setAntialias(SWT.OFF);
        // imageGC.setInterpolation(SWT.HIGH);

        Graphics imgGraphics = new SWTGraphics(imageGC);
        imgGraphics.setBackgroundColor(figureCanvas.getBackground());
        imgGraphics.fillRectangle(0, 0, rootFigureBounds.width + 20,
                rootFigureBounds.height + 20);

        int translateX = translateX(rootFigureBounds.x);
        int translateY = translateY(rootFigureBounds.y);

        imgGraphics.translate(translateX, translateY);

        rootFigure.paint(imgGraphics);

        Map<TableView, Location> tableLoacationMap = getTableLocationMap(
                rootEditPart, translateX, translateY, diagram);

        ImageInfo imageInfo = new ImageInfo(img, tableLoacationMap);
        imageInfo.setFormat(format);
        imageInfo.setPath(path);

        return imageInfo;

    } finally {
        if (figureCanvasGC != null) {
            figureCanvasGC.dispose();
        }
        if (imageGC != null) {
            imageGC.dispose();
        }
    }
}
项目:seg.jUCMNav    文件:RTFReportDiagram.java   
/**
 * inserts the diagram figure
 * 
 * @param document
 *            the document we are reporting into
 * @param mapDiagrams
 *            list of diagrams to be documented
 * @param diagram
 *            the diagram to insert
 * @param urndef
 *            the urn definition of the elements
 * @param i
 *            the diagram number
 * @param pagesize
 *            the rectangle with dimensions for the size of page
 */

public void insertDiagram(Document document, HashMap mapDiagrams, IURNDiagram diagram, URNdefinition urndef, int i, Rectangle pagesize) {
    try {
        // get the high level IFigure to be saved.
        IFigure pane = (IFigure) mapDiagrams.get(diagram);

        //int paneWidth = Math.round(pane.getSize().width * ReportUtils.ZOOMFACTOR);
        //int paneHeight = Math.round(pane.getSize().height * ReportUtils.ZOOMFACTOR);
        int paneWidth = Math.round(pane.getSize().width * ReportUtils.ZOOMFACTOR);
        int paneHeight = Math.round(pane.getSize().height * ReportUtils.ZOOMFACTOR);
        Image image = new Image(Display.getCurrent(), paneWidth, paneHeight);

        GC gc = new GC(image);
        SWTGraphics graphics = new SWTGraphics(gc);

        // zoom for better resolution
        graphics.scale(ReportUtils.ZOOMFACTOR); // temporary

        // if the bounds are in the negative x/y, we don't see them without a translation
        graphics.translate(-pane.getBounds().x, -pane.getBounds().y);
        pane.paint(graphics);

        // downSample the image to an 8-bit palette, using the 256 most frequently used color
        ImageData ideaImageData = ExportImageGIF.downSample(image);

        ImageData croppedImage = ReportUtils.cropImage(ideaImageData);
        java.awt.Image awtImage = ReportUtils.SWTimageToAWTImage(croppedImage);

        BufferedImage bufferedImage = (BufferedImage) awtImage;
        int imageHeight = bufferedImage.getHeight();
        int imageWidth = bufferedImage.getWidth();

        ReportUtils.insertRTFImage(document, pane, pagesize, imageWidth, imageHeight);

        gc.dispose();
        image.dispose();
        awtImage.flush();

        boolean isLast = i == mapDiagrams.size() - 1;

    } catch (Exception e) {
        jUCMNavErrorDialog error = new jUCMNavErrorDialog(e.getMessage());
        e.printStackTrace();

    }
}
项目:cogtool    文件:GraphicalWidgetClipper.java   
public Image clip(Image g)
{
    // Create temporary resources for drawing
    Image mask = new Image(null, g.getBounds());
    GC gc = new GC(mask);
    Graphics template = new SWTGraphics(gc);

    // Fill the visible portion of the mask with a dummy color
    Color maskColor = new Color(null, 255, 0, 0);
    gc.setBackground(maskColor);
    fillShape(template, new Rectangle(g.getBounds()));

    // Get an example of a transparent pixel
    // The 0,0 pixel is transparent since the fill only
    // applies to Rounded, or oval clip regions
    int transPixel = mask.getImageData().getPixel(0, 0);

    // Get pixel data array from the mask
    int[] pixels = new int[mask.getBounds().width * mask.getBounds().height];
    mask.getImageData().getPixels(0, 0, pixels.length, pixels, 0);

    // Use the mask image to generate an alpha data array
    ImageData data = g.getImageData();
    byte[] alphas = new byte[pixels.length];
    data.getAlphas(0, 0, alphas.length, alphas, 0);

    // XXX: Workaround for the dysfunctional transparency
    //      support with Graphics objects on Windows platforms
    //      SPlit the code here because its an extra comparison
    //      and set on each iteration of the loop
    if (OSUtils.WINDOWS) {
        // Create the clipped image
        for (int i = 0; i < pixels.length; i++) {
            if (pixels[i] == transPixel) {
                alphas[i] = (byte) 0;
            }
            else if (data.alpha != -1) {
                alphas[i] = (byte) data.alpha;
            }
        }
    }
    else {
        // Create the clipped image
        for (int i = 0; i < pixels.length; i++) {
            if (pixels[i] == transPixel) {
                alphas[i] = (byte) 0;
            }
        }
    }

    data.alpha = -1;
    data.setAlphas(0, 0, alphas.length, alphas, 0);
    Image clipped = new Image(null, data);

    // Dispose all temporary resources we created
    g.dispose();
    gc.dispose();
    mask.dispose();
    maskColor.dispose();
    template.dispose();

    return clipped;
}
项目:birt    文件:ReportPrintGraphicalViewerOperation.java   
/**
 * Creates a new PrinterGraphics with Graphics g, using Printer p ;
 * 
 * @param g
 *            Graphics object to draw with
 * @param p
 *            Printer to print to
 */
public CompositePrinterGraphics( SWTGraphics g, Device p )
{
    super( g );
    printer = p;
}
项目:seg.jUCMNav    文件:PDFReportDiagram.java   
/**
     * inserts the diagram figure
     * 
     * @param document
     *            the document we are reporting into
     * @param mapDiagrams
     *            list of diagrams to be documented
     * @param diagram
     *            the diagram to insert
     * @param urndef
     *            the urn definition of the elements
     * @param i
     *            the diagram number
     * @param pagesize
     *            the rectangle with dimensions for the size of page
     */

    public void insertDiagram(final Document document, HashMap mapDiagrams, IURNDiagram diagram, URNdefinition urndef, int i, final Rectangle pagesize) {

        try {
            // get the high level IFigure to be saved.
            final IFigure pane = (IFigure) mapDiagrams.get(diagram);

            final int paneWidth = Math.round(pane.getSize().width * ReportUtils.ZOOMFACTOR);
            final int paneHeight = Math.round(pane.getSize().height * ReportUtils.ZOOMFACTOR);

            Display.getDefault().syncExec(new Runnable() {
                public void run() {


//            Image image = new Image(Display.getCurrent(), paneWidth, paneHeight);
            Image image = new Image(Display.getDefault(), paneWidth, paneHeight);

            GC gc = new GC(image);
            SWTGraphics graphics = new SWTGraphics(gc);

            // zoom for better resolution
            graphics.scale(ReportUtils.ZOOMFACTOR);

            // if the bounds are in the negative x/y, we don't see them without a translation
            graphics.translate(-pane.getBounds().x, -pane.getBounds().y);
            pane.paint(graphics);

            // downSample the image to an 8-bit palette, using the 256 most frequently used color
            ImageData ideaImageData = ExportImageGIF.downSample(image);

            ImageData croppedImage = ReportUtils.cropImage(ideaImageData);
            java.awt.Image awtImage = ReportUtils.SWTimageToAWTImage(croppedImage);

            BufferedImage bufferedImage = (BufferedImage) awtImage;
            int imageHeight = bufferedImage.getHeight();
            int imageWidth = bufferedImage.getWidth();

            ReportUtils.insertImage(document, awtImage, pagesize, imageWidth, imageHeight);

            gc.dispose();
            image.dispose();
            awtImage.flush();

                }
            });

            boolean isLast = i == mapDiagrams.size() - 1;

        } catch (Exception e) {
            jUCMNavErrorDialog error = new jUCMNavErrorDialog(e.getMessage());
            e.printStackTrace();

        }
    }