Advertisement
Omar_Natour

Natour, O. 4/17/16 Csc-112 Clock

Apr 17th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.74 KB | None | 0 0
  1. /*
  2. * Omar Natour
  3. * 4/16/16
  4. * Csc-112 Java 2
  5. * Hw #15
  6. * Create a clock that can tick and update to current time
  7. * Ojnatour0001@student.stcc.edu
  8. */
  9.  
  10. import javafx.animation.KeyFrame;
  11. import javafx.animation.Timeline;
  12. import javafx.application.Application;
  13. import javafx.event.ActionEvent;
  14. import javafx.event.EventHandler;
  15. import java.lang.Math;
  16. import javafx.geometry.Pos;
  17. import javafx.scene.Scene;
  18. import javafx.scene.control.Button;
  19. import javafx.scene.control.CheckBox;
  20. import javafx.scene.control.TextField;
  21. import javafx.scene.input.MouseEvent;
  22. import javafx.stage.Stage;
  23. import javafx.util.Duration;
  24. import javafx.scene.layout.*;
  25. import javafx.scene.paint.Color;
  26. import javafx.scene.shape.*;
  27. import javafx.scene.text.Text;
  28. import java.util.Calendar;
  29. import java.util.GregorianCalendar;
  30.  
  31. public class ClockMain extends Application {
  32.  
  33.     ClockGears ClockGear = new ClockGears();
  34.     Time date0 = new Time();
  35.  
  36.     static BorderPane bPane = new BorderPane();
  37.     static Scene scene = new Scene(bPane, 600, 400);
  38.  
  39.     int i = 0;
  40.  
  41.     int hour;
  42.     int min;
  43.     int sec;
  44.  
  45.     public static void main(String[] args) {
  46.         launch(args);
  47.  
  48.     }
  49.  
  50.     @Override
  51.     public void start(Stage primaryStage) {
  52.  
  53.         bPane.setCenter(ClockGear.chooseClock(0));
  54.         getButtons();
  55.  
  56.         primaryStage.setTitle("Clock");
  57.         primaryStage.setScene(scene);
  58.         primaryStage.show();
  59.         primaryStage.setResizable(false);
  60.  
  61.     }
  62.  
  63.     void getButtons() {
  64.  
  65.         HBox hbBottom = new HBox(15);
  66.  
  67.         date0.setCurrentTime();// To start out with current time.
  68.  
  69.         TextField tfHour = new TextField(String.valueOf(ClockGear.date.getHour()));
  70.         Text tHour = new Text("Hour");
  71.         TextField tfMin = new TextField(String.valueOf(ClockGear.date.getMin()));
  72.         Text tMin = new Text("Minute");
  73.         TextField tfSec = new TextField(String.valueOf(ClockGear.date.getSec()));
  74.         Text tSec = new Text("Second");
  75.  
  76.         EventHandler<ActionEvent> anim1 = e -> {
  77.  
  78.             date0.tick();
  79.             tfHour.setText(String.valueOf(ClockGear.date.hour));
  80.             tfMin.setText(String.valueOf(ClockGear.date.getMin()));
  81.             tfSec.setText(String.valueOf(ClockGear.date.getSec()));                                             /// animation of the digital clock
  82.         };
  83.  
  84.         Timeline animation1 = new Timeline(new KeyFrame(Duration.seconds(1), anim1));
  85.         animation1.setCycleCount(Timeline.INDEFINITE);
  86.         animation1.play();
  87.  
  88.         EventHandler<ActionEvent> anim2 = e -> {
  89.             ClockGear.date.setCurrentTime();
  90.             bPane.setCenter(ClockGear.chooseClock(2));
  91.  
  92.         };
  93.         Timeline animation2 = new Timeline(new KeyFrame(Duration.seconds(1), anim2));
  94.  
  95.         tfHour.setPrefColumnCount(3);
  96.         tfMin.setPrefColumnCount(3);
  97.         tfSec.setPrefColumnCount(3);
  98.  
  99.         hbBottom.getChildren().addAll(tHour, tfHour, tMin, tfMin, tSec, tfSec);
  100.         hbBottom.setAlignment(Pos.CENTER);
  101.         hbBottom.setPrefHeight(40);
  102.  
  103.         HBox hbTop = new HBox(15);
  104.  
  105.         Button bStart = new Button("Start");
  106.         Button bStop = new Button("Stop");
  107.         Button bLoadStart = new Button("Load Current Time & Start");
  108.         Button bLoad = new Button("Just Load Current Time");
  109.         CheckBox cb24Hr = new CheckBox("24Hr");
  110.  
  111.         EventHandler<ActionEvent> eventHandler = e -> {
  112.             bPane.setCenter(ClockGear.chooseClock(i));
  113.         };
  114.  
  115.         Timeline animation = new Timeline(new KeyFrame(Duration.seconds(1), eventHandler));
  116.         animation.setCycleCount(Timeline.INDEFINITE);
  117.  
  118.         EventHandler<ActionEvent> eventStart = e -> {
  119.             animation.play();
  120.             animation1.play();
  121.         };
  122.  
  123.         EventHandler<ActionEvent> eventStop = e -> {
  124.             animation.pause();
  125.             animation1.pause();
  126.         };
  127.  
  128.         EventHandler<ActionEvent> eventLoadStart = e -> {
  129.             animation1.play();// to get the digital clock working again on
  130.             animation.play();
  131.             animation2.play();
  132.             tfHour.setText(String.valueOf(ClockGear.date.hour));
  133.             tfMin.setText(String.valueOf(ClockGear.date.getMin()));
  134.             tfSec.setText(String.valueOf(ClockGear.date.getSec()));
  135.         };
  136.  
  137.         EventHandler<ActionEvent> eventLoad = e -> {
  138.             ClockGear.date.setCurrentTime();
  139.             tfHour.setText(String.valueOf(ClockGear.date.hour));
  140.             tfMin.setText(String.valueOf(ClockGear.date.getMin()));
  141.             tfSec.setText(String.valueOf(ClockGear.date.getSec()));
  142.             bPane.setCenter(ClockGear.chooseClock(i));
  143.             animation.pause();
  144.             animation1.pause();
  145.         };
  146.  
  147.         EventHandler<MouseEvent> eventHold = e -> {
  148.             animation.pause();
  149.             animation1.pause();
  150.         };
  151.  
  152.         tfHour.setOnMouseClicked(eventHold);
  153.         tfMin.setOnMouseClicked(eventHold);
  154.         tfSec.setOnMouseClicked(eventHold);
  155.  
  156.         EventHandler<ActionEvent> event24Hr = e -> {
  157.             if (i == 0)
  158.                 i = 1;
  159.             else
  160.                 i = 0;
  161.             bPane.setCenter(ClockGear.chooseClock(i));
  162.         };
  163.  
  164.         EventHandler<ActionEvent> minEnter = e -> {
  165.             try {
  166.                 int h = Integer.parseInt(tfHour.getText());
  167.                 int m = Integer.parseInt(tfMin.getText());
  168.                 int s = Integer.parseInt(tfSec.getText());
  169.  
  170.                 if (h <= 24 && m <= 59 && s <= 59 && h >= 0 && m >= 0 && s >= 0) {
  171.                     ClockGear.date.setHour(h);
  172.                     ClockGear.date.setMin(m);
  173.                     ClockGear.date.setSec(s);
  174.                 } else
  175.                     Integer.parseInt("no");
  176.             } catch (NumberFormatException ex) {
  177.                 tfHour.setText("bad");
  178.                 tfMin.setText("bad");
  179.                 tfSec.setText("bad");
  180.             }
  181.  
  182.             bPane.setCenter(ClockGear.chooseClock(2));
  183.         };
  184.  
  185.         tfHour.setOnAction(minEnter);
  186.         tfMin.setOnAction(minEnter);
  187.         tfSec.setOnAction(minEnter);
  188.  
  189.         bLoadStart.setOnAction(eventLoadStart);
  190.         bLoad.setOnAction(eventLoad);
  191.         bStart.setOnAction(eventStart);
  192.         bStop.setOnAction(eventStop);
  193.         cb24Hr.setOnAction(event24Hr);
  194.         hbTop.getChildren().addAll(bStart, bStop, bLoadStart, bLoad, cb24Hr);
  195.         hbTop.setAlignment(Pos.CENTER);
  196.         hbTop.setPrefHeight(30);
  197.  
  198.         animation.play();
  199.         ClockGear.date.setCurrentTime();
  200.  
  201.         bPane.setTop(hbTop);
  202.         bPane.setBottom(hbBottom);
  203.     }
  204. }
  205.  
  206. /////////////////////////////////////////////////////////////////////////////////////////////////
  207.  
  208. class Time {
  209.     int hour;
  210.     int min;
  211.     int sec;
  212.  
  213.     public void tick() {
  214.         this.sec++;
  215.         if (this.sec >= 60) {
  216.             this.sec = 0;
  217.             this.min++;
  218.             if (this.min >= 60) {
  219.                 this.min = 0;
  220.                 this.hour++;
  221.                 if (hour >= 13)
  222.                     this.hour %= 12;
  223.             }
  224.         }
  225.     }
  226.  
  227.     public void setCurrentTime() {
  228.         Calendar calendar = new GregorianCalendar();
  229.         this.hour = calendar.get(Calendar.HOUR_OF_DAY);
  230.         this.min = calendar.get(Calendar.MINUTE);
  231.         this.sec = calendar.get(Calendar.SECOND);
  232.     }
  233.  
  234.     public int getHour() {
  235.         return this.hour;
  236.     }
  237.  
  238.     public int getMin() {
  239.         return this.min;
  240.     }
  241.  
  242.     public int getSec() {
  243.         return this.sec;
  244.     }
  245.  
  246.     public void setHour(int h) {
  247.         this.hour = h;
  248.     }
  249.  
  250.     public void setMin(int m) {
  251.         this.min = m;
  252.     }
  253.  
  254.     public void setSec(int s) {
  255.         this.sec = s;
  256.     }
  257. }
  258.  
  259. ///////////////////////////////////////////////////////////
  260.  
  261. class ClockGears {
  262.  
  263.     public Time date = new Time();
  264.  
  265.     int hour = date.getHour();
  266.     int min = date.getMin();
  267.     int sec = date.getSec();
  268.  
  269.     public Pane chooseClock(int i) {
  270.         if (i == 1)
  271.             return paintClock24();
  272.         else if (i == 2)
  273.             return paintClock12();
  274.         else if (i == 3) {
  275.             date.setCurrentTime();
  276.             return paintClock12();
  277.         } else
  278.             return paintClock12();
  279.     }
  280.  
  281.     Pane paintClock12() {
  282.  
  283.         int clockRadius = 155;
  284.         int centerX = 300;
  285.         int centerY = 173;
  286.  
  287.         hour = date.getHour();
  288.         min = date.getMin();
  289.         sec = date.getSec();
  290.  
  291.         date.tick();
  292.  
  293.         Pane clock = new Pane();
  294.  
  295.         Circle cOutline = new Circle(centerX, centerY, clockRadius);
  296.         cOutline.setFill(Color.WHITE);
  297.         cOutline.setStroke(Color.BLACK);
  298.         cOutline.setStrokeWidth(2);
  299.  
  300.         Circle cCenter = new Circle(centerX, centerY, 3.5);
  301.  
  302.         Text t12 = new Text(293, 32, "12");
  303.         Text t3 = new Text(445, 176.5, "3");
  304.         Text t6 = new Text(297, 324, "6");
  305.         Text t9 = new Text(149, 176.5, "9");
  306.  
  307.         int totalSeconds = 3600 * (hour % 12) + 60 * min + sec;
  308.         double hourAngle = ((double) totalSeconds / 3600.0) * 2.0 * Math.PI / 12.0;
  309.         int minuteSeconds = 60 * min + sec;
  310.         double minuteAngle = ((double) minuteSeconds / 60.0) * 2.0 * Math.PI / 60.0;
  311.         double secondAngle = ((double) sec) * 2.0 * Math.PI / 60.0;
  312.  
  313.         double sLength = clockRadius * 0.8;
  314.         double secondX = centerX + sLength * Math.sin(secondAngle);
  315.         double secondY = centerY - sLength * Math.cos(secondAngle);
  316.         Line sLine = new Line(centerX, centerY, secondX, secondY);
  317.         sLine.setStroke(Color.RED);
  318.  
  319.         double mLength = clockRadius * 0.65;
  320.         double xMinute = centerX + mLength * Math.sin(minuteAngle);
  321.         double minuteY = centerY - mLength * Math.cos(minuteAngle);
  322.         Line mLine = new Line(centerX, centerY, xMinute, minuteY);
  323.         mLine.setStrokeWidth(3);
  324.         mLine.setStroke(Color.BLUE);
  325.  
  326.         double hLength = clockRadius * 0.5;
  327.         double hourX = centerX + hLength * Math.sin(hourAngle);
  328.         double hourY = centerY - hLength * Math.cos(hourAngle);
  329.         Line hLine = new Line(centerX, centerY, hourX, hourY);
  330.         hLine.setStrokeWidth(5);
  331.         hLine.setStroke(Color.GREEN);
  332.  
  333.         clock.getChildren().addAll(cOutline, t12, t3, t6, t9, sLine, mLine, hLine, cCenter);
  334.  
  335.         return clock;
  336.     }
  337.  
  338.     ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  339.  
  340.     Pane paintClock24() {
  341.  
  342.         int clockRadius = 155;
  343.         int centerX = 300;
  344.         int centerY = 173;
  345.  
  346.         Pane clock = new Pane();
  347.  
  348.         Circle cOutline = new Circle(300, 173, 155);
  349.         cOutline.setFill(Color.WHITE);
  350.         cOutline.setStroke(Color.BLACK);
  351.         cOutline.setStrokeWidth(2);
  352.  
  353.         Circle cCenter = new Circle(300, 173, 3);
  354.  
  355.         Text t24 = new Text(293, 32, "24");
  356.         Text t6 = new Text(444, 176.5, "6");
  357.         Text t12 = new Text(293, 324, "12");
  358.         Text t18 = new Text(149, 176.5, "18");
  359.  
  360.         int hour = date.getHour();
  361.         int min = date.getMin();
  362.         int sec = date.getSec();
  363.  
  364.         date.tick();
  365.  
  366.         int totalSeconds = 3600 * (hour) + 60 * min + sec;
  367.         double hourAngle = (((double) totalSeconds / 7200.0) * 2.0 * Math.PI / 12.0);
  368.         int minuteSeconds = 60 * min + sec;
  369.         double minuteAngle = ((double) minuteSeconds / 60.0) * 2.0 * Math.PI / 60.0;
  370.         double secondAngle = ((double) sec) * 2.0 * Math.PI / 60.0;
  371.  
  372.         double sLength = clockRadius * 0.8;
  373.         double secondX = centerX + sLength * Math.sin(secondAngle);
  374.         double secondY = centerY - sLength * Math.cos(secondAngle);
  375.         Line sLine = new Line(centerX, centerY, secondX, secondY);
  376.         sLine.setStroke(Color.RED);
  377.  
  378.         double mLength = clockRadius * 0.65;
  379.         double xMinute = centerX + mLength * Math.sin(minuteAngle);
  380.         double minuteY = centerY - mLength * Math.cos(minuteAngle);
  381.         Line mLine = new Line(centerX, centerY, xMinute, minuteY);
  382.         mLine.setStrokeWidth(3);
  383.         mLine.setStroke(Color.BLUE);
  384.  
  385.         double hLength = clockRadius * 0.5;
  386.         double hourX = centerX + hLength * Math.sin(hourAngle);
  387.         double hourY = centerY - hLength * Math.cos(hourAngle);
  388.         Line hLine = new Line(centerX, centerY, hourX, hourY);
  389.         hLine.setStrokeWidth(5);
  390.         hLine.setStroke(Color.GREEN);
  391.  
  392.         clock.getChildren().addAll(cOutline, t24, t6, t12, t18, hLine, mLine, sLine, cCenter);
  393.  
  394.         return clock;
  395.     }
  396. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement