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!