java语言stroke函数_Java Text.setStroke方法代码示例

该博客展示了如何在JavaFX中使用stroke方法为文本添加边框效果。通过创建一个带有光照效果的圆形,将文本置于其中,并设置文本边框颜色为黑色。同时,利用Timeline和AnimationTimer动态改变文本内容,实现动画效果,使文本所在的StackPane在2秒内缩放,并在每次动画结束时随机平移。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import javafx.scene.text.Text; //导入方法依赖的package包/类

private void init(Stage primaryStage) {

Group root = new Group();

primaryStage.setResizable(false);

primaryStage.setScene(new Scene(root, 260,100));

//create a circle with effect

final Circle circle = new Circle(20, Color.rgb(156,216,255));

circle.setEffect(new Lighting());

//create a text inside a circle

final Text text = new Text (i.toString());

text.setStroke(Color.BLACK);

//create a layout for circle with text inside

final StackPane stack = new StackPane();

stack.getChildren().addAll(circle, text);

stack.setLayoutX(30);

stack.setLayoutY(30);

//create a timeline for moving the circle

timeline = new Timeline();

timeline.setCycleCount(Timeline.INDEFINITE);

timeline.setAutoReverse(true);

//one can add a specific action when each frame is started. There are one or more frames during

// executing one KeyFrame depending on set Interpolator.

timer = new AnimationTimer() {

@Override

public void handle(long l) {

text.setText(i.toString());

i++;

}

};

//create a keyValue with factory: scaling the circle 2times

KeyValue keyValueX = new KeyValue(stack.scaleXProperty(), 2);

KeyValue keyValueY = new KeyValue(stack.scaleYProperty(), 2);

//create a keyFrame, the keyValue is reached at time 2s

Duration duration = Duration.seconds(2);

//one can add a specific action when the keyframe is reached

EventHandler onFinished = new EventHandler() {

public void handle(ActionEvent t) {

stack.setTranslateX(java.lang.Math.random()*200);

//reset counter

i = 0;

}

};

KeyFrame keyFrame = new KeyFrame(duration, onFinished , keyValueX, keyValueY);

//add the keyframe to the timeline

timeline.getKeyFrames().add(keyFrame);

root.getChildren().add(stack);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值