Memory leak in JavaFX indefinite Timeline

◇◆丶佛笑我妖孽 提交于 2019-12-09 04:36:28

Try this and see if you are having the same problem.

ClockGUI

import javafx.scene.layout.*;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
import javafx.scene.transform.*;

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

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

    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);

    }

    public AnchorPane getCurrentClock()
    {
        return currentClockFace;
    }

    public void rotateSecondLine()
    {
        secondRotation.setAngle(secondRotation.getAngle() + 6);
    }

    public double getRotateSecondLine()
    {
        return secondRotation.getAngle();
    }

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

    public void rotateMinuteLine()
    {
        minuteRotation.setAngle(minuteRotation.getAngle() + 6);
    }

    public double getRotateMinuteLine()
    {
        return minuteRotation.getAngle();
    }

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

    public void rotateHourLine()
    {
        hourRotation.setAngle(hourRotation.getAngle() + 6);
    }

    public double getRotateHourLine()
    {
        return hourRotation.getAngle();
    }

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

Main

import javafx.animation.*;
import javafx.application.*;
import javafx.event.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;
import javafx.util.*;

/**
 *
 * @author Sedrick
 */
public class JavaFXApplication54 extends Application {

    @Override
    public void start(Stage primaryStage)
    {
        VBox root = new VBox();
        ClockGUI cgui = new ClockGUI();

        StackPane stackpane = new StackPane();
        stackpane.getChildren().add(cgui.getCurrentClock());
        root.getChildren().add(stackpane);

        Button btn = new Button("Rotate seconds");
        btn.setOnAction((event) -> {
            cgui.rotateSecondLine();
        });

        Button btn2 = new Button("Rotate minutes");
        btn2.setOnAction((event) -> {
            cgui.rotateMinuteLine();
        });

        Button btn3 = new Button("Rotate hours");
        btn3.setOnAction((event) -> {
            cgui.rotateHourLine();
        });

        root.getChildren().addAll(btn, btn2, btn3);
        Scene scene = new Scene(root, 300, 250);

        Timeline timeline = new Timeline();
        timeline.setCycleCount(Timeline.INDEFINITE);
        timeline.getKeyFrames().add(
                new KeyFrame(Duration.seconds(1),
                        new EventHandler() {
                    // KeyFrame event handler
                    @Override
                    public void handle(Event event)
                    {
                        System.out.println(cgui.getRotateSecondLine());
                        cgui.rotateSecondLine();
                        if (cgui.getRotateSecondLine() >= 360) {
                            cgui.setRotateSecond(0);
                            cgui.rotateMinuteLine();
                        }
                        if (cgui.getRotateMinuteLine() >= 360) {
                            cgui.setRotateMinute(0);
                            cgui.rotateHourLine();
                        }
                        if (cgui.getRotateHourLine() >= 360) {
                            cgui.setRotateHour(0);
                        }
                    }
                }
                ));
        timeline.playFromStart();
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!