analog_clock/AnalogClockImaging/src/analogclockimaging/AnalogClockImaging.java @ dcb5e6b3
| ba485147 | TAKAHASHI, Toru | /*
|
||
* Copyright © 2014 TAKAHASHI,Toru
|
||||
*/
|
||||
package analogclockimaging;
|
||||
| 6a2d84dd | TAKAHASHI, Toru | import java.time.LocalTime;
|
||
| ba485147 | TAKAHASHI, Toru | import javafx.animation.Animation;
|
||
import javafx.animation.Interpolator;
|
||||
import javafx.animation.RotateTransition;
|
||||
import javafx.application.Application;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.image.ImageView;
|
||||
import javafx.scene.layout.StackPane;
|
||||
import javafx.stage.Stage;
|
||||
import javafx.util.Duration;
|
||||
/**
|
||||
* アナログ時計(画像ファイルで実現)
|
||||
*
|
||||
* @author TAKAHASHI,Toru
|
||||
*/
|
||||
public class AnalogClockImaging extends Application {
|
||||
@Override
|
||||
public void start(Stage primaryStage) {
|
||||
StackPane root = new StackPane();
|
||||
ImageView clockDial = new ImageView(new Image("file:clockDial.png"));
|
||||
ImageView hourHand = new ImageView(new Image("file:clockHourHand.png"));
|
||||
ImageView minuteHand = new ImageView(new Image("file:clockMinuteHand.png"));
|
||||
ImageView secondsHand = new ImageView(new Image("file:clockSecondsHand.png"));
|
||||
ImageView centerPoint = new ImageView(new Image("file:clockCenterPoint.png"));
|
||||
| 6a2d84dd | TAKAHASHI, Toru | RotateTransition secondsHandTransition = createRotateTransition(Duration.seconds(60), secondsHand, getAngleOfSeconds(LocalTime.now()));
|
||
| ba485147 | TAKAHASHI, Toru | secondsHandTransition.play();
|
||
| 6a2d84dd | TAKAHASHI, Toru | RotateTransition minuteTransition = createRotateTransition(Duration.minutes(60), minuteHand, getAngleOfMinute(LocalTime.now()));
|
||
| ba485147 | TAKAHASHI, Toru | minuteTransition.play();
|
||
| 6a2d84dd | TAKAHASHI, Toru | RotateTransition hourTranslation = createRotateTransition(Duration.hours(12), hourHand, getAngleOfHour(LocalTime.now()));
|
||
| ba485147 | TAKAHASHI, Toru | hourTranslation.play();
|
||
root.getChildren().addAll(
|
||||
clockDial, hourHand, minuteHand, secondsHand, centerPoint
|
||||
);
|
||||
Scene scene = new Scene(root);
|
||||
primaryStage.setTitle("Clock");
|
||||
primaryStage.setScene(scene);
|
||||
primaryStage.show();
|
||||
}
|
||||
| 6a2d84dd | TAKAHASHI, Toru | /**
|
||
* 360度回転を繰り返すアニメーションの設定。
|
||||
*
|
||||
* @param duration 1回転するのに要する時間
|
||||
* @param node 回転するノード
|
||||
* @param startAngle 回転開始角度
|
||||
* @return 指定下パラメータで初期化したRotateTransitionインスタンス
|
||||
*/
|
||||
private RotateTransition createRotateTransition(Duration duration, Node node, double startAngle) {
|
||||
| ba485147 | TAKAHASHI, Toru | RotateTransition rt = new RotateTransition(duration, node);
|
||
| 6a2d84dd | TAKAHASHI, Toru | rt.setFromAngle(startAngle);
|
||
rt.setByAngle(360);
|
||||
| ba485147 | TAKAHASHI, Toru | rt.setCycleCount(Animation.INDEFINITE);
|
||
rt.setInterpolator(Interpolator.LINEAR);
|
||||
return rt;
|
||||
}
|
||||
| 6a2d84dd | TAKAHASHI, Toru | /**
|
||
* 引数で指定した時刻における短針(時)の角度(0時を0度とした時計回り)
|
||||
*
|
||||
* @param time 時刻
|
||||
* @return 指定した時刻における短針の角度
|
||||
*/
|
||||
private static double getAngleOfHour(LocalTime time) {
|
||||
return (time.getHour() % 12 + time.getMinute() / 60d + time.getSecond() / (60d * 60d)) * 360 / 12;
|
||||
}
|
||||
/**
|
||||
* 引数で指定した時刻における長針(分)の角度(0分を0度とした時計回り)。
|
||||
*
|
||||
* @param time 時刻
|
||||
* @return 指定した時刻における長針の角度
|
||||
*/
|
||||
private static double getAngleOfMinute(LocalTime time) {
|
||||
return (time.getMinute() + time.getSecond() / 60d) * 360 / 60;
|
||||
}
|
||||
/**
|
||||
* 引数で指定した時刻における秒針(秒)の角度(0秒を0度とした時計回り)
|
||||
*
|
||||
* @param time 時刻
|
||||
* @return 指定した時刻における秒針の角度
|
||||
*/
|
||||
private static double getAngleOfSeconds(LocalTime time) {
|
||||
return time.getSecond() * 360 / 60;
|
||||
}
|
||||
| ba485147 | TAKAHASHI, Toru | /**
|
||
* @param args the command line arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
launch(args);
|
||||
}
|
||||
}
|