JavaFX 8画布字体始终为粗体

3
我正在使用JavaFX画布(目前使用的是Java 8u40)来渲染一些图形和文本。我在画布中无法正确地呈现字体,似乎字体总是加粗的。我有一个示例来展示这个问题。
首先看一下它的样子。左边是标签组件,右边是画布,两者都使用相同的字体。
以下是生成此示例所使用的代码:
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);
    }
}

任何关于如何使字体看起来相同的提示都将非常有帮助。谢谢!
1个回答

3
我将回答自己的问题。不要使用GraphicsContext中的strokeText(),而应该使用fillText()。 如果我这样做就能正常工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接