ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2021-11-24 00:02:30  阅读:193  来源: 互联网

标签:javafx Button 按钮 JavaFX scene 风扇 import new public


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

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,Button,按钮,JavaFX,scene,风扇,import,new,public
来源: https://blog.csdn.net/weixin_46058231/article/details/121505705

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有