У меня проблема со столкновением по времени.У меня есть рабочая панель часов с временной шкалой анимации, и я хотел добавить секундомер со своей собственной временной шкалой.
Поскольку я хочу, чтобы они работали вместе, то я должен изменить, как сейчас, когда я запускаю секундомер, он не может остановить его, поскольку егопродолжать перезапуск?
приложение часов
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);
}
}
Панель часов
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();
}
}