Я хочу сделать циферблат, который будет каждую секунду менять время. - Java
Формулировка задачи:
Подскажите, как можно в javafx реализовать бесконечный цикл. Допустим я хочу сделать циферблат, который будет каждую секунду менять время.
Решение задачи: «Я хочу сделать циферблат, который будет каждую секунду менять время.»
textual
Листинг программы
import javafx.animation.*;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Ciferblat extends Application {
private double width = 500, height = 500, length = 200;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
ImageView view = new ImageView("http://poko-pic.ru/images/490625_ciferblat-png.jpg");
view.setPreserveRatio(true);
view.setFitWidth(width);
Line line = new Line(width / 2, height / 2, width / 2, height / 2 - length);
Circle circle = new Circle(width / 2, height / 2, 10, Color.WHITE);
circle.setStroke(Color.RED);
line.setStroke(Color.RED);
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(60),
new KeyValue(line.endXProperty(), line.getStartX() + length, new MyInterpolator(true)),
new KeyValue(line.endYProperty(), line.getStartY(), new MyInterpolator(false)))
);
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
primaryStage.setScene(new Scene(new AnchorPane(view, line, circle), 500, 500));
primaryStage.show();
}
class MyInterpolator extends Interpolator {
private boolean isX;
MyInterpolator(boolean isX) {
this.isX = isX;
}
@Override
protected double curve(double t) {
return isX ? Math.cos(Math.toRadians(90 - 360 * t)) : 1 - Math.sin(Math.toRadians(90 - 360 * t));
}
}
}