1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > JavaFX实现三个风扇 每个风扇有按钮控制 一个总按钮控制三个风扇

JavaFX实现三个风扇 每个风扇有按钮控制 一个总按钮控制三个风扇

时间:2022-08-19 09:34:35

相关推荐

JavaFX实现三个风扇 每个风扇有按钮控制 一个总按钮控制三个风扇

代码中有注释,直接上代码

package com.fan;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;public class FanPane1 extends Pane {//12个扇形private Arc arc[] = new Arc[12];//设置三个扇形的初始角度private double startAngle1 = 30;private double startAngle2 = 30;private double startAngle3 = 30;//风扇转动是每次增长的度数private double increment = 5;//3个圆private Circle[] circles = new Circle[3];public FanPane1(){for(int i = 100,j=0; i<=600; i+=200,j++){//设定3个圆的圆心circles[j] = new Circle();circles[j].setCenterX(i);circles[j].setCenterY(200/2);circles[j].setRadius(70);//边缘circles[j].setStroke(Color.BLUE);//填充颜色circles[j].setFill(Color.WHITE);//添加到面板中getChildren().add(circles[j]);}//为每个圆创建风扇for(int m = 0; m<12; m++){arc[m] = new Arc();//一个圆中有四个扇形并绑定圆心arc[m].centerXProperty().bind(circles[m/4].centerXProperty());arc[m].centerYProperty().bind(circles[m/4].centerYProperty());//扇形半径略小于圆arc[m].radiusXProperty().bind(circles[m/4].radiusProperty().divide(1.1));arc[m].radiusYProperty().bind(circles[m/4].radiusProperty().divide(1.1));//圆弧起始位置arc[m].setStartAngle(30+m*90);//圆弧长度arc[m].setLength(60);arc[m].setFill(Color.BLACK);arc[m].setType(ArcType.ROUND);getChildren().addAll(arc[m]);}}//public void reverse(){}//第一个风扇旋转方法public void move1(){setStartAngle1(startAngle1 + increment);}//设置角度public void setStartAngle1(double angle){startAngle1 = angle;setValues1();}//第二个风扇旋转方法public void move2(){setStartAngle2(startAngle2 + increment);}public void setStartAngle2(double angle){startAngle2 = angle;setValues2();}//第三个风扇旋转方法public void move3(){setStartAngle3(startAngle3 + increment);}public void setStartAngle3(double angle){startAngle3 = angle;setValues3();}//控制3个风扇旋转的方法public void move(){setStartAngle(increment);}public void setStartAngle(double angle){startAngle1 += angle;startAngle2 += angle;startAngle3 += angle;setValues1();setValues2();setValues3();}/*public void setValues(){for(int i = 0; i<12; i++){arc[i].setStartAngle(startAngle + i*90);}}*/public void setValues1(){for(int i = 0; i<4; i++){arc[i].setStartAngle(startAngle1 + i*90);}}public void setValues2(){for(int i = 4; i<8; i++){arc[i].setStartAngle(startAngle2 + (i-4)*90);}}public void setValues3(){for(int i = 8; i<12; i++){arc[i].setStartAngle(startAngle3 + (i-8)*90);}}}

package com.fan;//有些类可能不需要导入import javafx.animation.Animation;import javafx.animation.KeyFrame;import javafx.animation.Timeline;import javafx.application.Application;import javafx.event.EventHandler;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.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;import javafx.event.ActionEvent;//import java.awt.event.ActionEvent;import javax.lang.model.element.ElementVisitor;import java.util.EventListener;public class test extends Application {public static void main(String[] args) {launch(args);}@Overridepublic void start(Stage primaryStage) {//创建三个圆FanPane1 fan = new FanPane1();//水平存放按钮,间距为5HBox hbox = new HBox(5);Button btPause = new Button("start1");Button btResume = new Button("end1");//按钮靠右hbox.setAlignment(Pos.BOTTOM_LEFT);hbox.getChildren().addAll(btPause, btResume);HBox hbox1 = new HBox(5);Button btPause1 = new Button("start2");Button btResume1 = new Button("end2");//按钮放中间hbox1.setAlignment(Pos.BOTTOM_CENTER);hbox1.getChildren().addAll(btPause1, btResume1);HBox hbox2 = new HBox(5);Button btPause2 = new Button("start3");Button btResume2 = new Button("end3");//按钮放在右边hbox2.setAlignment(Pos.BOTTOM_RIGHT);hbox2.getChildren().addAll(btPause2, btResume2);HBox hbox3 = new HBox(5);Button btPause3 = new Button("start all");Button btResume3 = new Button("end all");//Button moreSpdde = new Button("speedUp");//总控制按钮放中间hbox3.setAlignment(Pos.BOTTOM_CENTER);hbox3.getChildren().addAll(btPause3,btResume3/*,moreSpdde*/);//三个圆放在最上方BorderPane pane = new BorderPane();pane.setTop(fan);//控制三个圆的按钮放在分别放在左中右pane.setLeft(hbox);pane.setCenter(hbox1);pane.setRight(hbox2);pane.setBottom(hbox3);//创建控制面板Scene scene = new Scene(pane, 600, 300);primaryStage.setTitle("三个风扇");primaryStage.setScene(scene);primaryStage.show();//一开始风扇都处于静止状态//对于第一个风扇,创建一个KeyFrame用于每100ms运行一个动作事件move1Timeline animation = new Timeline(new KeyFrame(Duration.millis(30),e->fan.move1()));animation.setCycleCount(Timeline.INDEFINITE);//对于第一个风扇,创建一个KeyFrame用于每100ms运行一个动作事件move1Timeline animation1 = new Timeline(new KeyFrame(Duration.millis(30),e->fan.move2()));animation1.setCycleCount(Timeline.INDEFINITE);//对于第一个风扇,创建一个KeyFrame用于每100ms运行一个动作事件move1Timeline animation2 = new Timeline(new KeyFrame(Duration.millis(30),e->fan.move3()));animation2.setCycleCount(Timeline.INDEFINITE);//使用lambda表达式简化表达式//点击start按钮开始转动btPause.setOnAction(e->animation.play());//点击end按钮结束btResume.setOnAction(e->animation.pause());btPause1.setOnAction(e->animation1.play());btResume1.setOnAction(e->animation1.pause());btPause2.setOnAction(e->animation2.play());btResume2.setOnAction(e->animation2.pause());//点击start all按钮所有风扇开始btResume3.setOnAction(e-> {animation.pause();animation1.pause();animation2.pause();});//点击end all按钮所有风扇停止btPause3.setOnAction(e-> {animation.play();animation1.play();animation2.play();});}}

测试结果

本来还想加一个控制速度的按钮的,但是没有实现

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。