Skip to content

Instantly share code, notes, and snippets.

@Da9el00
Created April 15, 2021 12:11
Show Gist options
  • Save Da9el00/6a4e561d4cd72b88ba86cd50c8f87035 to your computer and use it in GitHub Desktop.
Save Da9el00/6a4e561d4cd72b88ba86cd50c8f87035 to your computer and use it in GitHub Desktop.
Alarm Clock
package sample;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
import javafx.util.Duration;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
//Time time = new Time(new CurrentTime().currentTime());
Time time = new Time("12:13:34");
@FXML
private Text timer;
@FXML
private TextField alarmTime;
Timeline timeline = new Timeline(
new KeyFrame(Duration.seconds(1),
e -> {
if(time.getCurrentTime().equals(alarmTime.getText())){
System.out.println("ALARM!");
}
time.oneSecondPassed();
timer.setText(time.getCurrentTime());
}));
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
timer.setText(time.getCurrentTime());
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
}
}
package sample;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class CurrentTime {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
public CurrentTime(){
}
public String currentTime(){
return dtf.format(now);
}
}
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: #EAF4D3;" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Text fx:id="timer" layoutX="61.0" layoutY="93.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Text">
<font>
<Font size="38.0" />
</font>
</Text>
<TextField fx:id="alarmTime" layoutX="57.0" layoutY="226.0" />
</children>
</AnchorPane>
package sample;
public class Time {
private int hour;
private int minute;
private int second;
public Time(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
}
public Time(String currentTime) {
String[] time = currentTime.split(":");
hour = Integer.parseInt(time[0]);
minute = Integer.parseInt(time[1]);
second = Integer.parseInt(time[2]);
}
public String getCurrentTime(){
return hour + ":" + minute + ":" + second;
}
public void oneSecondPassed(){
second++;
if(second == 60){
minute++;
second = 0;
if(minute == 60){
hour++;
minute = 0;
if(hour == 24){
hour = 0;
System.out.println("Next day");
}
}
}
}
}
@nezarjhons
Copy link

good job

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment