如何为一个应用程序设置两个时间线? [英] how to set up two timelines to one app?

查看:23
本文介绍了如何为一个应用程序设置两个时间线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了时间线冲突问题.我有一个带有动画时间线的工作时钟窗格,我想添加一个带有自己时间线的秒表.

因为我希望他们一起工作,所以当我启动秒表时,我现在必须改变什么,因为它不断重启,所以无法停止?

时钟应用

public class Clock extends Application {@Override//覆盖Application类中的start方法公共无效开始(阶段primaryStage){ClockPane 时钟 = new ClockPane();//创建一个时钟clock.setStyle("-fx-background-color: DARKKHAKI;");//创建动画处理程序EventHandlereventHandler = e ->{时钟.setCurrentTime();//设置一个新的时钟时间};//为正在运行的时钟创建动画时间线动画 = 新时间线(new KeyFrame(Duration.millis(1000), eventHandler));animation.setCycleCount(Timeline.INDEFINITE);动画播放();//开始动画//创建一个场景并将其放置在舞台上场景场景=新场景(时钟,300,300);primaryStage.setTitle("时钟");//设置舞台标题primaryStage.setScene(场景);//将场景放置在舞台上primaryStage.show();//显示舞台primaryStage.setResizable(false);}/*** main 方法只需要有限的IDE* JavaFX 支持.从命令行运行不需要.*/公共静态无效主(字符串 [] args){发射(参数);}}

时钟面板

public class ClockPane extends Pane {私人整数小时;私人整数分钟;私人整数秒;按钮 sButton, rButton;文字文字;//时间线时间线;int 分钟 = 0,秒 = 0,毫秒 = 0;布尔 sos = 真;/** 用当前时间构造一个默认时钟 */公共时钟面板(){设置当前时间();}/** 构造一个指定时分秒的时钟 */公共时钟面板(整数小时,整数分钟,整数秒){this.hour = 小时;this.minute = 分钟;this.second = 第二个;}/** 返回时间 */公共 int getHour() {返程时间;}/** 设置一个新的小时 */公共无效设置小时(整数小时){this.hour = 小时;油漆时钟();}/** 返回分钟 */公共 int getMinute() {返回分钟;}/** 设置新的分钟 */公共无效集分钟(整数分钟){this.minute = 分钟;油漆时钟();}/** 返回第二个 */公共 int getSecond() {返回第二个;}/** 设置新的秒 */public void setSecond(int second) {this.second = 第二个;油漆时钟();}/* 设置时钟的当前时间 */公共无效 setCurrentTime() {//为当前日期和时间构造一个日历日历日历 = new GregorianCalendar();//设置当前时分秒this.hour = calendar.get(Calendar.HOUR_OF_DAY);this.minute = calendar.get(Calendar.MINUTE);this.second = calendar.get(Calendar.SECOND);油漆时钟();//重绘时钟}无效更改(文本文本){如果(毫秒 == 1000){秒++;毫秒 = 0;}如果(秒 == 60){分钟++;秒 = 0;}text.setText((((mins/10) == 0) ? "0" : "") + 分钟 + ":"+ (((secs/10) == 0) ? "0" : "") + secs + ":"+ ((((millis/10) == 0) ? "00" : ((((millis/100) == 0) ? "0" : "")) + millis++);}/** 绘制时钟 */私人无效paintClock(){text = new Text("00:00:000");text.setStyle("-fx-font-size: 11pt;-fx-stroke:silver;");text.setLayoutX(125);text.setLayoutY(180);Timeline timer = new Timeline(new KeyFrame(Duration.millis(1), new EventHandler() {@覆盖公共无效句柄(ActionEvent 事件){更改(文本);}}));timer.setCycleCount(Timeline.INDEFINITE);timer.setAutoReverse(false);sButton = new Button("开始");sButton.setLayoutX(12);sButton.setLayoutY(272);sButton.setStyle("-fx-font-size: 9pt;-fx-text-fill:#50f441;-fx-font-weight: bold;"+ "-fx-background-color:radial-gradient(radius 245%, black,derivative(#50f441, -30%));");sButton.setOnAction(new EventHandler() {@覆盖公共无效句柄(ActionEvent 事件){如果(sos){定时器播放();sos = 假;sButton.setText("停止");} 别的 {定时器暂停();sos = 真;sButton.setText("开始");}}});rButton = new Button("重置");rButton.setLayoutX(252);rButton.setLayoutY(272);rButton.setStyle("-fx-font-size: 9pt;-fx-text-fill:#50f441;-fx-font-weight: bold;"+ "-fx-background-color:radial-gradient(radius 245%, black,derivative(#50f441, -30%));");rButton.setOnAction(new EventHandler() {@覆盖公共无效句柄(ActionEvent 事件){分钟 = 0;秒 = 0;毫秒 = 0;定时器暂停();text.setText("00:00:000");如果(!sos){sos = 真;sButton.setText("开始");}}});//初始化时钟参数double clockRadius = Math.min(getWidth(), getHeight()) * 0.55 * 0.82;double centerX = getWidth()/2;double centerY = getHeight()/2;//画圆Circle circle = new Circle(centerX, centerY,clockRadius);circle.setStyle(" -fx-fill:radius-gradient(radius 245%, black,derivative(GREEN, -30%)); "+ "-fx-stroke: 径向渐变(半径 180%, 深绿​​色, 派生(#50f441, -40%),派生(#50f441, -30%));-fx-stroke-width:10;");组号 = new Group();numbers.setLayoutX(142);numbers.setLayoutY(142);for(int i = 0; i <12; i++){Label label = new Label(String.valueOf(i==0?12:i));label.setStyle("-fx-font-size: 13pt;-fx-text-fill:#50f441;-fx-font-weight: bold;");圆 c=新圆();c.getTransforms().add(new Rotate((i) * (360d/12d)));c.getTransforms().add(new Translate(0,-100d));label.setTranslateX(c.localToParent(0,0).getX());label.setTranslateY(c.localToParent(0,0).getY());StackPane sp = new StackPane(c,label);numbers.getChildren().add(sp);}//绘制秒针double sLength = clockRadius * 0.65;double secondX = centerX + sLength * Math.sin(second * (2 * Math.PI/60));double secondY = centerY - sLength * Math.cos(second * (2 * Math.PI/60));Line sLine = new Line(centerX, centerY, secondX, secondY);sLine.setStyle(" -fx-stroke: #50f441;-fx-stroke-width: 2;-fx-stroke-line-cap: round;");//绘制分针双 mLength = 时钟半径 * 0.55;double xMinute = centerX + mLength * Math.sin(minute * (2 * Math.PI/60));double minuteY = centerY - mLength * Math.cos(minute * (2 * Math.PI/60));Line mLine = new Line(centerX, centerY, xMinute, minuteY);mLine.setStyle(" -fx-stroke:derive(#50f441, -5%);-fx-stroke-width: 4;-fx-stroke-line-cap: round;");//绘制时针双 hLength = 时钟半径 * 0.35;double hourX = centerX + hLength * Math.sin((hour % 12 + minute/60.0) * (2 * Math.PI/12));double hourY = centerY - hLength * Math.cos((hour % 12 + minute/60.0) * (2 * Math.PI/12));Line hLine = new Line(centerX, centerY, hourX, hourY);hLine.setStyle(" -fx-stroke:derive(#50f441, -30%);-fx-stroke-width: 6;-fx-stroke-line-cap: round;");getChildren().clear();getChildren().addAll(circle, sLine, mLine, hLine, numbers,sButton,rButton,text);}@覆盖公共无效设置宽度(双倍宽度){super.setWidth(width);油漆时钟();}@覆盖公共无效设置高度(双高度){super.setHeight(height);油漆时钟();}}

解决方案

我使用 James 的想法创建了一个版本.

<块引用>

主要内容:

import javafx.application.Application;导入 javafx.scene.Scene;导入 javafx.stage.Stage;/**** @作者 sedj601*/公共类 JavaFXApplication200 扩展应用程序{@Override//覆盖Application类中的start方法公共无效开始(阶段primaryStage){FakeCustomNode clockNode = new FakeCustomNode();场景场景 = 新场景(clockNode, 300, 300);primaryStage.setTitle(时钟");//设置舞台标题primaryStage.setScene(场景);//将场景放置在舞台上primaryStage.show();//显示舞台primaryStage.setResizable(false);}/*** 只有具有有限 JavaFX 支持的 IDE 才需要 main 方法.* 从命令行运行不需要.*/public static void main(String[] args){发射(参数);}}

<块引用>

FakeCustomNodeClass

import javafx.scene.layout.VBox;/**** @作者 sedj601*/最终的公共类 FakeCustomNode 扩展了 VBox{ClockGUI时钟;StopWatchGUI stopWatchGUI;公共 FakeCustomNode(){时钟 = 新的 ClockGUI();stopWatchGUI = new StopWatchGUI();getChildren().addAll(clock.getCurrentClock(), stopWatchGUI.getStopWatch());}}

<块引用>

StopWATCHGUI 类:

import javafx.animation.KeyFrame;导入 javafx.animation.Timeline;导入 javafx.event.ActionEvent;导入 javafx.scene.control.Button;导入 javafx.scene.layout.HBox;导入 javafx.scene.layout.VBox;导入 javafx.scene.text.Text;导入 javafx.util.Duration;/**** @作者 sedj601*/公共类 StopWatchGUI{文字显示;按钮启动;按钮暂停;按钮复位;VBox vbox = new VBox();整数秒 = 0;公共秒表GUI(){display = new Text("00:00:00");开始=新按钮(开始");暂停 = 新按钮(暂停");重置 = 新按钮(重置");Timeline stopWatchTimeline = new Timeline(new KeyFrame(Duration.seconds(1), (ActionEvent event) -> {第二++;display.setText(String.format("%02d:%02d:%02d", second/3600, (second % 3600)/60, second % 60));}));stopWatchTimeline.setCycleCount(Timeline.INDEFINITE);stopWatchTimeline.play();start.setOnAction((事件) -> {stopWatchTimeline.play();});pause.setOnAction((事件) -> {stopWatchTimeline.pause();});reset.setOnAction((事件) -> {stopWatchTimeline.stop();秒 = 0;display.setText(00:00:00");});vbox.getChildren().addAll(display, new HBox(start, pause, reset));}公共 VBox getStopWatch(){返回 vbox;}}

<块引用>

ClockGUI 类:

import java.time.LocalDateTime;导入 javafx.animation.KeyFrame;导入 javafx.animation.Timeline;导入 javafx.event.ActionEvent;导入 javafx.scene.layout.*;导入 javafx.scene.paint.*;导入 javafx.scene.shape.*;导入 javafx.scene.transform.*;导入 javafx.util.Duration;/**** @作者塞德里克*/公共类 ClockGUI{圆形钟面;第二行;行分钟;线路小时;旋转第二轮;旋转分钟旋转;旋转小时旋转;AnchorPane currentClockFace;LocalDateTime localDateTime;公共时钟GUI(){currentClockFace = 新的 AnchorPane();currentClockFace.setPrefSize(100, 100);时钟面 = 新圆(100/2, 100/2, 100/2);clockFace.setStroke(Color.BLACK);clockFace.setFill(Color.TRANSPARENT);第二 = 新行(100/2, 100/2, 100/2, 100/2 - 40);secondRotation = new Rotate();secondRotation.pivotXProperty().bind(second.startXProperty());secondRotation.pivotYProperty().bind(second.startYProperty());second.getTransforms().add(secondRotation);分钟 = 新行(100/2, 100/2, 100/2, 100/2 - 30);分钟旋转 = 新旋转();minuteRotation.pivotXProperty().bind(minute.startXProperty());minuteRotation.pivotYProperty().bind(minute.startYProperty());分钟.getTransforms().add(minuteRotation);小时 = 新行(100/2, 100/2, 100/2, 100/2 - 20);小时旋转 = 新旋转();hourRotation.pivotXProperty().bind(hour.startXProperty());hourRotation.pivotYProperty().bind(hour.startYProperty());小时.getTransforms().add(hourRotation);currentClockFace.getChildren().addAll(clockFace, second, minute, hour);setToCurrentTime();时间线clockTimeline = new Timeline(new KeyFrame(Duration.seconds(1), (ActionEvent event) -> {setToCurrentTime();}));clockTimeline.setCycleCount(Timeline.INDEFINITE);clockTimeline.play();}公共 AnchorPane getCurrentClock(){返回当前时钟面;}public void setRotateSecond(双学位){secondRotation.setAngle(degree);}public void setRotateMinute(双学位){分钟旋转.setAngle(度);}public void setRotateHour(双学位){hourRotation.setAngle(degree);}私有无效 setToCurrentTime(){localDateTime = LocalDateTime.now();setRotateSecond(localDateTime.getSecond() * 6);setRotateMinute(localDateTime.getMinute() * 6);setRotateHour(((localDateTime.getHour() % 12) * 6) + (localDateTime.getMinute() * .1));System.out.println(localDateTime.getSecond() * 6);System.out.println(localDateTime.getMinute() * 6);System.out.println(((localDateTime.getHour() % 12) * 6) + (localDateTime.getMinute() * .1));}}

这很快就完成了.代码可能有问题!

I have a problem with timeline collision. I have working clock pane with animation timeline and I wanted to add a stopwatch with its own timeline.

As I want them working together what I have to change as right now when I start stopwatch a cannot stop it as its keep restarting?

clock application

public class Clock extends Application {


    @Override // Override the start method in the Application class
      public void start(Stage primaryStage) {
      ClockPane clock = new ClockPane(); // Create a clock
    clock.setStyle("-fx-background-color: DARKKHAKI;");


    // Create a handler for animation
    EventHandler<ActionEvent> eventHandler = e -> {
      clock.setCurrentTime(); // Set a new clock time
    };

      // Create an animation for a running clock
      Timeline animation = new Timeline(
      new KeyFrame(Duration.millis(1000), eventHandler));
      animation.setCycleCount(Timeline.INDEFINITE);
      animation.play(); // Start animation

      // Create a scene and place it in the stage
      Scene scene = new Scene(clock, 300, 300);
      primaryStage.setTitle("Clock"); // Set the stage title
      primaryStage.setScene(scene); // Place the scene in the stage
      primaryStage.show(); // Display the stage
      primaryStage.setResizable(false);

    }

    /**
     * The main method is only needed for the IDE with limited
     * JavaFX support. Not needed for running from the command line.
     */
    public static void main(String[] args) {
      launch(args);
    }

}

clock Pane

public class ClockPane extends Pane {


private int hour;
    private int minute;
    private int second;
    Button sButton, rButton;
    Text text;
    //Timeline timeline;
    int mins = 0, secs = 0, millis = 0;
    boolean sos = true;

    /** Construct a default clock with the current time */
    public ClockPane() {
        setCurrentTime();
    }

    /** Construct a clock with specified hour, minute, and second */
    public ClockPane(int hour, int minute, int second) {
        this.hour = hour;
        this.minute = minute;
        this.second = second;
    }

    /** Return hour */
    public int getHour() {
        return hour;
    }

    /** Set a new hour */
    public void setHour(int hour) {
        this.hour = hour;
        paintClock();
    }

    /** Return minute */
    public int getMinute() {
        return minute;
    }

    /** Set a new minute */
    public void setMinute(int minute) {
        this.minute = minute;
        paintClock();
    }

    /** Return second */
    public int getSecond() {
        return second;
    }

    /** Set a new second */
    public void setSecond(int second) {
        this.second = second;
        paintClock();
    }

    /* Set the current time for the clock */
    public void setCurrentTime() {
        // Construct a calendar for the current date and time
        Calendar calendar = new GregorianCalendar();

        // Set current hour, minute and second
        this.hour = calendar.get(Calendar.HOUR_OF_DAY);
        this.minute = calendar.get(Calendar.MINUTE);
        this.second = calendar.get(Calendar.SECOND);


        paintClock(); // Repaint the clock
    }
    void change(Text text) {
        if(millis == 1000) {
            secs++;
            millis = 0;
        }
        if(secs == 60) {
            mins++;
            secs = 0;
        }
        text.setText((((mins/10) == 0) ? "0" : "") + mins + ":"
         + (((secs/10) == 0) ? "0" : "") + secs + ":" 
            + (((millis/10) == 0) ? "00" : (((millis/100) == 0) ? "0" : "")) + millis++);
    }
    /** Paint the clock */
    private void paintClock() {



            text = new Text("00:00:000");
            text.setStyle("-fx-font-size: 11pt;-fx-stroke:silver;");
            text.setLayoutX(125);
            text.setLayoutY(180);
            Timeline timer = new Timeline(new KeyFrame(Duration.millis(1), new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    change(text);
                }
            }));
            timer.setCycleCount(Timeline.INDEFINITE);
            timer.setAutoReverse(false);
            sButton = new Button("Start");
            sButton.setLayoutX(12);
            sButton.setLayoutY(272);
            sButton.setStyle("-fx-font-size: 9pt;-fx-text-fill:#50f441;-fx-font-weight: bold;"
                    + "-fx-background-color:radial-gradient(radius 245%, black, derive(#50f441, -30%)); ");
            sButton.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    if(sos) {
                        timer.play();
                        sos = false;
                        sButton.setText("Stop");
                    } else {
                        timer.pause();
                        sos = true;
                        sButton.setText("Start");
                    }
                }
            });
            rButton = new Button("Reset");
            rButton.setLayoutX(252);
            rButton.setLayoutY(272);
            rButton.setStyle("-fx-font-size: 9pt;-fx-text-fill:#50f441;-fx-font-weight: bold;"
                    + "-fx-background-color:radial-gradient(radius 245%, black, derive(#50f441, -30%)); ");
            rButton.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    mins = 0;
                    secs = 0;
                    millis = 0;
                    timer.pause();
                    text.setText("00:00:000");
                    if(!sos) {
                        sos = true;
                        sButton.setText("Start");
                    }
                }
            });




        // Initialize clock parameters
        double clockRadius = Math.min(getWidth(), getHeight()) * 0.55 * 0.82;
        double centerX = getWidth() / 2;
        double centerY = getHeight() / 2;

        // Draw circle
        Circle circle = new Circle(centerX, centerY, clockRadius);


        circle.setStyle(" -fx-fill: radial-gradient(radius 245%, black, derive(GREEN, -30%)); "
                + "-fx-stroke: radial-gradient(radius 180%, darkgreen, derive(#50f441, -40%),derive(#50f441, -30%));-fx-stroke-width:10;");

        Group numbers = new Group();
        numbers.setLayoutX(142);
        numbers.setLayoutY(142);




        for(int i = 0; i < 12; i++){
            Label label = new Label(String.valueOf(i==0?12:i));
            label.setStyle("-fx-font-size: 13pt;-fx-text-fill:#50f441;-fx-font-weight: bold; ");
            Circle c=new Circle();


            c.getTransforms().add(new Rotate((i) * (360d / 12d)));
            c.getTransforms().add(new Translate(0,-100d));
            label.setTranslateX(c.localToParent(0,0).getX());
            label.setTranslateY(c.localToParent(0,0).getY());

            StackPane sp = new StackPane(c,label);
            numbers.getChildren().add(sp);
        }

        // Draw second hand
        double sLength = clockRadius * 0.65;
        double secondX = centerX + sLength * Math.sin(second * (2 * Math.PI / 60));
        double secondY = centerY - sLength * Math.cos(second * (2 * Math.PI / 60));
        Line sLine = new Line(centerX, centerY, secondX, secondY);
        sLine.setStyle(" -fx-stroke: #50f441;-fx-stroke-width: 2;-fx-stroke-line-cap: round;");

        // Draw minute hand
        double mLength = clockRadius * 0.55;
        double xMinute = centerX + mLength * Math.sin(minute * (2 * Math.PI / 60));
        double minuteY = centerY - mLength * Math.cos(minute * (2 * Math.PI / 60));
        Line mLine = new Line(centerX, centerY, xMinute, minuteY);
        mLine.setStyle(" -fx-stroke: derive(#50f441, -5%);-fx-stroke-width: 4;-fx-stroke-line-cap: round;");

        // Draw hour hand
        double hLength = clockRadius * 0.35;
        double hourX = centerX + hLength * Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12));
        double hourY = centerY - hLength * Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12));
        Line hLine = new Line(centerX, centerY, hourX, hourY);

        hLine.setStyle(" -fx-stroke: derive(#50f441, -30%);-fx-stroke-width: 6;-fx-stroke-line-cap: round;");


        getChildren().clear();
        getChildren().addAll(circle, sLine, mLine, hLine, numbers,sButton,rButton,text);
    }

    @Override
    public void setWidth(double width) {
        super.setWidth(width);
        paintClock();
    }

    @Override
    public void setHeight(double height) {
        super.setHeight(height);
        paintClock();
    }

}

