Advertisement
rjsantiago0001

Clock Pane

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