Please use javafx package, thank you (4) A GUI Define a class called AlarmApp th
ID: 3722866 • Letter: P
Question
Please use javafx package, thank you
(4) A GUI Define a class called AlarmApp that creates the following window by laying the components out manually using relocate() and setPrefSize0 Alarm App Time 01-54 16 PM 00:00:00 10-30.00 AM Select Alarm New Alarm Delete ON / OFF Note that the GUI MUST contain the following . a white background Pane (l.e., " -Ex-background-color: white:) three inner Pane objects with borders (ie., x-background-color: white -x-border-color: gray: x-padding: ) Labels on those bordered panes (ie., x-background-color: white:-x-ceanslate-y: -:- ex-translate-x: 10 " a large (eg. 48px or anything reasonable) system font Label with a dark green color in the . right-aligned TextFields in the Current Time and Alarm Time panes with the given times shown. Remaining Time" pane, a ComboBox with prompt text "Select Alarm and it should contain 3 entries: Weekday Saturday and "Sunday. and two RadioButtons belonging to the same ToggleGroup . The dimensions of everything is as follows: 10 200 10 200 0 Alarm App Remaining Hme 10 10 Earent ime (22,20) 48pt System font 00:00:00 190 25 01:54:16 PM 90 25 10:30:00 AM 40 10 10 Select Alarm 10 -10 New Alarm Delete OONOFF 30 10 10 100 20' 50 80 50 10 10Explanation / Answer
Alaram.java
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Alarm extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setResizable(false);
stage.setTitle("Alarm Application - Hassan Althaf");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
MainView.java
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.TextField;
public class MainView implements Initializable {
@FXML
private TextField hoursField;
@FXML
private TextField minutesField;
@FXML
private TextField secondsField;
private final AlarmController alarmController;
private Timer timer;
private boolean timerOn = false;
private boolean alarmPlayed = false;
private boolean timerValidation = true;
@FXML
private void startAlarm(ActionEvent event) {
if(!timerOn) {
this.alarmPlayed = false;
this.timerValidation = true;
String hoursString = this.hoursField.getText();
String minutesString = this.minutesField.getText();
String secondsString = this.secondsField.getText();
if(hoursString.equals("")) {
hoursString = "0";
}
if(minutesString.equals("")) {
minutesString = "0";
}
if(secondsString.equals("")) {
secondsString = "0";
}
int hours = 0;
int minutes = 0;
int seconds = 0;
try {
hours = Integer.parseInt(hoursString);
minutes = Integer.parseInt(minutesString);
seconds = Integer.parseInt(secondsString);
} catch(Exception ex) {
this.timerValidation = false;
this.showWarning("Please enter a number only for the times!", "Invalid input received");
}
int totalTime = seconds + (minutes * 60) + (hours * 3600);
if(totalTime == 0) {
this.timerValidation = false;
this.showWarning("Please set a time for the alarm before starting!", "Time not found!");
}
if(this.timerValidation) {
TimerTask task = new TimerTask() {
private final int ALARM_TIME = totalTime;
private int secondCounter = 0;
@Override
public void run()
{
if(secondCounter < ALARM_TIME) {
secondCounter++;
} else {
if(timerOn) {
alarmController.playAlarm();
alarmPlayed = true;
}
timerOn = false;
cancel();
}
}
};
this.timer = new Timer();
this.timer.schedule(task, 0, 1000);
this.timerOn = true;
}
} else {
this.showWarning("An alarm is already running at the moment! If you wish to override the current alarm, please stop it first!", "Cannot override current alarm.");
}
}
public void showWarning(String message, String title) {
Alert alert = new Alert(AlertType.WARNING);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
@FXML
private void stopAlarm(ActionEvent event) {
if(this.timerOn) {
this.timer.cancel();
this.timer.purge();
this.timerOn = false;
}
if(this.alarmPlayed) {
this.alarmController.stopAlarm();
this.alarmPlayed = false;
}
}
public MainView() {
this.alarmController = new AlarmController();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
}
MainWindow.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" maxHeight="300.0" maxWidth="500.0" minHeight="300.0" minWidth="500.0" prefHeight="300.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.HassanAlthaf.MainView">
<children>
<Label layoutX="171.0" layoutY="30.0" text="Alarm Application" textFill="#404040">
<font>
<Font name="Lato Regular" size="20.0" />
</font>
</Label>
<Label layoutX="150.0" layoutY="68.0" text="Set the timer for your alarm below" />
<TextField fx:id="hoursField" layoutX="193.0" layoutY="108.0" />
<Label layoutX="150.0" layoutY="113.0" text="Hours: " />
<Label layoutX="138.0" layoutY="150.0" text="Minutes: " />
<TextField fx:id="minutesField" layoutX="193.0" layoutY="145.0" />
<TextField fx:id="secondsField" layoutX="193.0" layoutY="182.0" />
<Label layoutX="133.0" layoutY="187.0" text="Seconds: " />
<Button layoutX="251.0" layoutY="222.0" mnemonicParsing="false" text="Start" />
<Button layoutX="309.0" layoutY="222.0" mnemonicParsing="false" text="Stop" />
</children>
</AnchorPane>
AlarmController.java
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
public class AlarmController {
private MediaPlayer mediaPlayer;
public void playAlarm()
{
Media media = new Media(getClass().getResource("alarm.mp3").toString());
this.mediaPlayer = new MediaPlayer(media);
this.mediaPlayer.play();
}
public void stopAlarm()
{
this.mediaPlayer.stop();
}
}