解决方案

I create a version using James' ideas.

Main:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author sedj601
 */
public class JavaFXApplication200 extends Application
{

    @Override // Override the start method in the Application class
    public void start(Stage primaryStage)
    {

        FakeCustomNode clockNode = new FakeCustomNode();

        Scene scene = new Scene(clockNode, 300, 300);
        primaryStage.setTitle("Clock"); // Set the stage title
        primaryStage.setScene(scene); // Place the scene in the stage
        primaryStage.show(); // Display the stage
        primaryStage.setResizable(false);

    }

    /**
     * The main method is only needed for the IDE with limited JavaFX support.
     * Not needed for running from the command line.
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}

FakeCustomNodeClass

import javafx.scene.layout.VBox;

/**
 *
 * @author sedj601
 */
final public class FakeCustomNode extends VBox
{

    ClockGUI clock;
    StopWatchGUI stopWatchGUI;

    public FakeCustomNode()
    {
        clock = new ClockGUI();
        stopWatchGUI = new StopWatchGUI();

        getChildren().addAll(clock.getCurrentClock(), stopWatchGUI.getStopWatch());
    }

}

StopWAtchGUI Class:

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.util.Duration;

/**
 *
 * @author sedj601
 */
public class StopWatchGUI
{
    Text display;
    Button start;
    Button pause;
    Button reset;
    VBox vbox = new VBox();

