java模拟风扇转动_用JavaFX显示一个转动的风扇

packagejavaseniorprograme;importjavafx.animation.KeyFrame;importjavafx.animation.KeyValue;importjavafx.animation.Timeline;importjavafx.scene.paint.Color;importjavafx.application.Application;importjavafx.geometry.Pos;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.layout.BorderPane;importjavafx.scene.layout.HBox;importjavafx.scene.layout.Pane;importjavafx.scene.shape.Arc;importjavafx.scene.shape.ArcType;importjavafx.scene.shape.Circle;importjavafx.stage.Stage;importjavafx.util.Duration;/*** 显示一个转动的风扇

*@authorASUS*/

public class Exercise15_28 extendsApplication{

@Overridepublic voidstart(Stage stage){//创建一个圆

Circle c = new Circle(250,135,125,Color.WHITE);

c.setStroke(Color.BLACK);//创建三个按钮

Button bt1 = new Button("Pause");

Button bt2= new Button("Resume");

Button bt3= new Button("Reverse");//创建一个HBox

HBox hbox = newHBox();

hbox.setAlignment(Pos.CENTER);

hbox.setSpacing(20);

hbox.getChildren().addAll(bt1,bt2,bt3);//创建四个Arc

Arc[] a = new Arc[4];for(int i = 0; i < 4; i++){

a[i]= new Arc(250,135,120,120,90*i,30);

a[i].setStroke(Color.BLACK);

a[i].setFill(Color.BLACK);

a[i].setType(ArcType.ROUND);

}//创建时间轴动画,顺时针动画与逆时针动画

Timeline timeline1 = newTimeline();

timeline1.setCycleCount(Timeline.INDEFINITE);

Timeline timeline2= newTimeline();

timeline2.setCycleCount(Timeline.INDEFINITE);

Duration duration= Duration.millis(2000);

KeyValue[] kv1= new KeyValue[4];

KeyFrame[] kf1= new KeyFrame[4];

KeyValue[] kv2= new KeyValue[4];

KeyFrame[] kf2= new KeyFrame[4];for(int j = 0; j < 4; j++){

kv1[j]= new KeyValue(a[j].startAngleProperty(), 360+a[j].getStartAngle());

kf1[j]= newKeyFrame(duration,kv1);

timeline1.getKeyFrames().add(kf1[j]);

kv2[j]= new KeyValue(a[j].startAngleProperty(), -360+a[j].getStartAngle());

kf2[j]= newKeyFrame(duration,kv2);

timeline2.getKeyFrames().add(kf2[j]);

}

bt2.setOnMouseClicked(e->{

timeline2.pause();

timeline1.play();

});

bt1.setOnMouseClicked(e->{

timeline1.pause();

timeline2.pause();

});

bt3.setOnMouseClicked(e->{

timeline1.pause();

timeline2.play();

});//创建一个Pane

Pane pa = newPane();

pa.getChildren().addAll(c,a[0],a[1],a[2],a[3]);//创建一个BorderPane

BorderPane pane = newBorderPane();

pane.setCenter(pa);

pane.setBottom(hbox);

Scene scene= new Scene(pane,500,320);

stage.setTitle("Exercise16_03");

stage.setScene(scene);

stage.show();

}/***

*@paramargs*/

public static voidmain(String[] args){

Application.launch(args);

}

}


版权声明:本文为weixin_39922642原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。