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

The following examples show how to use javafx.scene.canvas.GraphicsContext#strokeText() . 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: Hexagon.java    From chart-fx with Apache License 2.0 6 votes vote down vote up
public void renderCoordinates(GraphicsContext gc) {
    final Text text = new Text(position.getCoordinates());
    if (map != null) {
        // TODO re-enable font
        // text.setFont(map.getFont());
    }
    final double textWidth = text.getBoundsInLocal().getWidth();
    final double textHeight = text.getBoundsInLocal().getHeight();
    final double x = getGraphicsXoffset() - textWidth / 2;
    final double y = getGraphicsYoffset() + textHeight / 4;
    text.setX(x);
    text.setY(y);
    // Not sure why, but 4 seems like a good value
    gc.strokeText(position.getCoordinates(), x, y);
    // root.getChildren().add(text);
}
 
Example 2
Source File: TextHelper.java    From arma-dialog-creator with MIT License 5 votes vote down vote up
/**
 Paint the text where designated. The text will not be clipped anywhere.
 <p>
 This method will invoke {@link GraphicsContext#save()} and {@link GraphicsContext#restore()}
 at the start and end of this method, respectively.

 @param gc context to use
 @param textX x position of text
 @param textY y position of text
 @param font font to use
 @param text text to use
 @param textColor color of text
 @param textShadow shadow to use
 @param shadowColor color of shadow
 */
public static void paintText(@NotNull GraphicsContext gc, int textX, int textY, @NotNull Font font,
							 @NotNull String text, @NotNull Color textColor,
							 @NotNull TextShadow textShadow, @NotNull Color shadowColor) {
	gc.save();

	gc.setFont(font);
	gc.setFill(textColor);
	switch (textShadow) {
		case None: {
			gc.fillText(text, textX, textY);
			break;
		}
		case DropShadow: {
			final double offset = 2.0;
			gc.setFill(shadowColor);
			gc.fillText(text, textX + offset, textY + offset);
			gc.setFill(textColor);
			gc.fillText(text, textX, textY);
			break;
		}
		case Stroke: {
			gc.setLineWidth(2);
			gc.setStroke(shadowColor);
			gc.strokeText(text, textX, textY);
			gc.fillText(text, textX, textY);
			break;
		}
		default: {
			throw new IllegalStateException("unknown textShadow=" + textShadow);
		}
	}
	gc.restore();
}
 
Example 3
Source File: Example2.java    From Introduction-to-JavaFX-for-Game-Development with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void start(Stage theStage) 
{
    theStage.setTitle( "Canvas Example" );
    
    Group root = new Group();
    Scene theScene = new Scene( root );
    theStage.setScene( theScene );
    
    Canvas canvas = new Canvas( 400, 200 );
    root.getChildren().add( canvas );
    
    GraphicsContext gc = canvas.getGraphicsContext2D();
    
    gc.setFill( Color.RED );
    gc.setStroke( Color.BLACK );
    gc.setLineWidth(2);
    Font theFont = Font.font( "Times New Roman", FontWeight.BOLD, 48 );
    gc.setFont( theFont );
    gc.fillText( "Hello, World!", 60, 50 );
    gc.strokeText( "Hello, World!", 60, 50 );
    
    Image earth = new Image( "earth.png" );
    gc.drawImage( earth, 180, 100 );
    
    theStage.show();
}
 
Example 4
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();
}