-
-
Save nezarjhons/034a8b4c5257d57e4b44a6ce1f513e4a to your computer and use it in GitHub Desktop.
Alarm Clock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment