Java Code Examples for javafx.scene.canvas.GraphicsContext#translate()

The following examples show how to use javafx.scene.canvas.GraphicsContext#translate() . 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: GuiUtils.java    From CircuitSim with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Source orientation is assumed EAST
 */
public static void rotateGraphics(GuiElement element, GraphicsContext graphics, Direction direction) {
	int x = element.getScreenX();
	int y = element.getScreenY();
	int width = element.getScreenWidth();
	int height = element.getScreenHeight();
	
	graphics.translate(x + width * 0.5, y + height * 0.5);
	switch(direction) {
		case NORTH:
			graphics.rotate(270);
			graphics.translate(-x - height * 0.5, -y - width * 0.5);
			break;
		case SOUTH:
			graphics.rotate(90);
			graphics.translate(-x - height * 0.5, -y - width * 0.5);
			break;
		case WEST:
			graphics.rotate(180);
		default:
			graphics.translate(-x - width * 0.5, -y - height * 0.5);
	}
}
 
Example 2
Source File: ConcentricRingChart.java    From charts with Apache License 2.0 6 votes vote down vote up
private void drawTextAlongArc(final GraphicsContext CTX, final String TEXT, final double CENTER_X, final double CENTER_Y, final double RADIUS, final double ANGLE){
    int    length     = TEXT.length();
    double charSpacer = (7 / RADIUS) * size * 0.13;
    double textAngle  = (charSpacer * (length + 0.5));
    if (ANGLE > textAngle) {
        CTX.save();
        CTX.translate(CENTER_X, CENTER_Y);
        CTX.rotate(ANGLE - (charSpacer * (length + 0.5)));
        for (int i = 0; i < length; i++) {
            CTX.save();
            CTX.translate(0, -1 * RADIUS);
            char c = TEXT.charAt(i);
            CTX.fillText(Character.toString(c), 0, 0);
            CTX.restore();
            CTX.rotate(charSpacer);
        }
        CTX.restore();
    }
}
 
Example 3
Source File: StateTransitionEdgeViewer.java    From JetUML with GNU General Public License v3.0 6 votes vote down vote up
private void drawLabel(StateTransitionEdge pEdge, GraphicsContext pGraphics)
{
	adjustLabelFont(pEdge);
	Rectangle2D labelBounds = getLabelBounds(pEdge);
	double x = labelBounds.getMinX();
	double y = labelBounds.getMinY();
	
	Paint oldFill = pGraphics.getFill();
	Font oldFont = pGraphics.getFont();
	pGraphics.translate(x, y);
	pGraphics.setFill(Color.BLACK);
	pGraphics.setFont(aFont);
	pGraphics.setTextAlign(TextAlignment.CENTER);
	pGraphics.fillText(pEdge.getMiddleLabel(), labelBounds.getWidth()/2, 0);
	pGraphics.setFill(oldFill);
	pGraphics.setFont(oldFont);
	pGraphics.translate(-x, -y);        
}
 
Example 4
Source File: AbstractNodeViewer.java    From JetUML with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Canvas createIcon(Node pNode)
{
	Rectangle bounds = getBounds(pNode);
	int width = bounds.getWidth();
	int height = bounds.getHeight();
	double scaleX = (BUTTON_SIZE - OFFSET)/ (double) width;
	double scaleY = (BUTTON_SIZE - OFFSET)/ (double) height;
	double scale = Math.min(scaleX, scaleY);
	Canvas canvas = new Canvas(BUTTON_SIZE, BUTTON_SIZE);
	GraphicsContext graphics = canvas.getGraphicsContext2D();
	graphics.scale(scale, scale);
	graphics.translate(Math.max((height - width) / 2, 0), Math.max((width - height) / 2, 0));
	graphics.setFill(Color.WHITE);
	graphics.setStroke(Color.BLACK);
	draw(pNode, canvas.getGraphicsContext2D());
	return canvas;
}
 
Example 5
Source File: AbstractAxis.java    From chart-fx with Apache License 2.0 6 votes vote down vote up
protected static void drawTickMarkLabel(final GraphicsContext gc, final double x, final double y,
        final double scaleFont, final TickMark tickMark) {
    gc.save();

    gc.setFont(tickMark.getFont());
    gc.setFill(tickMark.getFill());
    gc.setTextAlign(tickMark.getTextAlignment());
    gc.setTextBaseline(tickMark.getTextOrigin());

    gc.translate(x, y);
    if (tickMark.getRotate() != 0.0) {
        gc.rotate(tickMark.getRotate());
    }
    gc.setGlobalAlpha(tickMark.getOpacity());
    if (scaleFont != 1.0) {
        gc.scale(scaleFont, 1.0);
    }

    gc.fillText(tickMark.getText(), 0, 0);
    // gc.fillText(tickMark.getText(), x, y);C
    gc.restore();
}
 
Example 6
Source File: RoundLcdClock.java    From Enzo with Apache License 2.0 5 votes vote down vote up
private void drawAlarmIcon(final GraphicsContext CTX, final Paint COLOR) {
    double iconSize = 0.1 * size;
    CTX.save();
    CTX.translate((size - iconSize) * 0.5, size * 0.25);
    CTX.beginPath();
    CTX.moveTo(0.6875 * iconSize, 0.875 * iconSize);
    CTX.bezierCurveTo(0.625 * iconSize, 0.9375 * iconSize, 0.5625 * iconSize, iconSize, 0.5 * iconSize, iconSize);
    CTX.bezierCurveTo(0.4375 * iconSize, iconSize, 0.375 * iconSize, 0.9375 * iconSize, 0.375 * iconSize, 0.875 * iconSize);
    CTX.bezierCurveTo(0.375 * iconSize, 0.875 * iconSize, 0.6875 * iconSize, 0.875 * iconSize, 0.6875 * iconSize, 0.875 * iconSize);
    CTX.closePath();
    CTX.moveTo(iconSize, 0.8125 * iconSize);
    CTX.bezierCurveTo(0.6875 * iconSize, 0.5625 * iconSize, 0.9375 * iconSize, 0.0, 0.5 * iconSize, 0.0);
    CTX.bezierCurveTo(0.5 * iconSize, 0.0, 0.5 * iconSize, 0.0, 0.5 * iconSize, 0.0);
    CTX.bezierCurveTo(0.5 * iconSize, 0.0, 0.5 * iconSize, 0.0, 0.5 * iconSize, 0.0);
    CTX.bezierCurveTo(0.125 * iconSize, 0.0, 0.375 * iconSize, 0.5625 * iconSize, 0.0, 0.8125 * iconSize);
    CTX.bezierCurveTo(0.0, 0.8125 * iconSize, 0.0, 0.8125 * iconSize, 0.0, 0.8125 * iconSize);
    CTX.bezierCurveTo(0.0, 0.8125 * iconSize, 0.0, 0.8125 * iconSize, 0.0, 0.8125 * iconSize);
    CTX.bezierCurveTo(0.0, 0.8125 * iconSize, 0.0, 0.8125 * iconSize, 0.0625 * iconSize, 0.8125 * iconSize);
    CTX.bezierCurveTo(0.0625 * iconSize, 0.8125 * iconSize, 0.5 * iconSize, 0.8125 * iconSize, 0.5 * iconSize, 0.8125 * iconSize);
    CTX.bezierCurveTo(0.5 * iconSize, 0.8125 * iconSize, iconSize, 0.8125 * iconSize, iconSize, 0.8125 * iconSize);
    CTX.bezierCurveTo(iconSize, 0.8125 * iconSize, iconSize, 0.8125 * iconSize, iconSize, 0.8125 * iconSize);
    CTX.bezierCurveTo(iconSize, 0.8125 * iconSize, iconSize, 0.8125 * iconSize, iconSize, 0.8125 * iconSize);
    CTX.bezierCurveTo(iconSize, 0.8125 * iconSize, iconSize, 0.8125 * iconSize, iconSize, 0.8125 * iconSize);
    CTX.closePath();
    CTX.setFill(COLOR);
    CTX.fill();
    CTX.restore();    
}
 
Example 7
Source File: MiscHelpers.java    From arma-dialog-creator with MIT License 5 votes vote down vote up
public static void paintRotatedImage(@NotNull GraphicsContext gc, @NotNull Image img, int x, int y, int w, int h, double rotateDeg) {
	gc.save();
	gc.translate(x + w / 2, y + h / 2); //move to center
	gc.rotate(rotateDeg);
	gc.drawImage(img, -w / 2, -h / 2, w, h);
	gc.restore();
}
 
Example 8
Source File: TintedImageHelperRenderer.java    From arma-dialog-creator with MIT License 5 votes vote down vote up
/** This method applies to {@link #setToPreviewMode(boolean)} */
public void paintTintedImage(@NotNull GraphicsContext gc) {
	gc.save();
	if (rotated) {
		gc.translate(x + w / 2, y + h / 2); //move to center
		gc.rotate(rotateDeg);
	}
	if (flipped) {
		gc.scale(-1, 1);
	}
	gc.setFill(Color.TRANSPARENT);
	gc.setEffect(previewMode ? effect2 : effect1);
	gc.fillRect(0, 0, 1, 1); //this is just to trigger drawing the effect. Won't draw anything itself
	gc.restore();
}
 