    int second = 0;

    public StopWatchGUI()
    {
        display = new Text("00:00:00");
        start = new Button("Start");
        pause = new Button("Pause");
        reset = new Button("Reset");

        Timeline stopWatchTimeline = new Timeline(new KeyFrame(Duration.seconds(1), (ActionEvent event) -> {
            second++;
            display.setText(String.format("%02d:%02d:%02d", second / 3600, (second % 3600) / 60, second % 60));
        }));
        stopWatchTimeline.setCycleCount(Timeline.INDEFINITE);
        stopWatchTimeline.play();

        start.setOnAction((event) -> {
            stopWatchTimeline.play();
        });
        pause.setOnAction((event) -> {
            stopWatchTimeline.pause();
        });
        reset.setOnAction((event) -> {
            stopWatchTimeline.stop();
            second = 0;
            display.setText("00:00:00");
        });
        vbox.getChildren().addAll(display, new HBox(start, pause, reset));
    }

    public VBox getStopWatch()
    {
        return vbox;
    }

}

ClockGUI Class:

import java.time.LocalDateTime;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
import javafx.scene.transform.*;
import javafx.util.Duration;

/**
 *
 * @author Sedrick
 */
public class ClockGUI
{

    Circle clockFace;
    Line second;
    Line minute;
    Line hour;
    Rotate secondRotation;
    Rotate minuteRotation;
    Rotate hourRotation;
    AnchorPane currentClockFace;

