下面列出了javafx.scene.text.Text#setFont ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void addLine(String title, String value) {
rowCount++;
Text titleText = new Text(title);
titleText.setFill(_textColor);
titleText.setFont(regularFont);
Text valueText = new Text(value);
valueText.setFill(_textColor);
valueText.setFont(regularFont);
vBoxTitles.getChildren().add(titleText);
vBoxValues.getChildren().add(valueText);
Helper.enableNode(titleText, true);
Helper.enableNode(valueText, true);
}
default void updatePathLabels() {
final Axes model = getModel();
final Font font = new Font("cmr10", model.getLabelsSize()); //NON-NLS
final PlottingStyle labelsDisplay = model.getLabelsDisplayed();
final PlottingStyle ticksDisplay = model.getTicksDisplayed();
final TicksStyle ticksStyle = model.getTicksStyle();
// This fake text is used to compute widths and heights and other font metrics of a current text.
final Text fooText = new Text("foo"); //NON-NLS
fooText.setFont(font);
if(labelsDisplay.isX()) {
updatePathLabelsX(ticksDisplay, ticksStyle, fooText);
}
if(labelsDisplay.isY()) {
updatePathLabelsY(ticksDisplay, ticksStyle, fooText);
}
}
public static Node createIconContent() {
Text sample = new Text("FX");
sample.setFont(Font.font(Font.getDefault().getFamily(), FontWeight.BOLD,80));
sample.setStyle("-fx-font-size: 80px;");
sample.setFill(Color.web("#333333"));
final DropShadow dropShadow = new DropShadow();
dropShadow.setOffsetX(4);
dropShadow.setOffsetY(6);
dropShadow.setColor(Color.rgb(0,0,0,0.7));
sample.setEffect(dropShadow);
return sample;
}
@Override
protected void initUI() {
Text uiScore = new Text("");
uiScore.setFont(Font.font(72));
uiScore.setTranslateX(getAppWidth() - 200);
uiScore.setTranslateY(50);
uiScore.fillProperty().bind(getop("stageColor"));
uiScore.textProperty().bind(getip("score").asString());
addUINode(uiScore);
Group dpadView = getInput().createVirtualDpadView();
addUINode(dpadView, 0, 625);
}
public Civ6MenuItem(String name) {
Polygon bg = new Polygon(
0, 0,
200, 0,
215, 15,
200, 30,
0, 30
);
bg.setStroke(Color.color(1, 1, 1, 0.75));
bg.setEffect(new GaussianBlur());
bg.fillProperty().bind(
Bindings.when(pressedProperty())
.then(Color.color(0, 0, 0, 0.75))
.otherwise(Color.color(0, 0, 0, 0.25))
);
text = new Text(name);
text.setTranslateX(5);
text.setTranslateY(20);
text.setFont(Font.loadFont(Civ6MenuApp.class.getResource("res/Penumbra-HalfSerif-Std_35114.ttf").toExternalForm(), 14));
text.setFill(Color.WHITE);
text.effectProperty().bind(
Bindings.when(hoverProperty())
.then(shadow)
.otherwise(blur)
);
getChildren().addAll(bg, text);
}
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create a stack pane
StackPane stackPane = new StackPane();
// Create a polygon and set its properties
Polygon polygon = new Polygon();
stackPane.getChildren().add(polygon);
polygon.setFill(Color.RED);
polygon.setRotate(20);
ObservableList<Double> list = polygon.getPoints();
final double WIDTH = 200, HEIGHT = 200;
double centerX = WIDTH / 2, centerY = HEIGHT / 2;
double radius = Math.min(WIDTH, HEIGHT) * 0.4;
// Add points to polygon list
for (int i = 0; i < 8; i++) {
list.add(centerX + radius * Math.cos(2 * i * Math.PI / 8));
list.add(centerY - radius * Math.sin(2 * i * Math.PI / 8));
}
// Create a text and set its properties
Text text = new Text("STOP");
text.setFont(Font.font("Times new Roman", FontWeight.BOLD, 40));
text.setFill(Color.WHITE);
stackPane.getChildren().add(text);
// Create a scene and place it in the stage
Scene scene = new Scene(stackPane, WIDTH, HEIGHT);
primaryStage.setTitle("Exercise_14_15"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
public static Node createIconContent() {
Text sample = new Text("FX");
sample.setFont(Font.font(Font.getDefault().getFamily(), FontWeight.BOLD,80));
sample.setStyle("-fx-font-size: 80px;");
sample.setFill(Color.web("#aaaaaa"));
final InnerShadow innerShadow = new InnerShadow();
innerShadow.setRadius(4);
innerShadow.setOffsetX(1);
innerShadow.setOffsetY(1);
innerShadow.setColor(Color.web("#333333"));
sample.setEffect(innerShadow);
return sample;
}
public Text createTextNode(String word) {
Text textNode = new Text(word);
textNode.getStyleClass().setAll("tag");
textNode.setStyle("-fx-padding: 10px");
textNode.applyCss();
textNode.setFont(configuration.font);
textNode.setCache(true);
// textNode.setCacheHint(CacheHint.SPEED);
return textNode;
}
public LettersPane() {
setId("LettersPane");
setPrefSize(480,480);
setFocusTraversable(true);
setOnMousePressed(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent me) {
requestFocus();
me.consume();
}
});
setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override public void handle(KeyEvent ke) {
createLetter(ke.getText());
ke.consume();
}
});
// create press keys text
pressText = new Text("Press Keys");
pressText.setTextOrigin(VPos.TOP);
pressText.setFont(new Font(Font.getDefault().getFamily(), 40));
pressText.setLayoutY(5);
pressText.setFill(Color.rgb(80, 80, 80));
DropShadow effect = new DropShadow();
effect.setRadius(0);
effect.setOffsetY(1);
effect.setColor(Color.WHITE);
pressText.setEffect(effect);
getChildren().add(pressText);
}
public Text createText(String value) {
Text text = new Text(value);
if( this.getFont() != null ) {
text.setFont(((JFXFont) this.getFont()).getHandle());
}
return text;
}
public VBox addVBox(final String head) {
final VBox $ = new VBox();
$.setPadding(new Insets(10, 10, 50, 10));
$.setSpacing(8);
final Text title = new Text(head);
title.setFont(Font.font("Arial", FontWeight.BOLD, 14));
$.getChildren().add(title);
return $;
}
private void adjustTextSize(final Text TEXT, final double MAX_WIDTH, double fontSize) {
final String FONT_NAME = TEXT.getFont().getName();
while (TEXT.getLayoutBounds().getWidth() > MAX_WIDTH && fontSize > 0) {
fontSize -= 0.005;
TEXT.setFont(new Font(FONT_NAME, fontSize));
}
}
public void build() {
// Create head
Circle head = new Circle(getHeadPoint().getX(), getHeadPoint().getY(), 5);
head.setFill(Color.WHITE);
// First leader line
Line firstLeaderLine = new Line(headPoint.getX(), headPoint.getY(),
headPoint.getX(), headPoint.getY());
firstLeaderLine.setStroke(Color.WHITE);
firstLeaderLine.setStrokeWidth(3);
// Second part of the leader line
Line secondLeaderLine = new Line(getLeaderLineToPoint().getX(),
getLeaderLineToPoint().getY(),
getLeaderLineToPoint().getX(),
getLeaderLineToPoint().getY());
secondLeaderLine.setStroke(Color.WHITE);
secondLeaderLine.setStrokeWidth(3);
// Main title Rectangle
HBox mainTitle = new HBox();
mainTitle.setBackground(
new Background(
new BackgroundFill(Color.WHITE,
new CornerRadii(2),
new Insets(0)))
);
// Main title text
Text mainTitleText = new Text(getMainTitleText());
HBox.setMargin(mainTitleText, new Insets(8, 8, 8, 8));
mainTitleText.setFont(Font.font(20));
mainTitle.getChildren().add(mainTitleText);
// Position sub tile rectangle under main title
Rectangle subTitleRect = new Rectangle(2, 20);
subTitleRect.setFill(Color.WHITE);
// Create the sub title
HBox subTitle = new HBox();
subTitle.setBackground(
new Background(
new BackgroundFill(Color.color(0, 0, 0, .20),
new CornerRadii(0),
new Insets(0)))
);
Text subTitleText = new Text(getSubTitleText());
subTitleText.setVisible(true);
subTitleText.setFill(Color.WHITE);
subTitleText.setFont(Font.font(14));
subTitle.getChildren().add(subTitleText);
// Build the animation code.
buildAnimation(head,
firstLeaderLine,
secondLeaderLine,
mainTitle,
subTitleRect,
subTitle);
// Must add nodes after buildAnimation.
// Positioning calculations are done
// outside of this Group.
getChildren().addAll(head,
firstLeaderLine,
secondLeaderLine,
mainTitle,
subTitleRect,
subTitle);
getChildren().forEach(node -> node.setVisible(false));
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Athena Rest Wizard");
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));
Scene scene = new Scene(grid, 500, 200);
primaryStage.setScene(scene);
Text scenetitle = new Text("Welcome, roberter!");
scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
grid.add(scenetitle, 0, 0, 2, 1);
Label projectNameLabel = new Label("Project Name:");
grid.add(projectNameLabel, 0, 1);
final TextField projectName = new TextField();
grid.add(projectName, 1, 1);
Label projectDescLabel = new Label("Project Description:");
grid.add(projectDescLabel, 0, 2);
final TextField projectDesc = new TextField();
grid.add(projectDesc, 1, 2);
Button btn = new Button("Generate Service Project Now");
HBox hbBtn = new HBox(10);
hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
hbBtn.getChildren().add(btn);
grid.add(hbBtn, 1, 4);
final Text actiontarget = new Text();
grid.add(actiontarget, 1, 6);
btn.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent arg0) {
try {
doWizard(projectName.getText(), projectDesc.getText(),
actiontarget);
} catch (IOException e) {
e.printStackTrace();
}
}
});
primaryStage.show();
}
private void initGraphics() {
stops = new ArrayList<>(16);
for (Stop stop : getGradientStops()) {
if (Double.compare(stop.getOffset(), 0.0) == 0) stops.add(new Stop(0, stop.getColor()));
stops.add(new Stop(stop.getOffset() * 0.69924 + 0.285, stop.getColor()));
}
chartPath = new Path();
overlayPath = new Path();
overlayPath.setFill(Color.TRANSPARENT);
centerCircle = new Circle();
thresholdCircle = new Circle();
thresholdCircle.setFill(Color.TRANSPARENT);
unitText = new Text(getUnit());
unitText.setTextAlignment(TextAlignment.CENTER);
unitText.setTextOrigin(VPos.CENTER);
unitText.setFont(Fonts.latoLight(0.045 * PREFERRED_WIDTH));
legendStep = (getMaxValue() - getMinValue()) / 5d;
dropShadow = new DropShadow(BlurType.TWO_PASS_BOX, Color.BLACK, 5, 0, 0, 0);
minValueText = new Text(String.format(Locale.US, formatString, getMinValue()));
minValueText.setTextAlignment(TextAlignment.CENTER);
minValueText.setTextOrigin(VPos.CENTER);
minValueText.setVisible(isLegendVisible());
minValueText.setEffect(dropShadow);
legend1Text = new Text(String.format(Locale.US, formatString, getMinValue() + legendStep));
legend1Text.setTextAlignment(TextAlignment.CENTER);
legend1Text.setTextOrigin(VPos.CENTER);
legend1Text.setVisible(isLegendVisible());
legend1Text.setEffect(dropShadow);
legend2Text = new Text(String.format(Locale.US, formatString, getMinValue() + legendStep * 2));
legend2Text.setTextAlignment(TextAlignment.CENTER);
legend2Text.setTextOrigin(VPos.CENTER);
legend2Text.setVisible(isLegendVisible());
legend2Text.setEffect(dropShadow);
legend3Text = new Text(String.format(Locale.US, formatString, getMinValue() + legendStep * 3));
legend3Text.setTextAlignment(TextAlignment.CENTER);
legend3Text.setTextOrigin(VPos.CENTER);
legend3Text.setVisible(isLegendVisible());
legend3Text.setEffect(dropShadow);
legend4Text = new Text(String.format(Locale.US, formatString, getMinValue() + legendStep * 3));
legend4Text.setTextAlignment(TextAlignment.CENTER);
legend4Text.setTextOrigin(VPos.CENTER);
legend4Text.setVisible(isLegendVisible());
legend4Text.setEffect(dropShadow);
maxValueText = new Text(String.format(Locale.US, formatString, getMaxValue()));
maxValueText.setTextAlignment(TextAlignment.CENTER);
maxValueText.setTextOrigin(VPos.CENTER);
maxValueText.setVisible(isLegendVisible());
maxValueText.setEffect(dropShadow);
textGroup = new Group();
// Add all nodes
pane = new Pane(chartPath, overlayPath, centerCircle, thresholdCircle, textGroup, unitText, minValueText, legend1Text, legend2Text, legend3Text, legend4Text, maxValueText);
getChildren().setAll(pane);
}
private void initGraphics() {
stops = new ArrayList<>(16);
for (Stop stop : getGradientStops()) {
if (Double.compare(stop.getOffset(), 0.0) == 0) stops.add(new Stop(0, stop.getColor()));
stops.add(new Stop(stop.getOffset() * 0.69924 + 0.285, stop.getColor()));
}
chartCanvas = new Canvas(PREFERRED_WIDTH, PREFERRED_HEIGHT);
chartCtx = chartCanvas.getGraphicsContext2D();
overlayCanvas = new Canvas(PREFERRED_WIDTH, PREFERRED_HEIGHT);
overlayCtx = overlayCanvas.getGraphicsContext2D();
unitText = new Text(getUnit());
unitText.setTextAlignment(TextAlignment.CENTER);
unitText.setTextOrigin(VPos.CENTER);
unitText.setFont(Fonts.latoLight(0.045 * PREFERRED_WIDTH));
legendStep = (getMaxValue() - getMinValue()) / 5d;
dropShadow = new DropShadow(BlurType.TWO_PASS_BOX, Color.BLACK, 5, 0, 0, 0);
minValueText = new Text(String.format(Locale.US, formatString, getMinValue()));
minValueText.setTextAlignment(TextAlignment.CENTER);
minValueText.setTextOrigin(VPos.CENTER);
minValueText.setVisible(isLegendVisible());
minValueText.setEffect(dropShadow);
legend1Text = new Text(String.format(Locale.US, formatString, getMinValue() + legendStep));
legend1Text.setTextAlignment(TextAlignment.CENTER);
legend1Text.setTextOrigin(VPos.CENTER);
legend1Text.setVisible(isLegendVisible());
legend1Text.setEffect(dropShadow);
legend2Text = new Text(String.format(Locale.US, formatString, getMinValue() + legendStep * 2));
legend2Text.setTextAlignment(TextAlignment.CENTER);
legend2Text.setTextOrigin(VPos.CENTER);
legend2Text.setVisible(isLegendVisible());
legend2Text.setEffect(dropShadow);
legend3Text = new Text(String.format(Locale.US, formatString, getMinValue() + legendStep * 3));
legend3Text.setTextAlignment(TextAlignment.CENTER);
legend3Text.setTextOrigin(VPos.CENTER);
legend3Text.setVisible(isLegendVisible());
legend3Text.setEffect(dropShadow);
legend4Text = new Text(String.format(Locale.US, formatString, getMinValue() + legendStep * 3));
legend4Text.setTextAlignment(TextAlignment.CENTER);
legend4Text.setTextOrigin(VPos.CENTER);
legend4Text.setVisible(isLegendVisible());
legend4Text.setEffect(dropShadow);
maxValueText = new Text(String.format(Locale.US, formatString, getMaxValue()));
maxValueText.setTextAlignment(TextAlignment.CENTER);
maxValueText.setTextOrigin(VPos.CENTER);
maxValueText.setVisible(isLegendVisible());
maxValueText.setEffect(dropShadow);
// Add all nodes
pane = new Pane(chartCanvas, overlayCanvas, unitText, minValueText, legend1Text, legend2Text, legend3Text, legend4Text, maxValueText);
pane.setBackground(new Background(new BackgroundFill(getChartBackgroundColor(), new CornerRadii(1024), Insets.EMPTY)));
getChildren().setAll(pane);
}
@Override
public synchronized void drawOverlays(GraphicsContext graphicsContext) {
if (width > 0.0 && height > 0.0 && config.getIsShowing()) {
double targetLength = Math.min(width, config.getTargetScaleBarLength());
double[] onePixelWidth = {1.0, 0.0, 0.0};
transform.applyInverse(onePixelWidth, onePixelWidth);
final double globalCoordinateWidth = LinAlgHelpers.length(onePixelWidth);
// length of scale bar in global coordinate system
final double scaleBarWidth = targetLength * globalCoordinateWidth;
final double pot = Math.floor( Math.log10( scaleBarWidth ) );
final double l2 = scaleBarWidth / Math.pow( 10, pot );
final int fracs = ( int ) ( 0.1 * l2 * subdivPerPowerOfTen );
final double scale1 = ( fracs > 0 ) ? Math.pow( 10, pot + 1 ) * fracs / subdivPerPowerOfTen : Math.pow( 10, pot );
final double scale2 = ( fracs == 3 ) ? Math.pow( 10, pot + 1 ) : Math.pow( 10, pot + 1 ) * ( fracs + 1 ) / subdivPerPowerOfTen;
final double lB1 = scale1 / globalCoordinateWidth;
final double lB2 = scale2 / globalCoordinateWidth;
final double scale;
final double scaleBarLength;
if (Math.abs(lB1 - targetLength) < Math.abs(lB2 - targetLength))
{
scale = scale1;
scaleBarLength = lB1;
}
else
{
scale = scale2;
scaleBarLength = lB2;
}
final double[] ratios = UNITS.stream().mapToDouble(unit -> config.getBaseUnit().getConverterTo(unit).convert(scale)).toArray();
int firstSmallerThanZeroIndex = 0;
for (; firstSmallerThanZeroIndex < ratios.length; ++firstSmallerThanZeroIndex) {
if (ratios[firstSmallerThanZeroIndex] < 1.0)
break;
}
final int unitIndex = Math.max(firstSmallerThanZeroIndex - 1, 0);
final Unit<Length> targetUnit = UNITS.get(unitIndex);
final double targetScale = config.getBaseUnit().getConverterTo(targetUnit).convert(scale);
final double x = 20;
final double y = height - 30;
final DecimalFormat format = new DecimalFormat(String.format("0.%s", String.join("", Collections.nCopies(config.getNumDecimals(), "#"))));
final String scaleBarText = format.format(targetScale) + targetUnit.toString();
final Text text = new Text(scaleBarText);
text.setFont(config.getOverlayFont());
final Bounds bounds = text.getBoundsInLocal();
final double tx = 20 + (scaleBarLength - bounds.getMaxX()) / 2;
final double ty = y - 5;
graphicsContext.setFill(config.getBackgroundColor());
// draw background
graphicsContext.fillRect(
x - 7,
ty - bounds.getHeight() - 3,
scaleBarLength + 14,
bounds.getHeight() + 25 );
// draw scalebar
graphicsContext.setFill(config.getForegroundColor());
graphicsContext.fillRect(x, y, ( int ) scaleBarLength, 10);
// draw label
graphicsContext.setFont(config.getOverlayFont());
graphicsContext.fillText(scaleBarText, tx, ty);
}
}
protected Text createItalicsText(String s) {
Text text = new Text(s);
text.setFont(Font.font(fontFamily, FontPosture.ITALIC, fontSize));
return text;
}
private void initGraphics() {
innerShadow = new InnerShadow(BlurType.TWO_PASS_BOX, Color.rgb(0, 0, 0, 0.65), PREFERRED_HEIGHT * 0.1, 0, 0, 0);
Color color = gradientLookup.getColorAt(getSkinnable().getValue() / (getSkinnable().getMaxValue() - getSkinnable().getMinValue()));
background = new Circle(0.5 * PREFERRED_WIDTH, 0.5 * PREFERRED_HEIGHT, 0.5 * PREFERRED_WIDTH);
background.setFill(new LinearGradient(0, 0, 0, PREFERRED_HEIGHT,
false, CycleMethod.NO_CYCLE,
new Stop(0, color.deriveColor(0, 1, 0.8, 1)),
new Stop(1, color.deriveColor(0, 1, 0.6, 1))));
background.setEffect(innerShadow);
ticksCanvas = new Canvas(PREFERRED_WIDTH, PREFERRED_HEIGHT);
ticksCanvas.setMouseTransparent(true);
ticks = ticksCanvas.getGraphicsContext2D();
targetIndicator = new Region();
targetIndicator.getStyleClass().setAll("target-indicator");
targetIndicatorRotate = new Rotate(180 - getSkinnable().getStartAngle() - getSkinnable().getMinValue() * angleStep);
targetIndicator.getTransforms().setAll(targetIndicatorRotate);
targetExceeded = false;
targetIndicator.setVisible(getSkinnable().isTargetEnabled());
valueIndicator = new Region();
valueIndicator.getStyleClass().setAll("value-indicator");
valueIndicatorRotate = new Rotate(180 - getSkinnable().getStartAngle());
valueIndicatorRotate.setAngle(valueIndicatorRotate.getAngle() + (getSkinnable().getValue() - getSkinnable().getOldValue() - getSkinnable().getMinValue()) * angleStep);
valueIndicator.getTransforms().setAll(valueIndicatorRotate);
infoText = new Text(getSkinnable().getInfoText().toUpperCase());
infoText.setTextOrigin(VPos.CENTER);
infoText.setFont(Fonts.opensansSemiBold(0.06 * PREFERRED_HEIGHT));
infoText.setMouseTransparent(true);
infoText.getStyleClass().setAll("info-text");
value = new Text(String.format(Locale.US, "%." + getSkinnable().getDecimals() + "f", getSkinnable().getValue()));
value.setMouseTransparent(true);
value.setTextOrigin(VPos.CENTER);
value.setFont(Fonts.opensansBold(0.32 * PREFERRED_HEIGHT));
value.setMouseTransparent(true);
value.getStyleClass().setAll("value");
// Add all nodes
pane = new Pane();
pane.getChildren().setAll(background,
ticksCanvas,
valueIndicator,
targetIndicator,
infoText,
value);
getChildren().setAll(pane);
}
private Bounds getBounds(String s)
{
Text t = new Text(s);
t.setFont(currentFont);
return t.getLayoutBounds();
}