rmeitz
rmeitz

Reputation: 75

JavaFX 8 Canvas Fonts always Bold

I am using a JavaFX canvas (currently using Java 8u40) to render some graphics and text. I am having a problem getting the fonts to render correctly in the canvas. It appears the font is always being drawn bold. I have an example to show the problem.

First of all this is what it looks like. This has a label component on the left and a canvas on the right, both using the same font.

enter image description here

Here is the code used to generate this example:

package sample;

import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
 * Test Canvas Fonts
 */
public class TestCanvas extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        HBox hbox = new HBox();

        hbox.setAlignment(Pos.CENTER_LEFT);
        Label label = new Label("Hello World!");
        Separator separator = new Separator(Orientation.VERTICAL);
        Canvas canvas = new Canvas(100, 40);

        hbox.getChildren().addAll(label, separator, canvas);

        GraphicsContext gc = canvas.getGraphicsContext2D();
        gc.setFill(Color.BLACK);
        gc.setFont(label.getFont());
        gc.strokeText(label.getText(), 5, canvas.getHeight()/2);

        primaryStage.setTitle("Canvas Test");
        primaryStage.setScene(new Scene(hbox, 200, 50));

        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
}

Any clues on how to make the fonts look the same would be really helpful.

Thanks!

Upvotes: 3

Views: 1932

Answers (1)

rmeitz
rmeitz

Reputation: 75

I am going to answer my own question. Instead of using: strokeText() in the GraphicsContext, I should use:fillText().

If I do that it works fine.

Upvotes: 3

Related Questions