Skip to content

Instantly share code, notes, and snippets.

@Da9el00
Created April 19, 2021 07:41
Show Gist options
  • Save Da9el00/e04475bea09b05764de6f40968f824bb to your computer and use it in GitHub Desktop.
Save Da9el00/e04475bea09b05764de6f40968f824bb to your computer and use it in GitHub Desktop.
Alarm Clock - 12-hour 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("11:13:50:AM");
@FXML
private Text timer;
@FXML
private TextField alarmTime;
Timeline timeline = new Timeline(
new KeyFrame(Duration.seconds(0.005),
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 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;
private boolean am = false;
private boolean pm = false;
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]);
if(time[3].equals("AM")){
am = true;
}
if (time[3].equals("PM")){
pm = true;
}
}
public String getCurrentTime(){
return hour + ":" + minute + ":" + second + " " + getAmOrPm();
}
public void oneSecondPassed(){
second++;
if(second == 60){
minute++;
second = 0;
if(minute == 60){
hour++;
if (hour == 12){
changeAMPM();
}
minute = 0;
if(hour == 13){
hour = 1;
}
}
}
}
private String getAmOrPm(){
if(am){
return "AM";
} else if (pm){
return "PM";
}
return null;
}
private void changeAMPM(){
if(am){
am = false;
pm = true;
} else if (pm){
am = true;
pm = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment