Java Code Examples for org.eclipse.swt.graphics.GC#setTransform()

The following examples show how to use org.eclipse.swt.graphics.GC#setTransform() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SwtUniversalImageBitmap.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@Override
protected Image renderRotated( Device device, int width, int height, double angleRadians ) {
  Image result = new Image( device, width * 2, height * 2 );

  GC gc = new GC( result );

  int bw = bitmap.getBounds().width;
  int bh = bitmap.getBounds().height;
  Transform affineTransform = new Transform( device );
  affineTransform.translate( width, height );
  affineTransform.rotate( (float) Math.toDegrees( angleRadians ) );
  affineTransform.scale( (float) 1.0 * width / bw, (float) 1.0 * height / bh );
  gc.setTransform( affineTransform );

  gc.drawImage( bitmap, 0, 0, bw, bh, -bw / 2, -bh / 2, bw, bh );

  gc.dispose();

  return result;
}
 
Example 2
Source File: TagCloud.java    From gef with Eclipse Public License 2.0 6 votes vote down vote up
private void zoom(double s) {
	checkWidget();
	if (selectionLayerImage == null)
		return;
	if (s < 0.1)
		s = 0.1;
	if (s > 3)
		s = 3;
	int width = (int) (selectionLayerImage.getBounds().width * s);
	int height = (int) (selectionLayerImage.getBounds().height * s);
	if (width == 0 || height == 0)
		return;
	zoomLayerImage = new Image(getDisplay(), width, height);
	Transform tf = new Transform(getDisplay());
	tf.scale((float) s, (float) s);
	GC gc = new GC(zoomLayerImage);
	gc.setTransform(tf);
	gc.drawImage(selectionLayerImage, 0, 0);
	gc.dispose();
	tf.dispose();
	currentZoom = s;
	updateScrollbars();
	redraw();
}
 
Example 3
Source File: SwtUniversalImageBitmap.java    From hop with Apache License 2.0 6 votes vote down vote up
@Override
protected Image renderRotated( Device device, int width, int height, double angleRadians ) {
  Image result = new Image( device, width * 2, height * 2 );

  GC gc = new GC( result );

  int bw = bitmap.getBounds().width;
  int bh = bitmap.getBounds().height;
  Transform affineTransform = new Transform( device );
  affineTransform.translate( width, height );
  affineTransform.rotate( (float) Math.toDegrees( angleRadians ) );
  affineTransform.scale( (float) zoomFactor * width / bw, (float) zoomFactor * height / bh );
  gc.setTransform( affineTransform );

  gc.drawImage( bitmap, 0, 0, bw, bh, -bw / 2, -bh / 2, bw, bh );

  gc.dispose();

  return result;
}
 
Example 4
Source File: VisualisationCanvas.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
protected void doPaint(GC gc, PaintEvent event) {
	gc.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE));
	gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));

	gc.fillRectangle(event.x, event.y, event.width, event.height);

	if (graph != null) {
		final Transform tf = new Transform(gc.getDevice());
		tf.translate(-offsetX, -offsetY);
		tf.scale(scale, scale);
		gc.setTransform(tf);

		if (graphNeedsLayout) {
			graph.layout();
			graphNeedsLayout = false;
		}

		graph.getNodes().forEach(n -> n.paint(gc));

		gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLUE));

		gc.setTransform(null);
		tf.dispose();

		if (hawkEye_active && hawkEye_target != null) {
			final int w = Math.round(getSize().x / hawkEye_oldScale * scale);
			final int h = Math.round(getSize().y / hawkEye_oldScale * scale);
			gc.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLUE));
			gc.setAlpha(64);
			gc.fillRectangle(
					(int) hawkEye_target.x - w / 2,
					(int) hawkEye_target.y - h / 2,
					w, h);
			gc.setAlpha(255);
		}
	}
}
 
Example 5
Source File: R31Enhance.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Equivalent to <code><b>GC.setTransform()</b></code> in r3.1.
 * 
 * @param gc
 * @param value
 */
static void setTransform( GC gc, Object value )
{
	if ( R31_AVAILABLE )
	{
		gc.setTransform( (Transform) value );
	}
}
 
Example 6
Source File: StatechartDefinitionSection.java    From statecharts with Eclipse Public License 1.0 5 votes vote down vote up
protected void drawIconBorder(Label icon, GC gc) {
	Rectangle rect = new Rectangle(0, 0, icon.getBounds().width - 1, icon.getBounds().height - 1);
	Transform t = new Transform(getDisplay());
	gc.setTransform(t);
	t.dispose();
	gc.setForeground(ColorConstants.lightGray);
	gc.drawRectangle(0, 0, rect.width, rect.height);
}
 
Example 7
Source File: TagCloud.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
private ImageData createImageData(final Word word, Font font, Point stringExtent, final double sin,
		final double cos, int x, int y, Color color) {
	Image img = new Image(null, x, y);
	word.width = x;
	word.height = y;
	word.stringExtent = stringExtent;
	GC g = new GC(img);
	g.setAntialias(antialias);
	g.setForeground(color);
	Transform t = new Transform(img.getDevice());
	if (word.angle < 0) {
		t.translate(0, img.getBounds().height - (int) (cos * stringExtent.y));
	} else {
		t.translate((int) (sin * stringExtent.y), 0);
	}
	t.rotate(word.angle);
	g.setTransform(t);
	g.setFont(font);
	// Why is drawString so slow? between 30 and 90 percent of the whole
	// draw time...
	g.drawString(word.string, 0, 0, false);
	int max = Math.max(x, y);
	int tmp = maxSize;
	while (max < tmp) {
		tmp = tmp / 2;
	}
	tmp = tmp * 2;
	SmallRect root = new SmallRect(0, 0, tmp, tmp);
	word.tree = new RectTree(root, accuracy);
	final ImageData id = img.getImageData();
	g.dispose();
	img.dispose();
	return id;
}
 
Example 8
Source File: RotatePiece.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
public void paint(GC gc, int x, int y) {
	Transform oldTransform = getOldTransform();
	gc.getTransform(oldTransform);

	Transform transform = getTransform();
	gc.getTransform(transform);
	transform.translate(x, y);
	rotateTransform(transform);
	gc.setTransform(transform);

	target.paint(gc, 0, 0);

	gc.setTransform(oldTransform);
}
 
Example 9
Source File: ScalePrint.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
public void paint(GC gc, int x, int y) {
	Transform oldTransform = getOldTransform();
	gc.getTransform(oldTransform);

	Transform transform = getTransform();
	gc.getTransform(transform);
	transform.translate(x, y);
	transform.scale((float) scale, (float) scale);
	gc.setTransform(transform);

	target.paint(gc, 0, 0);

	gc.setTransform(oldTransform);
}
 
Example 10
Source File: ToggleRenderer.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
private void paintMacTwistie(GC gc) {
	Transform transform = new Transform(gc.getDevice());
	transform.translate(getBounds().x, getBounds().y);
	gc.setTransform(transform);

	Color back = gc.getBackground();
	Color fore = gc.getForeground();

	if (!isHover()) {
		gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
	} else {
		gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_LIST_SELECTION));
	}

	gc.setBackground(gc.getForeground());
	if (isExpanded()) {
		gc.drawPolygon(new int[] { 1, 3, 4, 6, 5, 6, 8, 3 });
		gc.fillPolygon(new int[] { 1, 3, 4, 6, 5, 6, 8, 3 });
	} else {
		gc.drawPolygon(new int[] { 3, 1, 6, 4, 6, 5, 3, 8 });
		gc.fillPolygon(new int[] { 3, 1, 6, 4, 6, 5, 3, 8 });
	}

	if (isFocus()) {
		gc.setBackground(back);
		gc.setForeground(fore);
		gc.drawFocus(-1, -1, 12, 12);
	}

	gc.setTransform(null);
	transform.dispose();
}
 
Example 11
Source File: TreeNodeToggleRenderer.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
public void paint(GC gc, Object value)
{
    Transform transform = new Transform(gc.getDevice());
    transform.translate(getBounds().x, getBounds().y);
    gc.setTransform(transform);

    Color back = gc.getBackground();
    Color fore = gc.getForeground();

    gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
    gc.fillRectangle(0, 0, 8, 8);

    gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_WIDGET_FOREGROUND));
    gc.drawLine(2, 4, 6, 4);
    if (!isExpanded())
    {
        gc.drawLine(4, 2, 4, 6);
    }
    gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
    gc.drawRectangle(0, 0, 8, 8);

    if (isFocus())
    {
        gc.setBackground(back);
        gc.setForeground(fore);
        gc.drawFocus(-1, -1, 11, 11);
    }

    gc.setTransform(null);
    transform.dispose();
}
 
Example 12
Source File: ChevronsToggleRenderer.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
/** 
 * @see org.eclipse.nebula.widgets.pgroup.AbstractRenderer#paint(org.eclipse.swt.graphics.GC, java.lang.Object)
 */
public void paint(GC gc, Object value)
{
    Transform transform = new Transform(gc.getDevice());
    transform.translate(getBounds().x, getBounds().y);
    gc.setTransform(transform);

    Color back = gc.getBackground();
    Color fore = gc.getForeground();

    if (isHover())
    {
        Color old = gc.getForeground();
        gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
        gc.drawRoundRectangle(0, 0, 16, 15, 5, 5);
        gc.setForeground(old);
    }

    if (isExpanded())
    {
        gc.drawPolygon(new int[] {5, 6, 8, 3, 11, 6, 10, 6, 8, 4, 6, 6 });
        gc.drawPolygon(new int[] {5, 10, 8, 7, 11, 10, 10, 10, 8, 8, 6, 10 });
    }
    else
    {
        gc.drawPolygon(new int[] {5, 4, 8, 7, 11, 4, 10, 4, 8, 6, 6, 4 });
        gc.drawPolygon(new int[] {5, 8, 8, 11, 11, 8, 10, 8, 8, 10, 6, 8 });
    }

    if (isFocus())
    {
        gc.setBackground(back);
        gc.setForeground(fore);
        gc.drawFocus(2, 2, 13, 12);
    }

    gc.setTransform(null);
    transform.dispose();

}
 
Example 13
Source File: TwisteToggleRenderer.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
public void paint(GC gc, Object value)
{
    Transform transform = new Transform(gc.getDevice());
    transform.translate(getBounds().x, getBounds().y);
    gc.setTransform(transform);

    Color back = gc.getBackground();
    Color fore = gc.getForeground();

    if (!isHover())
    {
        gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
    }
    else
    {
        gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_LIST_SELECTION));
    }

    gc.setBackground(gc.getForeground());
    if (isExpanded())
    {
        gc.drawPolygon(new int[] {1, 3, 4, 6, 5, 6, 8, 3 });
        gc.fillPolygon(new int[] {1, 3, 4, 6, 5, 6, 8, 3 });
    }
    else
    {
        gc.drawPolygon(new int[] {3, 1, 6, 4, 6, 5, 3, 8 });
        gc.fillPolygon(new int[] {3, 1, 6, 4, 6, 5, 3, 8 });
    }

    if (isFocus())
    {
        gc.setBackground(back);
        gc.setForeground(fore);
        gc.drawFocus(-1, -1, 12, 12);
    }

    gc.setTransform(null);
    transform.dispose();
}
 
Example 14
Source File: MinMaxToggleRenderer.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
public void paint(GC gc, Object value)
{

    Transform transform = new Transform(gc.getDevice());
    transform.translate(getBounds().x, getBounds().y);
    gc.setTransform(transform);

    gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
    gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));

    if (isHover())
    {
        gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
        gc.drawRoundRectangle(0, 0, 17, 17, 5, 5);
    }

    gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
    gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));

    if (isExpanded())
    {
        gc.fillRectangle(4, 3, 9, 3);
        gc.drawRectangle(4, 3, 9, 3);
    }
    else
    {
        gc.fillRectangle(4, 3, 9, 9);
        gc.drawRectangle(4, 3, 9, 9);
        gc.drawLine(4, 5, 13, 5);
    }

    gc.setTransform(null);
    transform.dispose();

}
 
