JavaFX实现三个风扇、每个风扇有按钮控制、一个总按钮控制三个风扇
2021/11/24 1:10:17
本文主要是介绍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); } @Override public void start(Stage primaryStage) { //创建三个圆 FanPane1 fan = new FanPane1(); //水平存放按钮,间距为5 HBox 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运行一个动作事件move1 Timeline animation = new Timeline(new KeyFrame(Duration.millis(30),e->fan.move1())); animation.setCycleCount(Timeline.INDEFINITE); //对于第一个风扇,创建一个KeyFrame用于每100ms运行一个动作事件move1 Timeline animation1 = new Timeline(new KeyFrame(Duration.millis(30),e->fan.move2())); animation1.setCycleCount(Timeline.INDEFINITE); //对于第一个风扇,创建一个KeyFrame用于每100ms运行一个动作事件move1 Timeline 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(); }); } }
测试结果
本来还想加一个控制速度的按钮的,但是没有实现
这篇关于JavaFX实现三个风扇、每个风扇有按钮控制、一个总按钮控制三个风扇的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南