Example 9
Source File: AbstractAxis.java    From chart-fx with Apache License 2.0 5 votes vote down vote up
protected static void drawAxisLabel(final GraphicsContext gc, final double x, final double y, final Text label) {
    gc.save();
    gc.setTextAlign(label.getTextAlignment());
    gc.setFont(label.getFont());
    gc.setFill(label.getFill());
    gc.setStroke(label.getStroke());
    gc.setLineWidth(label.getStrokeWidth());
    gc.translate(x, y);
    gc.rotate(label.getRotate());
    gc.fillText(label.getText(), 0, 0);
    gc.restore();
}
 
Example 10
Source File: AmpSkin.java    From Medusa with Apache License 2.0 4 votes vote down vote up
private void drawTickMarks(final GraphicsContext CTX) {
    double     sinValue;
    double     cosValue;
    double     orthText         = TickLabelOrientation.ORTHOGONAL == gauge.getTickLabelOrientation() ? 0.51 : 0.52;
    Point2D    center           = new Point2D(width * 0.5, height * 0.77);
    double     minorTickSpace   = gauge.getMinorTickSpace();
    double     minValue         = gauge.getMinValue();
    //double     maxValue         = gauge.getMaxValue();
    double     tmpAngleStep     = angleStep * minorTickSpace;
    int        decimals         = gauge.getTickLabelDecimals();
    BigDecimal minorTickSpaceBD = BigDecimal.valueOf(minorTickSpace);
    BigDecimal majorTickSpaceBD = BigDecimal.valueOf(gauge.getMajorTickSpace());
    BigDecimal mediumCheck2     = BigDecimal.valueOf(2 * minorTickSpace);
    BigDecimal mediumCheck5     = BigDecimal.valueOf(5 * minorTickSpace);
    BigDecimal counterBD        = BigDecimal.valueOf(minValue);
    double     counter          = minValue;

    Font tickLabelFont = Fonts.robotoCondensedRegular((decimals == 0 ? 0.045 : 0.038) * height);
    CTX.setFont(tickLabelFont);

    for (double angle = 0 ; Double.compare(-ANGLE_RANGE - tmpAngleStep, angle) < 0 ; angle -= tmpAngleStep) {
        sinValue = Math.sin(Math.toRadians(angle + START_ANGLE));
        cosValue = Math.cos(Math.toRadians(angle + START_ANGLE));

        Point2D innerPoint       = new Point2D(center.getX() + width * 0.41987097 * sinValue, center.getY() + width * 0.41987097 * cosValue);
        Point2D outerMinorPoint  = new Point2D(center.getX() + width * 0.45387097 * sinValue, center.getY() + width * 0.45387097 * cosValue);
        Point2D outerMediumPoint = new Point2D(center.getX() + width * 0.46387097 * sinValue, center.getY() + width * 0.46387097 * cosValue);
        Point2D outerPoint       = new Point2D(center.getX() + width * 0.48387097 * sinValue, center.getY() + width * 0.48387097 * cosValue);
        Point2D textPoint        = new Point2D(center.getX() + width * orthText * sinValue, center.getY() + width * orthText * cosValue);

        CTX.setStroke(gauge.getTickMarkColor());
        if (Double.compare(counterBD.remainder(majorTickSpaceBD).doubleValue(), 0.0) == 0) {
            // Draw major tickmark
            CTX.setLineWidth(height * 0.0055);
            CTX.strokeLine(innerPoint.getX(), innerPoint.getY(), outerPoint.getX(), outerPoint.getY());

            // Draw text
            CTX.save();
            CTX.translate(textPoint.getX(), textPoint.getY());
            switch(gauge.getTickLabelOrientation()) {
                case ORTHOGONAL:
                    if ((360 - START_ANGLE - angle) % 360 > 90 && (360 - START_ANGLE - angle) % 360 < 270) {
                        CTX.rotate((180 - START_ANGLE - angle) % 360);
                    } else {
                        CTX.rotate((360 - START_ANGLE - angle) % 360);
                    }
                    break;
                case TANGENT:
                    if ((360 - START_ANGLE - angle - 90) % 360 > 90 && (360 - START_ANGLE - angle - 90) % 360 < 270) {
                        CTX.rotate((90 - START_ANGLE - angle) % 360);
                    } else {
                        CTX.rotate((270 - START_ANGLE - angle) % 360);
                    }
                    break;
                case HORIZONTAL:
                default:
                    break;
            }
            CTX.setTextAlign(TextAlignment.CENTER);
            CTX.setTextBaseline(VPos.CENTER);
            CTX.setFill(gauge.getTickLabelColor());
            CTX.fillText(String.format(locale, "%." + decimals + "f", counter), 0, 0);
            CTX.restore();
        } else if (gauge.getMediumTickMarksVisible() &&
                   Double.compare(minorTickSpaceBD.remainder(mediumCheck2).doubleValue(), 0.0) != 0.0 &&
                   Double.compare(counterBD.remainder(mediumCheck5).doubleValue(), 0.0) == 0.0) {
            CTX.setLineWidth(height * 0.0035);
            CTX.strokeLine(innerPoint.getX(), innerPoint.getY(), outerMediumPoint.getX(), outerMediumPoint.getY());
        } else if (gauge.getMinorTickMarksVisible() && Double.compare(counterBD.remainder(minorTickSpaceBD).doubleValue(), 0.0) == 0) {
            CTX.setLineWidth(height * 0.00225);
            CTX.strokeLine(innerPoint.getX(), innerPoint.getY(), outerMinorPoint.getX(), outerMinorPoint.getY());
        }
        counterBD = counterBD.add(minorTickSpaceBD);
        counter   = counterBD.doubleValue();
    }
}
 
