Advertisement
luliu

HW #15 Clock Animation Pane

Apr 15th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.56 KB | None | 0 0
  1.  
  2. /*
  3.  * Name: Lu Liu
  4.  * Date: 4/15/2016
  5.  * Course Number: CSC-112
  6.  * Course Name: Intermediate Topics in Java Programming
  7.  * Email: lliu0001@student.stcc.edu
  8.  *
  9.  * Assignment: HW # 15
  10.  * Programe Description:
  11.  * Clock Animation Pane
  12.  */
  13. package clockAnimation;
  14.  
  15. import java.time.Clock;
  16. import java.util.Calendar;
  17. import java.util.GregorianCalendar;
  18.  
  19. import javafx.animation.KeyFrame;
  20. import javafx.animation.Timeline;
  21. import javafx.application.Application;
  22. import javafx.scene.Scene;
  23. import javafx.scene.layout.Pane;
  24. import javafx.scene.paint.Color;
  25. import javafx.scene.shape.Circle;
  26. import javafx.scene.shape.Line;
  27. import javafx.scene.text.Text;
  28. import javafx.stage.Stage;
  29. import javafx.util.Duration;
  30.  
  31. public class ClockAnimate extends Pane {
  32.     private int hour;
  33.     private int minute;
  34.     private int second;
  35.  
  36.     /** Construct a default clock with the current time */
  37.     public ClockAnimate() {
  38.         setCurrentTime();
  39.     }
  40.  
  41.     /** Construct a clock with specified hour, minute, and second */
  42.     public ClockAnimate(int hour, int minute, int second) {
  43.         this.hour = hour;
  44.         this.minute = minute;
  45.         this.second = second;
  46.     }
  47.  
  48.     /** Return hour */
  49.     public int getHour() {
  50.         return hour;
  51.     }
  52.  
  53.     /** Set a new hour */
  54.     public void setHour(int hour) {
  55.         this.hour = hour;
  56.         paintClock();
  57.     }
  58.  
  59.     /** Return minute */
  60.     public int getMinute() {
  61.         return minute;
  62.     }
  63.  
  64.     /** Set a new minute */
  65.     public void setMinute(int minute) {
  66.         this.minute = minute;
  67.         paintClock();
  68.     }
  69.  
  70.     /** Return second */
  71.     public int getSecond() {
  72.         return second;
  73.     }
  74.  
  75.     /** Set a new second */
  76.     public void setSecond(int second) {
  77.         this.second = second;
  78.         paintClock();
  79.     }
  80.  
  81.     // public void runClock() {
  82.     // tick();
  83.     // paintClock();
  84.     // }
  85.  
  86.     /* Set the current time for the clock */
  87.     public void setCurrentTime() {
  88.         // Construct a calendar for the current date and time
  89.         Calendar calendar = new GregorianCalendar();
  90.         // Set current hour, minute and second
  91.         this.hour = calendar.get(Calendar.HOUR_OF_DAY);
  92.         this.minute = calendar.get(Calendar.MINUTE);
  93.         this.second = calendar.get(Calendar.SECOND);
  94.         paintClock(); // Repaint the clock
  95.     }
  96.  
  97.     /** Paint the clock */
  98.     public void paintClock() {
  99.         // Initialize clock parameters
  100.         double clockRadius = Math.min(getWidth(), getHeight()) * 0.8 * 0.5;
  101.         double centerX = getWidth() / 2;
  102.         double centerY = getHeight() / 2;
  103.  
  104.         // Draw circle
  105.         Circle circle = new Circle(centerX, centerY, clockRadius);
  106.         circle.setFill(Color.WHITE);
  107.         circle.setStroke(Color.BLACK);
  108.         Text t1 = new Text(centerX - 5, centerY - clockRadius + 12, "12");
  109.         Text t2 = new Text(centerX - clockRadius + 3, centerY + 5, "9");
  110.         Text t3 = new Text(centerX + clockRadius - 10, centerY + 3, "3");
  111.         Text t4 = new Text(centerX - 3, centerY + clockRadius - 3, "6");
  112.  
  113.         // Angle Calculations
  114.         int totalNoOfSeconds = 3600 * (this.hour % 12) + 60 * this.minute + this.second;
  115.         double hourAngle = ((double) totalNoOfSeconds / 3600.0) * 2.0 * Math.PI / 12.0;
  116.         int minuteSeconds = 60 * this.minute + this.second;
  117.         double minuteAngle = ((double) minuteSeconds / 60.0) * 2.0 * Math.PI / 60.0;
  118.         int seconds = this.second;
  119.         double secondAngle = ((double) seconds) * 2.0 * Math.PI / 60.0;
  120.  
  121.         // Draw second hand
  122.         double sLength = clockRadius * 0.8;
  123.         double secondX = centerX + sLength * Math.sin(secondAngle);
  124.         double secondY = centerY - sLength * Math.cos(secondAngle);
  125.         Line sLine = new Line(centerX, centerY, secondX, secondY);
  126.         sLine.setStroke(Color.RED);
  127.  
  128.         // Draw minute hand
  129.         double mLength = clockRadius * 0.65;
  130.         double xMinute = centerX + mLength * Math.sin(minuteAngle);
  131.         double minuteY = centerY - mLength * Math.cos(minuteAngle);
  132.         Line mLine = new Line(centerX, centerY, xMinute, minuteY);
  133.         mLine.setStroke(Color.BLUE);
  134.  
  135.         // Draw hour hand
  136.         double hLength = clockRadius * 0.5;
  137.         double hourX = centerX + hLength * Math.sin(hourAngle);
  138.         double hourY = centerY - hLength * Math.cos(hourAngle);
  139.         Line hLine = new Line(centerX, centerY, hourX, hourY);
  140.         hLine.setStroke(Color.GREEN);
  141.  
  142.         getChildren().clear();
  143.         getChildren().addAll(circle, t1, t2, t3, t4, sLine, mLine, hLine);
  144.     }
  145.  
  146.     @Override
  147.     public void setWidth(double width) {
  148.         super.setWidth(width);
  149.         paintClock();
  150.     }
  151.  
  152.     @Override
  153.     public void setHeight(double height) {
  154.         super.setHeight(height);
  155.         paintClock();
  156.     }
  157.  
  158.     public void tick() {
  159.         this.second += 1;
  160.         if (this.second >= 60) {
  161.             this.second = 0;
  162.             this.minute++;
  163.             if (this.minute >= 60) {
  164.                 this.minute = 0;
  165.                 this.hour++;
  166.                 if (hour >= 13)
  167.                     this.hour %= 12;
  168.             }
  169.         }
  170.         paintClock();
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement