《java程序设计基础》图形坐标系与形状类(三)

这篇博客通过三个示例介绍了JavaFX的基础图形动画设计,包括使用FadeTransition实现圆的淡入淡出效果,PathTransition让文本沿椭圆轨道移动,以及使用Timeline创建字幕滚动动画。示例代码详细展示了如何配置动画参数并响应鼠标交互事件。
摘要由CSDN通过智能技术生成

8.过渡动画
编制程序对圆实现淡入淡出的效果.
package yuan;
//filename:App16_8.java
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.animation.Animation;
import javafx.animation.FadeTransition;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
import javafx.scene.layout.StackPane;
import javafx.util.Duration;
public class App16_8 extends Application
{
@Override
public void start(Stage stage){
StackPane pane=new StackPane();
Circle c=new Circle(50);
c.setStroke(Color.BLUE);
c.setFill(Color.RED);
pane.getChildren().add©;
FadeTransition ft=new FadeTransition(Duration.millis(2000));
ft.setFromValue(1.0); //设置起始透明度为1.0,表示不透明
ft.setToValue(0.0); //设置结束时透明度为0.0,表示完全透明
ft.setCycleCount(Animation.INDEFINITE); //设置循环周期为无限
ft.setAutoReverse(true); //设置自动反转
ft.setNode©; //设置动画应用的结点
ft.play(); //播放动画
c.setOnMousePressed(e->ft.pause()); //当在圆上按下鼠标时暂停动画
c.setOnMouseReleased(e->ft.play()); //当在圆上释放鼠标时继续播放
Scene scene=new Scene(pane,200,120);
stage.setTitle(“淡入淡出动画”);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Application.launch(args); //启动独立的JavaFx程序
}
}

在这里插入图片描述
9.移动效果
编制程序实现将一个文本对象设置在椭圆轨道上移动的动画
package yuan; //路径移动动画程序设计
//filename:App16_9.java
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.animation.Animation;
import javafx.animation.PathTransition;
import javafx.scene.shape.Ellipse;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.layout.Pane;
import javafx.util.Duration;
public class App16_9 extends Application{
@Override
public void start(Stage stage){
Pane pane=new Pane();
Text t=new Text(“您好”);
t.setFont(Font.font(20));
t.setFill(Color.RED);
Ellipse elli=new Ellipse(100,60,70,40);
elli.setFill(Color.WHITE);
elli.setStroke(Color.BLUE);
pane.getChildren().addAll(elli,t);
PathTransition pt=new PathTransition(); //创建动画移动路径对象pt
pt.setDuration(Duration.millis(4000)); //设置播放持续时间为4s
pt.setPath(elli); //设置椭圆elli为路径
pt.setNode(t); //设置t为动画节点
pt.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
pt.setCycleCount(Animation.INDEFINITE); //设置无限次播放
pt.setAutoReverse(false); //设置不反转
pt.play();
elli.setOnMousePressed(e->pt.pause()); //当在椭圆上按下鼠标时暂停动画
elli.setOnMouseReleased(e->pt.play());//当在椭圆上释放鼠标时继续播放
Scene scene=new Scene(pane,200,120);
stage.setTitle(“移动路径动画”);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Application.launch(args); //启动独立的JavaFx程序
}
}

在这里插入图片描述
10.时间轴动画
利用时间轴动画,编写一个字幕滚动程序
package yuan; //字幕滚动程序设计
//filename:App16_10.java
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.geometry.VPos;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.util.Duration;
public class App16_10 extends Application{
@Override
public void start(Stage stage){
Text t=new Text(“滚动字幕”);
t.setTextOrigin(VPos.TOP);
t.setFont(Font.font(24));
Pane root=new Pane(t);
root.setPrefSize(300,60);
Scene scene=new Scene(root);
stage.setScene(scene);
stage.setTitle(“时间轴动画程序设计”);
stage.show();
double sceneWidth=scene.getWidth();
double tWidth=t.getLayoutBounds().getWidth();
KeyValue sKeyValue=
new KeyValue(t.translateXProperty(),sceneWidth);
KeyFrame sFrame=new KeyFrame(Duration.ZERO,sKeyValue);
KeyValue eKeyValue=
new KeyValue(t.translateXProperty(),-1.0*tWidth);
KeyFrame eFrame=new KeyFrame(Duration.seconds(5),eKeyValue);
Timeline timeline=new Timeline(sFrame,eFrame);
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Application.launch(args); //启动独立的JavaFx程序
}
}

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值