Example 15
Source File: GanttChartPrintJob.java    From nebula with Eclipse Public License 2.0 4 votes vote down vote up
public void run() {
	if (printer.startJob(jobName)) {
		GC gc = new GC(printer);

		int currentPage = 1;
		for (GanttChart ganttChart : this.ganttCharts) {
			
			Image printerImage = null;
			if (printer.getPrinterData().scope == PrinterData.SELECTION) {
				//the user selected to only print the selected area
				//as this is quite difficult in GanttChart, we specify that
				//this means to print the visible area
				//but it is possible to configure via settings what "visible"
				//area means: 
				// - really only the visible area horizontally and vertically
				// - only the horizontal visible area, but vertically everything
				printerImage = ganttChart.getSettings().printSelectedVerticallyComplete() ? 
						ganttChart.getGanttComposite().getVerticallyFullImage() : ganttChart.getGanttComposite().getImage();
			}
			else {
				printerImage = ganttChart.getGanttComposite().getFullImage();
			}
			
			final Rectangle printerClientArea = PrintUtils.computePrintArea(printer);
			final Point scaleFactor = PrintUtils.computeScaleFactor(printer);
			final Point pageCount = PrintUtils.getPageCount(printer, printerImage);

			// Print pages Left to Right and then Top to Down
			for (int verticalPageNumber = 0; verticalPageNumber < pageCount.y; verticalPageNumber++) {

				for (int horizontalPageNumber = 0; horizontalPageNumber < pageCount.x; horizontalPageNumber++) {

					// Calculate bounds for the next page
					int printerClientAreaHeight = ganttChart.getSettings().printFooter() ? 
							(printerClientArea.height - PrintUtils.FOOTER_HEIGHT_IN_PRINTER_DPI) : printerClientArea.height;
					Rectangle printBounds = new Rectangle((printerClientArea.width / scaleFactor.x) * horizontalPageNumber,
					                                      (printerClientAreaHeight / scaleFactor.y) * verticalPageNumber,
					                                      printerClientArea.width / scaleFactor.x,
					                                      printerClientAreaHeight / scaleFactor.y);

					if (shouldPrint(printer.getPrinterData(), currentPage)) {
						printer.startPage();

						Transform printerTransform = new Transform(printer);

						// Adjust for DPI difference between display and printer
						printerTransform.scale(scaleFactor.x, scaleFactor.y);

						// Adjust for margins
						printerTransform.translate(printerClientArea.x / scaleFactor.x, printerClientArea.y / scaleFactor.y);

						// GanttChart will not automatically print the pages at the left margin.
						// Example: page 1 will print at x = 0, page 2 at x = 100, page 3 at x = 300
						// Adjust to print from the left page margin. i.e x = 0
						printerTransform.translate(-1 * printBounds.x, -1 * printBounds.y);
						gc.setTransform(printerTransform);

						int imgWidthClipping = printBounds.width;
						if (((horizontalPageNumber * printBounds.width)+printBounds.width) > printerImage.getImageData().width) {
							imgWidthClipping = printerImage.getImageData().width - (horizontalPageNumber * printBounds.width);
						}
						
						int imgHeightClipping = printBounds.height;
						if (((verticalPageNumber * printBounds.height)+printBounds.height) > printerImage.getImageData().height) {
							imgHeightClipping = printerImage.getImageData().height - (verticalPageNumber * printBounds.height);
						}
						
						gc.drawImage(printerImage, 
								horizontalPageNumber * printBounds.width, 
								verticalPageNumber * printBounds.height, 
								imgWidthClipping, imgHeightClipping,
								printBounds.x, printBounds.y, imgWidthClipping, imgHeightClipping);
						
						if (ganttChart.getSettings().printFooter())
							printFooter(gc, ganttChart, currentPage, printBounds);

						printer.endPage();
						printerTransform.dispose();
						
					}
					currentPage++;
				}
			}
			
			printerImage.dispose();
		}
		
		printer.endJob();
		gc.dispose();
		
		//only dispose the printer after the print job is done if it is configured to do so
		//this configuration enables the possibility to reuse a printer for several jobs
		if (disposePrinter)
			printer.dispose();
	}
}
 