Example 11
Source File: ModernSkin.java    From Medusa with Apache License 2.0 4 votes vote down vote up
private void drawTickMarks(final GraphicsContext CTX) {
    double               sinValue;
    double               cosValue;
    double               centerX                = size * 0.5;
    double               centerY                = size * 0.5;
    double               minorTickSpace         = gauge.getMinorTickSpace();
    double               minValue               = gauge.getMinValue();
    double               maxValue               = gauge.getMaxValue();
    double               tmpAngleStep           = angleStep * minorTickSpace;
    int                  decimals               = gauge.getTickLabelDecimals();
    BigDecimal           minorTickSpaceBD       = BigDecimal.valueOf(minorTickSpace);
    BigDecimal           majorTickSpaceBD       = BigDecimal.valueOf(gauge.getMajorTickSpace());
    BigDecimal           mediumCheck2           = BigDecimal.valueOf(2 * minorTickSpace);
    BigDecimal           mediumCheck5           = BigDecimal.valueOf(5 * minorTickSpace);
    BigDecimal           counterBD              = BigDecimal.valueOf(minValue);
    double               counter                = minValue;
    boolean              majorTickMarksVisible  = gauge.getMajorTickMarksVisible();
    boolean              mediumTickMarksVisible = gauge.getMediumTickMarksVisible();
    boolean              tickLabelsVisible      = gauge.getTickLabelsVisible();
    TickLabelOrientation tickLabelOrientation   = gauge.getTickLabelOrientation();
    Color                tickMarkColor          = gauge.getTickMarkColor();
    Color                majorTickMarkColor     = tickMarkColor;
    Color                mediumTickMarkColor    = tickMarkColor;
    Color                tickLabelColor         = gauge.getTickLabelColor();

    double innerPointX;
    double innerPointY;
    double outerPointX;
    double outerPointY;
    double innerMediumPointX;
    double innerMediumPointY;
    double outerMediumPointX;
    double outerMediumPointY;
    double textPointX;
    double textPointY;

    double orthTextFactor = 0.46; //TickLabelOrientation.ORTHOGONAL == gauge.getTickLabelOrientation() ? 0.46 : 0.46;

    Font tickLabelFont = Fonts.robotoCondensedLight((decimals == 0 ? 0.047 : 0.040) * size);
    CTX.setFont(tickLabelFont);
    CTX.setTextAlign(TextAlignment.CENTER);
    CTX.setTextBaseline(VPos.CENTER);

    CTX.setLineCap(StrokeLineCap.BUTT);
    CTX.setLineWidth(size * 0.0035);
    for (double angle = 0 ; Double.compare(-ANGLE_RANGE - tmpAngleStep, angle) < 0 ; angle -= tmpAngleStep) {
        sinValue = Math.sin(Math.toRadians(angle + START_ANGLE));
        cosValue = Math.cos(Math.toRadians(angle + START_ANGLE));

        innerPointX       = centerX + size * 0.375 * sinValue;
        innerPointY       = centerY + size * 0.375 * cosValue;
        outerPointX       = centerX + size * 0.425 * sinValue;
        outerPointY       = centerY + size * 0.425 * cosValue;
        innerMediumPointX = centerX + size * 0.35 * sinValue;
        innerMediumPointY = centerY + size * 0.35 * cosValue;
        outerMediumPointX = centerX + size * 0.4 * sinValue;
        outerMediumPointY = centerY + size * 0.4 * cosValue;
        textPointX        = centerX + size * orthTextFactor * sinValue;
        textPointY        = centerY + size * orthTextFactor * cosValue;

        // Set the general tickmark color
        CTX.setStroke(tickMarkColor);

        if (Double.compare(counterBD.remainder(majorTickSpaceBD).doubleValue(), 0.0) == 0) {
            // Draw major tick mark
            if (majorTickMarksVisible) {
                CTX.setFill(majorTickMarkColor);
                CTX.setStroke(majorTickMarkColor);
                CTX.strokeLine(innerPointX, innerPointY, outerPointX, outerPointY);
            }
            // Draw tick label text
            if (tickLabelsVisible) {
                CTX.save();
                CTX.translate(textPointX, textPointY);

                Helper.rotateContextForText(CTX, START_ANGLE, angle, tickLabelOrientation);

                CTX.setFill(tickLabelColor);
                if (TickLabelOrientation.HORIZONTAL == tickLabelOrientation &&
                    (Double.compare(counter, minValue) == 0 ||
                    Double.compare(counter, maxValue) == 0)) {
                        CTX.setFill(Color.TRANSPARENT);
                }
                CTX.fillText(String.format(locale, "%." + decimals + "f", counter), 0, 0);
                CTX.restore();
            }
        } else if (mediumTickMarksVisible &&
                   Double.compare(minorTickSpaceBD.remainder(mediumCheck2).doubleValue(), 0.0) != 0.0 &&
                   Double.compare(counterBD.remainder(mediumCheck5).doubleValue(), 0.0) == 0.0) {
            // Draw medium tick mark
            CTX.setFill(mediumTickMarkColor);
            CTX.setStroke(mediumTickMarkColor);
            CTX.strokeLine(innerMediumPointX, innerMediumPointY, outerMediumPointX, outerMediumPointY);
        }
        counterBD = counterBD.add(minorTickSpaceBD);
        counter   = counterBD.doubleValue();
    }
}
 
Example 12
Source File: GaugeSkin.java    From Enzo with Apache License 2.0 4 votes vote down vote up
private void drawTickMarks(final GraphicsContext CTX) {
    if (getSkinnable().isHistogramEnabled()) {
        double xy;
        double wh;
        double step         = 0;
        double OFFSET       = 90 - getSkinnable().getStartAngle();
        double ANGLE_EXTEND = (getSkinnable().getMaxValue()) * angleStep;
        CTX.setStroke(Color.rgb(200, 200, 200));
        CTX.setLineWidth(size * 0.001);
        CTX.setLineCap(StrokeLineCap.BUTT);
        for (int i = 0 ; i < 5 ; i++) {
            xy = (size - (0.435 + step) * size) / 2;
            wh = size * (0.435 + step);
            CTX.strokeArc(xy, xy, wh, wh, -OFFSET, -ANGLE_EXTEND, ArcType.OPEN);
            step += 0.075;
        }
    }

    double  sinValue;
    double  cosValue;
    double  startAngle = getSkinnable().getStartAngle();
    double  orthText   = Gauge.TickLabelOrientation.ORTHOGONAL == getSkinnable().getTickLabelOrientation() ? 0.33 : 0.31;
    Point2D center     = new Point2D(size * 0.5, size * 0.5);
    for (double angle = 0, counter = getSkinnable().getMinValue() ; Double.compare(counter, getSkinnable().getMaxValue()) <= 0 ; angle -= angleStep, counter++) {
        sinValue = Math.sin(Math.toRadians(angle + startAngle));
        cosValue = Math.cos(Math.toRadians(angle + startAngle));

        Point2D innerMainPoint   = new Point2D(center.getX() + size * 0.368 * sinValue, center.getY() + size * 0.368 * cosValue);
        Point2D innerMediumPoint = new Point2D(center.getX() + size * 0.388 * sinValue, center.getY() + size * 0.388 * cosValue);
        Point2D innerMinorPoint  = new Point2D(center.getX() + size * 0.3975 * sinValue, center.getY() + size * 0.3975 * cosValue);
        Point2D outerPoint       = new Point2D(center.getX() + size * 0.432 * sinValue, center.getY() + size * 0.432 * cosValue);
        Point2D textPoint        = new Point2D(center.getX() + size * orthText * sinValue, center.getY() + size * orthText * cosValue);

        CTX.setStroke(getSkinnable().getTickMarkFill());
        if (counter % getSkinnable().getMajorTickSpace() == 0) {
            // Draw major tickmark
            CTX.setLineWidth(size * 0.0055);
            CTX.strokeLine(innerMainPoint.getX(), innerMainPoint.getY(), outerPoint.getX(), outerPoint.getY());

            // Draw text
            CTX.save();
            CTX.translate(textPoint.getX(), textPoint.getY());
            switch(getSkinnable().getTickLabelOrientation()) {
                case ORTHOGONAL:
                    if ((360 - startAngle - angle) % 360 > 90 && (360 - startAngle - angle) % 360 < 270) {
                        CTX.rotate((180 - startAngle - angle) % 360);
                    } else {
                        CTX.rotate((360 - startAngle - angle) % 360);
                    }
                    break;
                case TANGENT:
                    if ((360 - startAngle - angle - 90) % 360 > 90 && (360 - startAngle - angle - 90) % 360 < 270) {
                        CTX.rotate((90 - startAngle - angle) % 360);
                    } else {
                        CTX.rotate((270 - startAngle - angle) % 360);
                    }
                    break;
                case HORIZONTAL:
                default:
                    break;
            }
            CTX.setFont(Font.font("Verdana", FontWeight.NORMAL, 0.045 * size));
            CTX.setTextAlign(TextAlignment.CENTER);
            CTX.setTextBaseline(VPos.CENTER);
            CTX.setFill(getSkinnable().getTickLabelFill());
            CTX.fillText(Integer.toString((int) counter), 0, 0);
            CTX.restore();
        } else if (getSkinnable().getMinorTickSpace() % 2 != 0 && counter % 5 == 0) {
            CTX.setLineWidth(size * 0.0035);
            CTX.strokeLine(innerMediumPoint.getX(), innerMediumPoint.getY(), outerPoint.getX(), outerPoint.getY());
        } else if (counter % getSkinnable().getMinorTickSpace() == 0) {
            CTX.setLineWidth(size * 0.00225);
            CTX.strokeLine(innerMinorPoint.getX(), innerMinorPoint.getY(), outerPoint.getX(), outerPoint.getY());
        }
    }
}
 
Example 13
Source File: Helper.java    From charts with Apache License 2.0 4 votes vote down vote up
public static final void rotateCtx(final GraphicsContext CTX, final double X, final double Y, final double ANGLE) {
    CTX.translate(X, Y);
    CTX.rotate(ANGLE);
    CTX.translate(-X, -Y);
}
 
Example 14
Source File: StringViewer.java    From JetUML with GNU General Public License v3.0 4 votes vote down vote up
/**
    * Draws the string inside a given rectangle.
    * @param pString The string to draw.
    * @param pGraphics the graphics context
    * @param pRectangle the rectangle into which to place the string
 */
public void draw(String pString, GraphicsContext pGraphics, Rectangle pRectangle)
{
	Text label = getLabel(pString);
	
	pGraphics.setTextAlign(label.getTextAlignment());
	
	int textX = 0;
	int textY = 0;
	if(aAlignment == Align.CENTER) 
	{
		textX = pRectangle.getWidth()/2;
		textY = pRectangle.getHeight()/2;
		pGraphics.setTextBaseline(VPos.CENTER);
	}
	else
	{
		pGraphics.setTextBaseline(VPos.TOP);
		textX = HORIZONTAL_TEXT_PADDING;
	}
	
	pGraphics.translate(pRectangle.getX(), pRectangle.getY());
	ViewUtils.drawText(pGraphics, textX, textY, pString.trim(), getFont());
	
	if(aUnderlined && pString.trim().length() > 0)
	{
		int xOffset = 0;
		int yOffset = 0;
		Bounds bounds = label.getLayoutBounds();
		if(aAlignment == Align.CENTER)
		{
			xOffset = (int) (bounds.getWidth()/2);
			yOffset = (int) (getFont().getSize()/2) + 1;
		}
		else if(aAlignment == Align.RIGHT)
		{
			xOffset = (int) bounds.getWidth();
		}
		
		ViewUtils.drawLine(pGraphics, textX-xOffset, textY+yOffset, 
				(int) (textX-xOffset+bounds.getWidth()), textY+yOffset, LineStyle.SOLID);
	}
	pGraphics.translate(-pRectangle.getX(), -pRectangle.getY());
}
 
