Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Omar Natour
- * 4/16/16
- * Csc-112 Java 2
- * Hw #15
- * Create a clock that can tick and update to current time
- * Ojnatour0001@student.stcc.edu
- */
- import javafx.animation.KeyFrame;
- import javafx.animation.Timeline;
- import javafx.application.Application;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import java.lang.Math;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.CheckBox;
- import javafx.scene.control.TextField;
- import javafx.scene.input.MouseEvent;
- import javafx.stage.Stage;
- import javafx.util.Duration;
- import javafx.scene.layout.*;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.*;
- import javafx.scene.text.Text;
- import java.util.Calendar;
- import java.util.GregorianCalendar;
- public class ClockMain extends Application {
- ClockGears ClockGear = new ClockGears();
- Time date0 = new Time();
- static BorderPane bPane = new BorderPane();
- static Scene scene = new Scene(bPane, 600, 400);
- int i = 0;
- int hour;
- int min;
- int sec;
- public static void main(String[] args) {
- launch(args);
- }
- @Override
- public void start(Stage primaryStage) {
- bPane.setCenter(ClockGear.chooseClock(0));
- getButtons();
- primaryStage.setTitle("Clock");
- primaryStage.setScene(scene);
- primaryStage.show();
- primaryStage.setResizable(false);
- }
- void getButtons() {
- HBox hbBottom = new HBox(15);
- date0.setCurrentTime();// To start out with current time.
- TextField tfHour = new TextField(String.valueOf(ClockGear.date.getHour()));
- Text tHour = new Text("Hour");
- TextField tfMin = new TextField(String.valueOf(ClockGear.date.getMin()));
- Text tMin = new Text("Minute");
- TextField tfSec = new TextField(String.valueOf(ClockGear.date.getSec()));
- Text tSec = new Text("Second");
- EventHandler<ActionEvent> anim1 = e -> {
- date0.tick();
- tfHour.setText(String.valueOf(ClockGear.date.hour));
- tfMin.setText(String.valueOf(ClockGear.date.getMin()));
- tfSec.setText(String.valueOf(ClockGear.date.getSec())); /// animation of the digital clock
- };
- Timeline animation1 = new Timeline(new KeyFrame(Duration.seconds(1), anim1));
- animation1.setCycleCount(Timeline.INDEFINITE);
- animation1.play();
- EventHandler<ActionEvent> anim2 = e -> {
- ClockGear.date.setCurrentTime();
- bPane.setCenter(ClockGear.chooseClock(2));
- };
- Timeline animation2 = new Timeline(new KeyFrame(Duration.seconds(1), anim2));
- tfHour.setPrefColumnCount(3);
- tfMin.setPrefColumnCount(3);
- tfSec.setPrefColumnCount(3);
- hbBottom.getChildren().addAll(tHour, tfHour, tMin, tfMin, tSec, tfSec);
- hbBottom.setAlignment(Pos.CENTER);
- hbBottom.setPrefHeight(40);
- HBox hbTop = new HBox(15);
- Button bStart = new Button("Start");
- Button bStop = new Button("Stop");
- Button bLoadStart = new Button("Load Current Time & Start");
- Button bLoad = new Button("Just Load Current Time");
- CheckBox cb24Hr = new CheckBox("24Hr");
- EventHandler<ActionEvent> eventHandler = e -> {
- bPane.setCenter(ClockGear.chooseClock(i));
- };
- Timeline animation = new Timeline(new KeyFrame(Duration.seconds(1), eventHandler));
- animation.setCycleCount(Timeline.INDEFINITE);
- EventHandler<ActionEvent> eventStart = e -> {
- animation.play();
- animation1.play();
- };
- EventHandler<ActionEvent> eventStop = e -> {
- animation.pause();
- animation1.pause();
- };
- EventHandler<ActionEvent> eventLoadStart = e -> {
- animation1.play();// to get the digital clock working again on
- animation.play();
- animation2.play();
- tfHour.setText(String.valueOf(ClockGear.date.hour));
- tfMin.setText(String.valueOf(ClockGear.date.getMin()));
- tfSec.setText(String.valueOf(ClockGear.date.getSec()));
- };
- EventHandler<ActionEvent> eventLoad = e -> {
- ClockGear.date.setCurrentTime();
- tfHour.setText(String.valueOf(ClockGear.date.hour));
- tfMin.setText(String.valueOf(ClockGear.date.getMin()));
- tfSec.setText(String.valueOf(ClockGear.date.getSec()));
- bPane.setCenter(ClockGear.chooseClock(i));
- animation.pause();
- animation1.pause();
- };
- EventHandler<MouseEvent> eventHold = e -> {
- animation.pause();
- animation1.pause();
- };
- tfHour.setOnMouseClicked(eventHold);
- tfMin.setOnMouseClicked(eventHold);
- tfSec.setOnMouseClicked(eventHold);
- EventHandler<ActionEvent> event24Hr = e -> {
- if (i == 0)
- i = 1;
- else
- i = 0;
- bPane.setCenter(ClockGear.chooseClock(i));
- };
- EventHandler<ActionEvent> minEnter = e -> {
- try {
- int h = Integer.parseInt(tfHour.getText());
- int m = Integer.parseInt(tfMin.getText());
- int s = Integer.parseInt(tfSec.getText());
- if (h <= 24 && m <= 59 && s <= 59 && h >= 0 && m >= 0 && s >= 0) {
- ClockGear.date.setHour(h);
- ClockGear.date.setMin(m);
- ClockGear.date.setSec(s);
- } else
- Integer.parseInt("no");
- } catch (NumberFormatException ex) {
- tfHour.setText("bad");
- tfMin.setText("bad");
- tfSec.setText("bad");
- }
- bPane.setCenter(ClockGear.chooseClock(2));
- };
- tfHour.setOnAction(minEnter);
- tfMin.setOnAction(minEnter);
- tfSec.setOnAction(minEnter);
- bLoadStart.setOnAction(eventLoadStart);
- bLoad.setOnAction(eventLoad);
- bStart.setOnAction(eventStart);
- bStop.setOnAction(eventStop);
- cb24Hr.setOnAction(event24Hr);
- hbTop.getChildren().addAll(bStart, bStop, bLoadStart, bLoad, cb24Hr);
- hbTop.setAlignment(Pos.CENTER);
- hbTop.setPrefHeight(30);
- animation.play();
- ClockGear.date.setCurrentTime();
- bPane.setTop(hbTop);
- bPane.setBottom(hbBottom);
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////
- class Time {
- int hour;
- int min;
- int sec;
- public void tick() {
- this.sec++;
- if (this.sec >= 60) {
- this.sec = 0;
- this.min++;
- if (this.min >= 60) {
- this.min = 0;
- this.hour++;
- if (hour >= 13)
- this.hour %= 12;
- }
- }
- }
- public void setCurrentTime() {
- Calendar calendar = new GregorianCalendar();
- this.hour = calendar.get(Calendar.HOUR_OF_DAY);
- this.min = calendar.get(Calendar.MINUTE);
- this.sec = calendar.get(Calendar.SECOND);
- }
- public int getHour() {
- return this.hour;
- }
- public int getMin() {
- return this.min;
- }
- public int getSec() {
- return this.sec;
- }
- public void setHour(int h) {
- this.hour = h;
- }
- public void setMin(int m) {
- this.min = m;
- }
- public void setSec(int s) {
- this.sec = s;
- }
- }
- ///////////////////////////////////////////////////////////
- class ClockGears {
- public Time date = new Time();
- int hour = date.getHour();
- int min = date.getMin();
- int sec = date.getSec();
- public Pane chooseClock(int i) {
- if (i == 1)
- return paintClock24();
- else if (i == 2)
- return paintClock12();
- else if (i == 3) {
- date.setCurrentTime();
- return paintClock12();
- } else
- return paintClock12();
- }
- Pane paintClock12() {
- int clockRadius = 155;
- int centerX = 300;
- int centerY = 173;
- hour = date.getHour();
- min = date.getMin();
- sec = date.getSec();
- date.tick();
- Pane clock = new Pane();
- Circle cOutline = new Circle(centerX, centerY, clockRadius);
- cOutline.setFill(Color.WHITE);
- cOutline.setStroke(Color.BLACK);
- cOutline.setStrokeWidth(2);
- Circle cCenter = new Circle(centerX, centerY, 3.5);
- Text t12 = new Text(293, 32, "12");
- Text t3 = new Text(445, 176.5, "3");
- Text t6 = new Text(297, 324, "6");
- Text t9 = new Text(149, 176.5, "9");
- int totalSeconds = 3600 * (hour % 12) + 60 * min + sec;
- double hourAngle = ((double) totalSeconds / 3600.0) * 2.0 * Math.PI / 12.0;
- int minuteSeconds = 60 * min + sec;
- double minuteAngle = ((double) minuteSeconds / 60.0) * 2.0 * Math.PI / 60.0;
- double secondAngle = ((double) sec) * 2.0 * Math.PI / 60.0;
- double sLength = clockRadius * 0.8;
- double secondX = centerX + sLength * Math.sin(secondAngle);
- double secondY = centerY - sLength * Math.cos(secondAngle);
- Line sLine = new Line(centerX, centerY, secondX, secondY);
- sLine.setStroke(Color.RED);
- double mLength = clockRadius * 0.65;
- double xMinute = centerX + mLength * Math.sin(minuteAngle);
- double minuteY = centerY - mLength * Math.cos(minuteAngle);
- Line mLine = new Line(centerX, centerY, xMinute, minuteY);
- mLine.setStrokeWidth(3);
- mLine.setStroke(Color.BLUE);
- double hLength = clockRadius * 0.5;
- double hourX = centerX + hLength * Math.sin(hourAngle);
- double hourY = centerY - hLength * Math.cos(hourAngle);
- Line hLine = new Line(centerX, centerY, hourX, hourY);
- hLine.setStrokeWidth(5);
- hLine.setStroke(Color.GREEN);
- clock.getChildren().addAll(cOutline, t12, t3, t6, t9, sLine, mLine, hLine, cCenter);
- return clock;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- Pane paintClock24() {
- int clockRadius = 155;
- int centerX = 300;
- int centerY = 173;
- Pane clock = new Pane();
- Circle cOutline = new Circle(300, 173, 155);
- cOutline.setFill(Color.WHITE);
- cOutline.setStroke(Color.BLACK);
- cOutline.setStrokeWidth(2);
- Circle cCenter = new Circle(300, 173, 3);
- Text t24 = new Text(293, 32, "24");
- Text t6 = new Text(444, 176.5, "6");
- Text t12 = new Text(293, 324, "12");
- Text t18 = new Text(149, 176.5, "18");
- int hour = date.getHour();
- int min = date.getMin();
- int sec = date.getSec();
- date.tick();
- int totalSeconds = 3600 * (hour) + 60 * min + sec;
- double hourAngle = (((double) totalSeconds / 7200.0) * 2.0 * Math.PI / 12.0);
- int minuteSeconds = 60 * min + sec;
- double minuteAngle = ((double) minuteSeconds / 60.0) * 2.0 * Math.PI / 60.0;
- double secondAngle = ((double) sec) * 2.0 * Math.PI / 60.0;
- double sLength = clockRadius * 0.8;
- double secondX = centerX + sLength * Math.sin(secondAngle);
- double secondY = centerY - sLength * Math.cos(secondAngle);
- Line sLine = new Line(centerX, centerY, secondX, secondY);
- sLine.setStroke(Color.RED);
- double mLength = clockRadius * 0.65;
- double xMinute = centerX + mLength * Math.sin(minuteAngle);
- double minuteY = centerY - mLength * Math.cos(minuteAngle);
- Line mLine = new Line(centerX, centerY, xMinute, minuteY);
- mLine.setStrokeWidth(3);
- mLine.setStroke(Color.BLUE);
- double hLength = clockRadius * 0.5;
- double hourX = centerX + hLength * Math.sin(hourAngle);
- double hourY = centerY - hLength * Math.cos(hourAngle);
- Line hLine = new Line(centerX, centerY, hourX, hourY);
- hLine.setStrokeWidth(5);
- hLine.setStroke(Color.GREEN);
- clock.getChildren().addAll(cOutline, t24, t6, t12, t18, hLine, mLine, sLine, cCenter);
- return clock;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement