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

The following examples show how to use javafx.scene.canvas.GraphicsContext#setTextAlign() . 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: 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 2
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 3
Source File: LetterAvatar.java    From youtube-comment-suite with MIT License 6 votes vote down vote up
private void draw() {
    Canvas canvas = new Canvas(scale, scale);
    GraphicsContext gc = canvas.getGraphicsContext2D();

    gc.setFill(Color.TRANSPARENT);
    gc.fillRect(0, 0, scale, scale);

    gc.setFill(background);
    gc.fillRect(0, 0, scale, scale);

    gc.setFill(Color.WHITE);
    gc.setTextAlign(TextAlignment.CENTER);
    gc.setTextBaseline(VPos.CENTER);
    gc.setFont(Font.font("Tahoma", FontWeight.SEMI_BOLD, scale * 0.6));

    gc.fillText(String.valueOf(character), Math.round(scale / 2.0), Math.round(scale / 2.0));
    Platform.runLater(() -> canvas.snapshot(null, this));
}
 
Example 4
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 5
Source File: SegmentedEdgeViewer.java    From JetUML with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draws a string.
 * @param pGraphics the graphics context
 * @param pEndPoint1 an endpoint of the segment along which to draw the string
 * @param pEndPoint2 the other endpoint of the segment along which to draw the string
 * @param pString the string to draw 
 * @param pCenter true if the string should be centered along the segment
 */
private static void drawString(GraphicsContext pGraphics, Point2D pEndPoint1, Point2D pEndPoint2, 
		ArrowHead pArrowHead, String pString, boolean pCenter)
{
	if (pString == null || pString.length() == 0)
	{
		return;
	}
	Rectangle bounds = getStringBounds(pEndPoint1, pEndPoint2, pArrowHead, pString, pCenter);
	
	Paint oldFill = pGraphics.getFill();
	VPos oldVPos = pGraphics.getTextBaseline();
	TextAlignment oldAlign = pGraphics.getTextAlign();
	pGraphics.translate(bounds.getX(), bounds.getY());
	pGraphics.setFill(Color.BLACK);
	int textX = 0;
	int textY = 0;
	if(pCenter) 
	{
		textX = bounds.getWidth()/2;
		textY = bounds.getHeight() - textDimensions(pString).getHeight()/2;
		pGraphics.setTextBaseline(VPos.CENTER);
		pGraphics.setTextAlign(TextAlignment.CENTER);
	}
	pGraphics.fillText(pString, textX, textY);
	pGraphics.translate(-bounds.getX(), -bounds.getY()); 
	pGraphics.setFill(oldFill);
	pGraphics.setTextBaseline(oldVPos);
	pGraphics.setTextAlign(oldAlign);
}
 
Example 6
Source File: Helper.java    From charts with Apache License 2.0 5 votes vote down vote up
public static final void drawTextWithBackground(final GraphicsContext CTX, final String TEXT, final Font FONT, final Color TEXT_BACKGROUND, final Color TEXT_FILL, final double X, final double Y) {
    CtxDimension dim = getTextDimension(TEXT, FONT);
    double textWidth  = dim.getWidth() * 1.2;
    double textHeight = dim.getHeight();
    CTX.save();
    CTX.setFont(FONT);
    CTX.setTextBaseline(VPos.CENTER);
    CTX.setTextAlign(TextAlignment.CENTER);
    CTX.setFill(TEXT_BACKGROUND);
    CTX.fillRect(X - textWidth * 0.5, Y - textHeight * 0.5, textWidth, textHeight);
    CTX.setFill(TEXT_FILL);
    CTX.fillText(TEXT, X, Y);
    CTX.restore();
}
 
Example 7
Source File: DeckView.java    From Solitaire with GNU General Public License v2.0 5 votes vote down vote up
private Canvas createNewGameImage()
{
	double width = CardImages.getBack().getWidth();
	double height = CardImages.getBack().getHeight();
	Canvas canvas = new Canvas( width, height );
	GraphicsContext context = canvas.getGraphicsContext2D();
	
	// The reset image
	context.setStroke(Color.DARKGREEN);
	context.setLineWidth(IMAGE_NEW_LINE_WIDTH);
	context.strokeOval(width/4, height/2-width/4 + IMAGE_FONT_SIZE, width/2, width/2);

	// The text
	
	context.setTextAlign(TextAlignment.CENTER);
	context.setTextBaseline(VPos.CENTER);
	context.setFill(Color.DARKKHAKI);
	context.setFont(Font.font(Font.getDefault().getName(), IMAGE_FONT_SIZE));
	
	
	
	if( GameModel.instance().isCompleted() )
	{
		context.fillText("You won!", Math.round(width/2), IMAGE_FONT_SIZE);
	}
	else
	{
		context.fillText("Give up?", Math.round(width/2), IMAGE_FONT_SIZE);
	}
	context.setTextAlign(TextAlignment.CENTER);
	return canvas;
}
 
Example 8
Source File: BulletChartSkin.java    From Medusa with Apache License 2.0 4 votes vote down vote up
private void drawTickMarks(final GraphicsContext CTX) {
    tickMarkCanvas.setCache(false);
    CTX.clearRect(0, 0, tickMarkCanvas.getWidth(), tickMarkCanvas.getHeight());
    CTX.setFill(gauge.getMajorTickMarkColor());

    List<Section> tickMarkSections         = gauge.getTickMarkSections();
    List<Section> tickLabelSections        = gauge.getTickLabelSections();
    Color         majorTickMarkColor       = gauge.getTickMarkColor();
    Color         tickLabelColor           = gauge.getTickLabelColor();
    boolean       smallRange               = Double.compare(gauge.getRange(), 10.0) <= 0;
    double        minValue                 = gauge.getMinValue();
    double        maxValue                 = gauge.getMaxValue();
    double        tmpStepSize              = smallRange ? stepSize / 10 : stepSize;
    Font          tickLabelFont            = Fonts.robotoRegular(0.1 * size);
    boolean       tickMarkSectionsVisible  = gauge.getTickMarkSectionsVisible();
    boolean       tickLabelSectionsVisible = gauge.getTickLabelSectionsVisible();
    double        offsetX                  = 0.18345865 * width;
    double        offsetY                  = 0.1 * height;
    double        innerPointX              = 0;
    double        innerPointY              = 0;
    double        outerPointX              = 0.07 * width;
    double        outerPointY              = 0.08 * height;
    double        textPointX               = Orientation.HORIZONTAL == orientation ? 0.55 * tickMarkCanvas.getWidth() : outerPointX + size * 0.05;
    double        textPointY               = 0.7 * tickMarkCanvas.getHeight();
    BigDecimal    minorTickSpaceBD         = BigDecimal.valueOf(gauge.getMinorTickSpace());
    BigDecimal    majorTickSpaceBD         = BigDecimal.valueOf(gauge.getMajorTickSpace());
    BigDecimal    counterBD                = BigDecimal.valueOf(gauge.getMinValue());
    double        counter                  = minValue;
    double        range                    = gauge.getRange();

    for (double i = 0 ; Double.compare(i, range) <= 0 ; i++) {
        if (Orientation.VERTICAL == orientation) {
            innerPointY = counter * tmpStepSize + offsetY;
            outerPointY = innerPointY;
            textPointY  = innerPointY;
        } else {
            innerPointX = counter * tmpStepSize + offsetX;
            outerPointX = innerPointX;
            textPointX  = innerPointX;
        }

        // Set the general tickmark color
        CTX.setStroke(gauge.getTickMarkColor());
        if (Double.compare(counterBD.remainder(majorTickSpaceBD).doubleValue(), 0.0) == 0) {
            // Draw major tick mark
            if (gauge.getMajorTickMarksVisible()) {
                CTX.setFill(tickMarkSectionsVisible ? Helper.getColorOfSection(tickMarkSections, counter, majorTickMarkColor) : majorTickMarkColor);
                CTX.setStroke(tickMarkSectionsVisible ? Helper.getColorOfSection(tickMarkSections, counter, majorTickMarkColor) : majorTickMarkColor);
                CTX.setLineWidth(1);
                CTX.strokeLine(innerPointX, innerPointY, outerPointX, outerPointY);
            }
            // Draw tick label text
            if (gauge.getTickLabelsVisible()) {
                CTX.save();
                CTX.translate(textPointX, textPointY);
                CTX.setFont(tickLabelFont);
                CTX.setTextAlign(Orientation.HORIZONTAL == orientation ? TextAlignment.CENTER : TextAlignment.LEFT);
                CTX.setTextBaseline(VPos.CENTER);
                CTX.setFill(tickLabelSectionsVisible ? Helper.getColorOfSection(tickLabelSections, counter, tickLabelColor) : tickLabelColor);
                if (Orientation.VERTICAL == orientation) {
                    CTX.fillText(Integer.toString((int) (maxValue - counter)), 0, 0);
                } else {
                    CTX.fillText(Integer.toString((int) counter), 0, 0);
                }
                CTX.restore();
            }
        }
        counterBD = counterBD.add(minorTickSpaceBD);
        counter   = counterBD.doubleValue();
        if (counter > maxValue) break;
    }

    tickMarkCanvas.setCache(true);
    tickMarkCanvas.setCacheHint(CacheHint.QUALITY);
}
 