Example 15
Source File: ContourDataSetRenderer.java    From chart-fx with Apache License 2.0 4 votes vote down vote up
private void drawContour(final GraphicsContext gc, final ContourDataSetCache lCache) {
    final double[] levels = new double[getNumberQuantisationLevels()];
    for (int i = 0; i < levels.length; i++) {
        levels[i] = (i + 1) / (double) levels.length;
    }

    final int xSize = lCache.xSize;
    final int ySize = lCache.ySize;
    final double[][] data = new double[ySize][xSize];
    for (int yIndex = 0; yIndex < ySize; yIndex++) {
        for (int xIndex = 0; xIndex < xSize; xIndex++) {
            final double offset = lCache.reduced[yIndex * xSize + xIndex];
            data[ySize - 1 - yIndex][xIndex] = offset;
        }
    }

    // abort if min/max == 0 -> cannot compute contours
    final double zRange = Math.abs(lCache.zMax - lCache.zMin);
    if (zRange <= 0) {
        return;
    }

    final ColorGradient colorGradient = getColorGradient();
    final MarchingSquares marchingSquares = new MarchingSquares();
    final double scaleX = lCache.xDataPixelRange / xSize;
    final double scaleY = lCache.yDataPixelRange / ySize;
    gc.save();
    gc.translate(lCache.xDataPixelMin, lCache.yDataPixelMin);
    gc.scale(scaleX, scaleY);
    final GeneralPath[] isolines;
    try {
        isolines = marchingSquares.buildContours(data, levels);
        int levelCount = 0;
        for (final GeneralPath path : isolines) {
            if (path.size() > getMaxContourSegments()) {
                levelCount++;
                continue;
            }
            final Color color = lCache.zInverted ? colorGradient.getColor(1 - levels[levelCount++])
                                                 : colorGradient.getColor(levels[levelCount++]);
            gc.setStroke(color);
            gc.setLineDashes(1.0);
            gc.setMiterLimit(10);
            gc.setFill(color);
            gc.setLineWidth(0.5);
            path.draw(gc);
        }
    } catch (InterruptedException | ExecutionException e) {
        if (LOGGER.isErrorEnabled()) {
            LOGGER.atError().setCause(e).log("marchingSquares algorithm");
        }
    } finally {
        gc.restore();
    }
}
 
Example 16
Source File: FxNinePatch.java    From NinePatch with Apache License 2.0 4 votes vote down vote up
@Override
public void translate(GraphicsContext g2d, int x, int y)
{
    g2d.translate(x, y);
}
 
Example 17
Source File: PriorityEncoderPeer.java    From CircuitSim with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void paint(GraphicsContext graphics, CircuitState circuitState) {
	GuiUtils.drawName(graphics, this, getProperties().getValue(Properties.LABEL_LOCATION));

	Direction direction = getProperties().getValue(Properties.DIRECTION);
	graphics.translate(getScreenX(), getScreenY());

	int height;
	int width;

	int inputSideLength = ((1 << getComponent().getNumSelectBits()) + 1) * GuiUtils.BLOCK_SIZE;
	int enabledSideLength = ENABLED_INOUT_SIDE_LEN * GuiUtils.BLOCK_SIZE;

	double zeroX = GuiUtils.BLOCK_SIZE >> 1;
	double zeroY;

	if(direction == Direction.EAST || direction == Direction.WEST) {
		height = inputSideLength;
		width = enabledSideLength;
		zeroY = GuiUtils.BLOCK_SIZE * 1.5;
		if(direction == Direction.WEST) {
			zeroX = width - GuiUtils.BLOCK_SIZE;
		}
	} else {
		height = enabledSideLength;
		width = inputSideLength;
		zeroY = height - (GuiUtils.BLOCK_SIZE >> 1);
		if(direction == Direction.SOUTH) {
			zeroY = GuiUtils.BLOCK_SIZE * 1.5;
		}
	}

	graphics.setStroke(Color.BLACK);
	graphics.strokeRect(0, 0, width, height);

	graphics.setFill(Color.WHITE);
	graphics.fillRect(0, 0, width, height);

	graphics.setFill(Color.DARKGRAY);
	graphics.fillText("0", zeroX, zeroY);

	graphics.setFill(Color.BLACK);
	graphics.fillText("Pri", (width >> 1) - graphics.getFont().getSize(), (height >> 1) + 0.5 * GuiUtils.BLOCK_SIZE);
}
 
Example 18
Source File: LabelledMarkerRenderer.java    From chart-fx with Apache License 2.0 4 votes vote down vote up
/**
 * Draws horizontal markers with horizontal (default) labels attached to the top
 *
 * @param gc the graphics context from the Canvas parent
 * @param chart instance of the calling chart
 * @param dataSet instance of the data set that is supposed to be drawn
 * @param indexMin minimum index of data set to be drawn
 * @param indexMax maximum index of data set to be drawn
 */