Example 16
Source File: SwtTextRenderer.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * @param ipr
 * @param lo
 * @param la
 * @param b
 */
private void showTopValue( IPrimitiveRenderer ipr, Location lo, Label la,
		boolean b )
{
	GC gc = (GC) ( (IDeviceRenderer) ipr ).getGraphicsContext( );
	IChartComputation cComp = ( (IDeviceRenderer) ipr ).getChartComputation( );
	double dX = lo.getX( ), dY = lo.getY( );
	final FontDefinition fd = la.getCaption( ).getFont( );

	final int dAngleInDegrees = (int)fd.getRotation( );

	final Color clrBackground = (Color) _sxs.getColor( la.getShadowColor( ) );

	final ITextMetrics itm = cComp.getTextMetrics( _sxs, la, 0 );
	final double dFW = itm.getFullWidth( );
	final double dFH = itm.getFullHeight( );
	cComp.recycleTextMetrics( itm );

	double scaledThickness = SHADOW_THICKNESS
			* _sxs.getDpiResolution( )
			/ 72d;

	gc.setBackground( clrBackground );

	R31Enhance.setAlpha( gc, la.getShadowColor( ) );

	// HORIZONTAL TEXT
	if ( dAngleInDegrees == 0 )
	{
		gc.fillRectangle( (int) ( dX + scaledThickness ),
				(int) ( dY + scaledThickness ),
				(int) dFW,
				(int) dFH );
	}
	// TEXT AT POSITIVE 90 (TOP RIGHT HIGHER THAN TOP LEFT CORNER)
	else if ( dAngleInDegrees == 90 )
	{
		gc.fillRectangle( (int) ( dX + scaledThickness ),
				(int) ( dY - dFW - scaledThickness ),
				(int) dFH,
				(int) dFW );
	}
	// TEXT AT NEGATIVE 90 (TOP RIGHT LOWER THAN TOP LEFT CORNER)
	else if ( dAngleInDegrees == -90 )
	{
		gc.fillRectangle( (int) ( dX - dFH - scaledThickness ),
				(int) ( dY + scaledThickness ),
				(int) dFH,
				(int) dFW );
	}
	else
	{
		Transform transform = new Transform( getDevice( ) );
		transform.translate( (float) dX, (float) dY );
		transform.rotate( -dAngleInDegrees );
		gc.setTransform( transform );
		gc.fillRectangle( (int) scaledThickness,
				(int) scaledThickness,
				(int) dFW,
				(int) dFH );
		transform.dispose( );
		gc.setTransform( null );
	}
}
 
Example 17
Source File: MinimapOverviewRuler.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Redraws the base image based on the StyledText contents.
 *
 * (i.e.: draw the lines)
 */