Example 9
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 10
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 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: 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 13
Source File: PlainAmpSkin.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.61 : 0.62;
    Point2D center              = new Point2D(width * 0.5, height * 1.4);
    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.07 : 0.068) * 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.51987097 * sinValue, center.getY() + width * 0.51987097 * cosValue);
        Point2D outerMinorPoint  = new Point2D(center.getX() + width * 0.55387097 * sinValue, center.getY() + width * 0.55387097 * cosValue);
        Point2D outerMediumPoint = new Point2D(center.getX() + width * 0.56387097 * sinValue, center.getY() + width * 0.56387097 * cosValue);
        Point2D outerPoint       = new Point2D(center.getX() + width * 0.58387097 * sinValue, center.getY() + width * 0.58387097 * 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 14
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 15
Source File: CustomPlainAmpSkin.java    From medusademo 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.61 : 0.62;
    Point2D center         = new Point2D(width * 0.5, height * 1.4);
    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.07 : 0.068) * 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.51987097 * sinValue, center.getY() + width * 0.51987097 * cosValue);
        Point2D outerMinorPoint  = new Point2D(center.getX() + width * 0.55387097 * sinValue, center.getY() + width * 0.55387097 * cosValue);
        Point2D outerMediumPoint = new Point2D(center.getX() + width * 0.56387097 * sinValue, center.getY() + width * 0.56387097 * cosValue);
        Point2D outerPoint       = new Point2D(center.getX() + width * 0.58387097 * sinValue, center.getY() + width * 0.58387097 * 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 16
Source File: YOLO2_TF_Client.java    From SKIL_Examples with Apache License 2.0 4 votes vote down vote up
private void renderJavaFXStyle(GraphicsContext ctx) throws Exception {

        INDArray boundingBoxPriors = Nd4j.create(YOLO2.DEFAULT_PRIOR_BOXES);

        ctx.setLineWidth(3);
        ctx.setTextAlign(TextAlignment.LEFT);

        long start = System.nanoTime();
        for (int i = 0; i < 1; i++) {
            INDArray permuted = networkGlobalOutput.permute(0, 3, 1, 2);
            INDArray activated = YoloUtils.activate(boundingBoxPriors, permuted);
            List<DetectedObject> predictedObjects = YoloUtils.getPredictedObjects(boundingBoxPriors, activated, 0.6, 0.4);
            
            //System.out.println( "width: " + imageWidth );
            //System.out.println( "height: " + imageHeight );

            for (DetectedObject o : predictedObjects) {
                ClassPrediction classPrediction = labels.decodePredictions(o.getClassPredictions(), 1).get(0).get(0);
                String label = classPrediction.getLabel();
                long x = Math.round(imageWidth  * o.getCenterX() / gridWidth);
                long y = Math.round(imageHeight * o.getCenterY() / gridHeight);
                long w = Math.round(imageWidth  * o.getWidth()   / gridWidth);
                long h = Math.round(imageHeight * o.getHeight()  / gridHeight);

                System.out.println("\"" + label + "\" at [" + x + "," + y + ";" + w + "," + h + "], score = "
                                + o.getConfidence() * classPrediction.getProbability());

                double[] xy1 = o.getTopLeftXY();
                double[] xy2 = o.getBottomRightXY();
                int x1 = (int) Math.round(imageWidth  * xy1[0] / gridWidth);
                int y1 = (int) Math.round(imageHeight * xy1[1] / gridHeight);
                int x2 = (int) Math.round(imageWidth  * xy2[0] / gridWidth);
                int y2 = (int) Math.round(imageHeight * xy2[1] / gridHeight);

                int rectW = x2 - x1;
                int rectH = y2 - y1;
                //System.out.printf("%s - %d, %d, %d, %d \n", label, x1, x2, y1, y2);
                //System.out.println( "color: " + colors.get(label) );
                ctx.setStroke(colors.get(label));
                ctx.strokeRect(x1, y1, rectW, rectH);
                
                int labelWidth = label.length() * 10;
                int labelHeight = 14;
                
                ctx.setFill( colors.get(label) );
                ctx.strokeRect(x1, y1-labelHeight, labelWidth, labelHeight);
                ctx.fillRect(x1, y1 - labelHeight, labelWidth, labelHeight);
                ctx.setFill( Color.WHITE );
                ctx.fillText(label, x1 + 3, y1 - 3 );

            }

        }

        globalEndTime = System.nanoTime();
        long end = System.nanoTime();
        System.out.println("Rendering code took: " + (end - start) / 1000000 + " ms");

        System.out.println("Overall Program Time: " + (globalEndTime - globalStartTime) / 1000000 + " ms");

    }
 
Example 17
Source File: GridRenderer.java    From chart-fx with Apache License 2.0 4 votes vote down vote up
protected void drawPolarGrid(final GraphicsContext gc, XYChart xyChart) {
    final Axis xAxis = xyChart.getXAxis();
    final Axis yAxis = xyChart.getYAxis();
    final double xAxisWidth = xyChart.getCanvas().getWidth();
    final double yAxisHeight = xyChart.getCanvas().getHeight();
    final double xRange = xAxis.getWidth();
    final double yRange = yAxis.getHeight();
    final double xCentre = xRange / 2;
    final double yCentre = yRange / 2;
    final double maxRadius = 0.5 * Math.min(xRange, yRange) * 0.9;
    if (xAxis instanceof Node) {
        ((Node) xAxis).setVisible(false);
    }

    gc.save();
    if (verMajorGridStyleNode.isVisible() || verMinorGridStyleNode.isVisible()) {
        applyGraphicsStyleFromLineStyle(gc, verMajorGridStyleNode);
        for (double phi = 0.0; phi <= 360; phi += xyChart.getPolarStepSize().get()) {
            final double x = xCentre + maxRadius * Math.sin(phi * GridRenderer.DEG_TO_RAD);
            final double y = yCentre - maxRadius * Math.cos(phi * GridRenderer.DEG_TO_RAD);
            final double xl = xCentre + maxRadius * Math.sin(phi * GridRenderer.DEG_TO_RAD) * 1.05;
            final double yl = yCentre - maxRadius * Math.cos(phi * GridRenderer.DEG_TO_RAD) * 1.05;

            gc.strokeLine(xCentre, yCentre, x, y);

            gc.save();
            gc.setFont(yAxis.getTickLabelFont());
            gc.setStroke(yAxis.getTickLabelFill());
            gc.setLineDashes(null);
            gc.setTextBaseline(VPos.CENTER);
            if (phi < 350) {
                if (phi < 20) {
                    gc.setTextAlign(TextAlignment.CENTER);
                } else if (phi <= 160) {
                    gc.setTextAlign(TextAlignment.LEFT);
                } else if (phi <= 200) {
                    gc.setTextAlign(TextAlignment.CENTER);
                } else {
                    gc.setTextAlign(TextAlignment.RIGHT);
                }
                gc.strokeText(String.valueOf(phi), xl, yl);
            }
            gc.restore();

        }

        if (xAxis.isLogAxis() || verMinorGridStyleNode.isVisible()) {
            applyGraphicsStyleFromLineStyle(gc, verMinorGridStyleNode);
            xAxis.getMinorTickMarks().stream().mapToDouble(TickMark::getPosition).forEach(xPos -> {
                if (xPos > 0 && xPos <= xAxisWidth) {
                    gc.strokeLine(xPos, 0, xPos, yAxisHeight);
                }
            });
        }
    }

    drawPolarCircle(gc, yAxis, yRange, xCentre, yCentre, maxRadius);

    gc.restore();
}
 
Example 18
Source File: LabelledMarkerRenderer.java    From chart-fx with Apache License 2.0 4 votes vote down vote up
/**
 * Draws vertical markers with vertical (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 drawVerticalLabelledMarker(final GraphicsContext gc, final XYChart chart, final DataSet dataSet,
        final int indexMin, final int indexMax) {
    Axis xAxis = this.getFirstAxis(Orientation.HORIZONTAL);
    if (xAxis == null) {
        xAxis = chart.getFirstAxis(Orientation.HORIZONTAL);
    }
    if (xAxis == null) {
        if (LOGGER.isWarnEnabled()) {
            LOGGER.atWarn().addArgument(LabelledMarkerRenderer.class.getSimpleName()).log("{}::drawVerticalLabelledMarker(...) getFirstAxis(HORIZONTAL) returned null skip plotting");
        }
        return;
    }

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

    final double height = chart.getCanvas().getHeight();
    double lastLabel = -Double.MAX_VALUE;
    double lastFontSize = 0;
    for (int i = indexMin; i < indexMax; i++) {
        final double screenX = (int) xAxis.getDisplayPosition(dataSet.get(DataSet.DIM_X, 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(screenX, 0, screenX, height);

        if (Math.abs(screenX - lastLabel) > lastFontSize && !label.isEmpty()) {
            gc.save();
            gc.setLineWidth(0.8);
            gc.setLineDashes(1.0);
            gc.translate(Math.ceil(screenX + 3), Math.ceil(0.01 * height));
            gc.rotate(+90);
            gc.fillText(label, 0.0, 0);
            gc.restore();
            lastLabel = screenX;
            lastFontSize = gc.getFont().getSize();
        }
        if (pointStyle != null) {
            gc.restore();
        }
    }
    gc.restore();
}