protected void drawHorizontalLabelledMarker(final GraphicsContext gc, final XYChart chart, final DataSet dataSet,
        final int indexMin, final int indexMax) {
    final Axis yAxis = this.getFirstAxis(Orientation.VERTICAL, chart);

    gc.save();
    setGraphicsContextAttributes(gc, dataSet.getStyle());
    gc.setTextAlign(TextAlignment.RIGHT);

    final double width = chart.getCanvas().getWidth();
    double lastLabel = -Double.MAX_VALUE;
    double lastFontSize = 0;
    for (int i = indexMin; i < indexMax; i++) {
        final double screenY = (int) yAxis.getDisplayPosition(dataSet.get(DataSet.DIM_Y, i));
        final String label = dataSet.getDataLabel(i);
        if (label == null) {
            continue;
        }

        final String pointStyle = dataSet.getStyle(i);
        if (pointStyle != null) {
            gc.save();
            setGraphicsContextAttributes(gc, pointStyle);
        }

        gc.strokeLine(0, screenY, width, screenY);

        if (Math.abs(screenY - lastLabel) > lastFontSize && !label.isEmpty()) {
            gc.save();
            gc.setLineWidth(0.8);
            gc.setLineDashes(1.0);
            gc.translate(Math.ceil(screenY + 3), Math.ceil(0.99 * width));
            gc.fillText(label, 0.0, 0);
            gc.restore();
            lastLabel = screenY;
            lastFontSize = gc.getFont().getSize();
        }

        if (pointStyle != null) {
            gc.restore();
        }
    }
    gc.restore();
}
 
Example 19
Source File: DecoderPeer.java    From CircuitSim with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void paint(GraphicsContext graphics, CircuitState circuitState) {
	GuiUtils.drawName(graphics, this, getProperties().getValue(Properties.LABEL_LOCATION));
	Direction direction = getProperties().getValue(Properties.DIRECTION);
	
	int x = getScreenX();
	int y = getScreenY();
	int width = 3 * GuiUtils.BLOCK_SIZE;
	int height = (getComponent().getNumOutputs() + 2) * GuiUtils.BLOCK_SIZE;
	
	int zeroXOffset = 0;
	
	switch(direction) {
		case SOUTH:
			graphics.translate(x, y);
			graphics.rotate(270);
			graphics.translate(-x - width, -y);
		case WEST:
			zeroXOffset = 2;
			graphics.beginPath();
			graphics.moveTo(x, y);
			graphics.lineTo(x + width, y + Math.min(20, height * 0.2));
			graphics.lineTo(x + width, y + height - Math.min(20, height * 0.2));
			graphics.lineTo(x, y + height);
			graphics.closePath();
			break;
		case NORTH:
			graphics.translate(x, y);
			graphics.rotate(270);
			graphics.translate(-x - width, -y);
		case EAST:
			zeroXOffset = width - 10;
			graphics.beginPath();
			graphics.moveTo(x + width, y);
			graphics.lineTo(x, y + Math.min(20, height * 0.2));
			graphics.lineTo(x, y + height - Math.min(20, height * 0.2));
			graphics.lineTo(x + width, y + height);
			graphics.closePath();
			break;
	}
	
	graphics.setFill(Color.WHITE);
	graphics.fill();
	graphics.setStroke(Color.BLACK);
	graphics.stroke();
	
	graphics.setFill(Color.DARKGRAY);
	graphics.fillText("0", x + zeroXOffset, y + 13);
}
 
Example 20
Source File: DemultiplexerPeer.java    From CircuitSim with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void paint(GraphicsContext graphics, CircuitState circuitState) {
	GuiUtils.drawName(graphics, this, getProperties().getValue(Properties.LABEL_LOCATION));
	Direction direction = getProperties().getValue(Properties.DIRECTION);
	
	int x = getScreenX();
	int y = getScreenY();
	int width = 3 * GuiUtils.BLOCK_SIZE;
	int height = (getComponent().getNumOutputs() + 2) * GuiUtils.BLOCK_SIZE;
	
	int zeroXOffset = 0;
	
	switch(direction) {
		case SOUTH:
			graphics.translate(x, y);
			graphics.rotate(270);
			graphics.translate(-x - width, -y);
		case WEST:
			zeroXOffset = 2;
			graphics.beginPath();
			graphics.moveTo(x, y);
			graphics.lineTo(x + width, y + Math.min(20, height * 0.2));
			graphics.lineTo(x + width, y + height - Math.min(20, height * 0.2));
			graphics.lineTo(x, y + height);
			graphics.closePath();
			break;
		case NORTH:
			graphics.translate(x, y);
			graphics.rotate(270);
			graphics.translate(-x - width, -y);
		case EAST:
			zeroXOffset = width - 10;
			graphics.beginPath();
			graphics.moveTo(x + width, y);
			graphics.lineTo(x, y + Math.min(20, height * 0.2));
			graphics.lineTo(x, y + height - Math.min(20, height * 0.2));
			graphics.lineTo(x + width, y + height);
			graphics.closePath();
			break;
	}
	
	graphics.setFill(Color.WHITE);
	graphics.fill();
	graphics.setStroke(Color.BLACK);
	graphics.stroke();
	
	graphics.setFill(Color.DARKGRAY);
	graphics.fillText("0", x + zeroXOffset, y + 13);
}