private void redrawBaseImage(Parameters parameters, IProgressMonitor monitor) {
    if (MinimapOverviewRulerPreferencesPage.getShowMinimapContents() && parameters.lineCount > 0
            && parameters.size.x > 0) {

        GC gc = parameters.gc;
        gc.setForeground(parameters.styledTextForeground);
        gc.setAlpha(200);

        gc.setTransform(parameters.transform);

        IOutlineModel outlineModel = fOutlineModel;

        int x1, x2, y, beginLine;
        if (outlineModel != null) {
            IParsedItem root = outlineModel.getRoot();
            if (root == null) {
                Log.log("Minimap overview ruler is trying to use outlineModel which was already disposed.");
                return;
            }
            IParsedItem[] children = root.getChildren();
            for (IParsedItem iParsedItem : children) {
                if (monitor.isCanceled()) {
                    return;
                }

                beginLine = iParsedItem.getBeginLine() - 1;
                y = (int) ((float) beginLine * parameters.imageHeight / parameters.lineCount);
                x1 = iParsedItem.getBeginCol();
                x2 = x1 + (iParsedItem.toString().length() * 5);
                gc.drawLine(x1, y, x2 - x1, y);

                IParsedItem[] children2 = iParsedItem.getChildren();
                for (IParsedItem iParsedItem2 : children2) {
                    if (monitor.isCanceled()) {
                        return;
                    }
                    beginLine = iParsedItem2.getBeginLine() - 1;
                    y = (int) ((float) beginLine * parameters.imageHeight / parameters.lineCount);
                    x1 = iParsedItem2.getBeginCol();
                    x2 = x1 + (iParsedItem2.toString().length() * 5);
                    gc.drawLine(x1, y, x2 - x1, y);

                }
            }

        }

        //This would draw the margin.
        //gc.setForeground(marginColor);
        //gc.setBackground(marginColor);
        //gc.drawLine(marginCols, 0, marginCols, imageHeight);
    }
}
 
Example 18
Source File: VerticalLabel.java    From bonita-studio with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Fix the issues in the ImageUtilities where the size of the image is the
 * ascent of the font instead of being its height.
 * 
 * Also uses the GC for the rotation.
 * 
 * The biggest issue is the very idea of using an image. The size of the
 * font should be given by the mapmode, not in absolute device pixel as it
 * does look ugly when zooming in.
 * 
 * 
 * @param string
 *            the String to be rendered
 * @param font
 *            the font
 * @param foreground
 *            the text's color
 * @param background
 *            the background color
 * @param useGCTransform
 *            true to use the Transform on the GC object.
 *            false to rely on the homemade algorithm. Transform seems to
 *            not be supported in eclipse-3.2 except on windows.
 *            It is working on macos-3.3M3.
 * @return an Image which must be disposed
 */
public Image createRotatedImageOfString(Graphics g, String string,
        Font font, Color foreground, Color background,
        boolean useGCTransform) {
    Display display = Display.getCurrent();
    if (display == null) {
        SWT.error(SWT.ERROR_THREAD_INVALID_ACCESS);
    }

    List<FontData> fontDatas = new ArrayList<FontData>();
    // take all font datas, mac and linux specific
    for (FontData data : font.getFontData()) {
        FontData data2 = new FontData(data.getName(),
                (int) (data.getHeight() * g.getAbsoluteScale()), data.getStyle());
        fontDatas.add(data2);
    }
    // create the new font
    Font zoomedFont = new Font(display, fontDatas.toArray(new FontData[fontDatas.size()]));
    // get the dimension in this font
    Dimension strDim = FigureUtilities
            .getTextExtents(string, zoomedFont);
    if (strDim.width == 0 || strDim.height == 0) {
        strDim = FigureUtilities
                .getTextExtents(string, font);
    }
    Image srcImage = useGCTransform ?
            new Image(display, strDim.height, strDim.width) :
            new Image(display, strDim.width, strDim.height);
    GC gc = new GC(srcImage);
    if (useGCTransform) {
        Transform transform = new Transform(display);
        transform.rotate(-90);

        gc.setTransform(transform);
    }
    gc.setFont(zoomedFont);
    gc.setForeground(foreground);
    gc.setBackground(background);
    gc.fillRectangle(gc.getClipping());
    gc.drawText(string, gc.getClipping().x, gc.getClipping().y
            // - metrics.getLeading()
            );
    gc.dispose();
    if (useGCTransform) {
        srcImage.dispose();
        zoomedFont.dispose();
        return srcImage;
    }
    disposeImage();
    Image rotated = ImageUtilities.createRotatedImage(srcImage);
    srcImage.dispose();
    zoomedFont.dispose();
    return rotated;
}
 
Example 19
Source File: ViewLattice.java    From arx with Apache License 2.0 4 votes vote down vote up
/**
 * Draws a node.
 *
 * @param g
 */
private void drawNodes(final GC g) {

    // Prepare
    Rectangle bounds = new Rectangle(0, 0, (int)nodeWidth, (int)nodeHeight);
    Transform transform = new Transform(g.getDevice());
    
    // Set style
    g.setLineWidth(STROKE_WIDTH_NODE);
    g.setFont(font);

    // Draw nodes
    for (List<ARXNode> level : lattice) {
        for (ARXNode node : level) {
            
            // Obtain coordinates
            double[] center = (double[]) node.getAttributes().get(ATTRIBUTE_CENTER);
            bounds.x = (int)(center[0] - nodeWidth / 2d);
            bounds.y = (int)(center[1] - nodeHeight / 2d);
            
            // Clipping
            if (bounds.intersects(new Rectangle(0, 0, screen.x, screen.y))) { 
                
                // Retrieve/compute text rendering data
                SerializablePath path = (SerializablePath) node.getAttributes().get(ATTRIBUTE_PATH);
                Point extent = (Point) node.getAttributes().get(ATTRIBUTE_EXTENT);
                if (path == null || path.getPath() == null) {
                    String text = (String) node.getAttributes().get(ATTRIBUTE_LABEL);
                    path = new SerializablePath(new Path(canvas.getDisplay()));
                    path.getPath().addString(text, 0, 0, font);
                    node.getAttributes().put(ATTRIBUTE_PATH, path);
                    extent = g.textExtent(text);
                    node.getAttributes().put(ATTRIBUTE_EXTENT, extent);
                }
        
                // Degrade if too far away
                if (bounds.width <= 4) {
                    g.setBackground(getInnerColor(node));
                    g.setAntialias(SWT.OFF);
                    g.fillRectangle(bounds.x, bounds.y, bounds.width, bounds.height);
        
                    // Draw real node
                } else {
                    
                    // Fill background
                    g.setBackground(getInnerColor(node));
                    g.setAntialias(SWT.OFF);
                    if (node != getSelectedNode()) {
                        g.fillOval(bounds.x, bounds.y, bounds.width, bounds.height);
                    } else {
                        g.fillRectangle(bounds.x, bounds.y, bounds.width, bounds.height);
                    }
                    
                    // Draw line
                    g.setLineWidth(getOuterStrokeWidth(node, bounds.width));
                    g.setForeground(getOuterColor(node));
                    g.setAntialias(SWT.ON);
                    if (node != getSelectedNode()) {
                        g.drawOval(bounds.x, bounds.y, bounds.width, bounds.height);
                    } else {
                        g.drawRectangle(bounds.x, bounds.y, bounds.width, bounds.height);
                    }
                    
                    // Draw text
                    if (bounds.width >= 20) {
                        
                        // Enable anti-aliasing
                        g.setTextAntialias(SWT.ON);
                        
                        // Compute position and factor
                        float factor1 = (bounds.width * 0.7f) / (float)extent.x;
                        float factor2 = (bounds.height * 0.7f) / (float)extent.y;
                        float factor = Math.min(factor1, factor2);
                        int positionX = bounds.x + (int)(((float)bounds.width - (float)extent.x * factor) / 2f); 
                        int positionY = bounds.y + (int)(((float)bounds.height - (float)extent.y * factor) / 2f);
                        
                        // Initialize transformation
                        transform.identity();
                        transform.translate(positionX, positionY);
                        transform.scale(factor, factor);
                        g.setTransform(transform);
                        
                        // Draw and reset
                        g.setBackground(COLOR_BLACK);
                        g.fillPath(path.getPath());
                        g.setTransform(null);
                        g.setTextAntialias(SWT.OFF);
                    }
                }
            }
        }
    }
    
    // Clean up
    transform.dispose();
}