    LocalDateTime localDateTime;

    public ClockGUI()
    {
        currentClockFace = new AnchorPane();
        currentClockFace.setPrefSize(100, 100);

        clockFace = new Circle(100 / 2, 100 / 2, 100 / 2);
        clockFace.setStroke(Color.BLACK);
        clockFace.setFill(Color.TRANSPARENT);

        second = new Line(100 / 2, 100 / 2, 100 / 2, 100 / 2 - 40);
        secondRotation = new Rotate();
        secondRotation.pivotXProperty().bind(second.startXProperty());
        secondRotation.pivotYProperty().bind(second.startYProperty());
        second.getTransforms().add(secondRotation);

        minute = new Line(100 / 2, 100 / 2, 100 / 2, 100 / 2 - 30);
        minuteRotation = new Rotate();
        minuteRotation.pivotXProperty().bind(minute.startXProperty());
        minuteRotation.pivotYProperty().bind(minute.startYProperty());
        minute.getTransforms().add(minuteRotation);

        hour = new Line(100 / 2, 100 / 2, 100 / 2, 100 / 2 - 20);
        hourRotation = new Rotate();
        hourRotation.pivotXProperty().bind(hour.startXProperty());
        hourRotation.pivotYProperty().bind(hour.startYProperty());
        hour.getTransforms().add(hourRotation);

        currentClockFace.getChildren().addAll(clockFace, second, minute, hour);

        setToCurrentTime();
        Timeline clockTimeline = new Timeline(new KeyFrame(Duration.seconds(1), (ActionEvent event) -> {
            setToCurrentTime();
        }));
        clockTimeline.setCycleCount(Timeline.INDEFINITE);
        clockTimeline.play();

    }

    public AnchorPane getCurrentClock()
    {
        return currentClockFace;
    }

    public void setRotateSecond(double degree)
    {
        secondRotation.setAngle(degree);
    }

    public void setRotateMinute(double degree)
    {
        minuteRotation.setAngle(degree);
    }

    public void setRotateHour(double degree)
    {
        hourRotation.setAngle(degree);
    }

    private void setToCurrentTime()
    {
        localDateTime = LocalDateTime.now();
        setRotateSecond(localDateTime.getSecond() * 6);
        setRotateMinute(localDateTime.getMinute() * 6);
        setRotateHour(((localDateTime.getHour() % 12) * 6) + (localDateTime.getMinute() * .1));
        System.out.println(localDateTime.getSecond() * 6);
        System.out.println(localDateTime.getMinute() * 6);
        System.out.println(((localDateTime.getHour() % 12) * 6) + (localDateTime.getMinute() * .1));
    }
}

This was done fast. There may be problems with code!

这篇关于如何为一个应用程序设置两个时间线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