**16.19(控制一组风扇)
编写一个程序,在一组中显示三个风扇,有控制按钮来启动和停止整组风扇,如图16-44所示。
习题分析
- 要完成这道题目,需要将16.18中的代码变成一个自定义面板(继承自BorderPane),再由第二个类去示例化三个风扇面板
代码示例
编程练习题16_19FanPane.java
package chapter_16;
import javafx.animation.KeyFrame;
import javafx.animation.RotateTransition;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
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.util.Duration;
public class 编程练习题16_19FanPane extends BorderPane{
private Timeline fanAnimation;
private Arc arc1;
private Arc arc2;
private Arc arc3;
private Arc arc4;
private double x = 0;
private double y = 0;
private double angle = 30;
private boolean direction = false;
编程练习题16_19FanPane(){
drawFan();
}
编程练习题16_19FanPane(double x, double y){
setX(x);
setY(y);
drawFan();
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public void pause() {
fanAnimation.pause();
}
public void play() {
fanAnimation.play();
}
public void reverse() {
fanAnimation.setRate(fanAnimation.getRate() * -1);// 通过将动画的速率乘以-1,可以实现反转动画的效果。
direction = !direction;
}
public void drawFan() {
setLayoutX(636);
setLayoutY(637);
Pane pane = new Pane();
Circle c = new Circle(x, y, 80);
c.setStroke(Color.BLACK);
c.setFill(Color.WHITE);
pane.getChildren().add(c);
int angle = 30;
arc1 = new Arc(x, y, 70, 70, angle, 30);
angle += 90;
arc1.setFill(Color.BLUE);
arc1.setType(ArcType.ROUND);
arc2 = new Arc(x, y, 70, 70, angle, 30);
angle += 90;
arc2.setFill(Color.BLUE);
arc2.setType(ArcType.ROUND);
arc3 = new Arc(x, y, 70, 70, angle, 30);
angle += 90;
arc3.setFill(Color.BLUE);
arc3.setType(ArcType.ROUND);
arc4 = new Arc(x, y, 70, 70, angle, 30);
angle += 90;
arc4.setFill(Color.BLUE);
arc4.setType(ArcType.ROUND);
pane.getChildren().addAll(arc1,arc2,arc3, arc4);
HBox hBox = new HBox();
hBox.setAlignment(Pos.CENTER);
hBox.setSpacing(5);
Button btPause = new Button("Pause");
Button btResume = new Button("Resume");
Button btReverse = new Button("Reverse");
hBox.getChildren().addAll(btPause, btResume, btReverse);
Slider slider = new Slider(1,100,1);
slider.setPrefWidth(200);
HBox hBox2 = new HBox(slider);
hBox2.setAlignment(Pos.CENTER);
EventHandler<ActionEvent> eventHandler = e -> {
setNewRotate();
};
fanAnimation = new Timeline();
//此处设置注意
fanAnimation.getKeyFrames().add(new KeyFrame(Duration.millis(50), eventHandler));
fanAnimation.setCycleCount(Timeline.INDEFINITE);
fanAnimation.play();
btPause.setOnMouseClicked(e -> pause());
btResume.setOnMouseClicked(e -> play());
btReverse.setOnMouseClicked(e -> {
reverse();
});
slider.valueProperty().addListener(ov ->{
fanAnimation.setRate(slider.getValue());
});
setTop(hBox);
setCenter(pane);
setBottom(hBox2);
}
public void setNewRotate()
{
if (direction)
{
angle = 10.0;
}
else
angle = -10.0;
arc1.startAngleProperty().setValue((arc1.startAngleProperty().getValue() + angle) % 360);
arc2.startAngleProperty().setValue((arc2.startAngleProperty().getValue() + angle) % 360);
arc3.startAngleProperty().setValue((arc3.startAngleProperty().getValue() + angle) % 360);
arc4.startAngleProperty().setValue((arc4.startAngleProperty().getValue() + angle) % 360);
}
}
编程练习题16_19ControlASetOfFans.java
package chapter_16;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class 编程练习题16_19ControlASetOfFans extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
HBox hBox = new HBox(10);
编程练习题16_19FanPane fanPane = new 编程练习题16_19FanPane(100, 100);
编程练习题16_19FanPane fanPane2 = new 编程练习题16_19FanPane(100, 100);
编程练习题16_19FanPane fanPane3 = new 编程练习题16_19FanPane(100, 100);
fanPane.setStyle("-fx-border-color:black");
fanPane2.setStyle("-fx-border-color:black");
fanPane3.setStyle("-fx-border-color:black");
hBox.getChildren().addAll(fanPane,fanPane2,fanPane3);
HBox hBox2 = new HBox(20);
hBox2.setAlignment(Pos.CENTER);
Button btStart = new Button("Start All");
Button btStop = new Button("Stop All");
hBox2.getChildren().addAll(btStart, btStop);
btStop.setOnMouseClicked(e ->{
fanPane.pause();
fanPane2.pause();
fanPane3.pause();
});
btStart.setOnMouseClicked(e ->{
fanPane.play();
fanPane2.play();
fanPane3.play();
});
BorderPane borderPane = new BorderPane();
borderPane.setCenter(hBox);
borderPane.setBottom(hBox2);
Scene scene = new Scene(borderPane,630, 250);
primaryStage.setTitle("编程练习题16_19ControlASetOfFans");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
-
结果展示