*15.26 (改变透明度)
重写编程练习题15.24,当球摆动的时候改变球的透明度
- 思路:
- 很简单,改写编程练习题15.24,为小圆Circle添加一个FadeTransition,再把暂停和开始方法设置在鼠标事件中
代码示例:编程练习题15_26FadeAnimationSwinging.java
package chapter_15;
import javafx.animation.FadeTransition;
import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class 编程练习题15_26FadeAnimationSwinging extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
Pane pane = new Pane();
Arc arc = new Arc(150, 200, 100, 40, 180, 180);
arc.setType(ArcType.OPEN);
arc.setFill(Color.WHITE);
arc.setStroke(Color.BLACK);
pane.getChildren().add(arc);
Circle circle = new Circle(10);
circle.setFill(Color.ORANGE);
pane.getChildren().add(circle);
PathTransition pt = new PathTransition();
pt.setDuration(Duration.millis(2000));
pt.setPath(arc);
pt.setNode(circle);
pt.setCycleCount(Timeline.INDEFINITE);
pt.setAutoReverse(true);
pt.play();
pane.setOnMousePressed(e -> pt.pause());
pane.setOnMouseReleased(e -> pt.play());
FadeTransition ft = new FadeTransition(
Duration.millis(2000),circle);
ft.setFromValue(1.0);
ft.setToValue(0.1);
ft.setCycleCount(Timeline.INDEFINITE);
ft.setAutoReverse(true);
ft.play();
pane.setOnMousePressed(e -> {
ft.pause();
pt.pause();
});
pane.setOnMouseReleased(e ->{
ft.play();
pt.play();
});
Scene scene = new Scene(pane, 300, 300);
primaryStage.setTitle("编程练习题15_26FadeAnimationSwinging");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
- 结果展示