License text

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER 
 * Copyright  2008, 2010 Oracle and/or its affiliates.  All rights reserved. 
 * Use is subject to license terms.
 * 
 * This file is available and licensed under the following license:
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met: 
 * 
 *   * Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer. 
 *
 *   * Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *
 *   * Neither the name of Oracle Corporation nor the names of its contributors 
 *     may be used to endorse or promote products derived from this software 
 *     without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

    

package analogclock;

//Author: Vaibhav Choudhary
 
import java.util.Calendar;
import java.util.Date;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.paint.RadialGradient;
import javafx.scene.paint.Stop;
import javafx.scene.Scene;
import javafx.scene.shape.ArcTo;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Transform;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

// varies according to scene size

var width:Number = bind scene.width;
var height:Number = bind scene.height;

// list of months
var months:String[] = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN",
    "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
var radius:Number = bind width/3;            // Decides the main size of the clock
var centerX:Number = bind scene.width/2;     // Shifting the X center
var centerY:Number = bind scene.height/2;    // Shifting the Y center
var hours:Number;
var minutes:Number;
var seconds:Number;
var date:String;
var month:String;
var year:String;
var combination: String;

var time = Timeline {
    repeatCount: Timeline.INDEFINITE
    keyFrames: [
        KeyFrame {
            time: 1s
            action: function() {
                actionOnTick();
            }
        }
    ]
}

time.play();

//instance of calendar
function createCalendar() {
    var date = new Date();
    def calendar = Calendar.getInstance();
    calendar.setTime(new Date()); // fix for mobile
    calendar
}
// action taken on one tick
function actionOnTick () {
    var calendar = createCalendar();
    seconds = calendar.get(Calendar.SECOND);
    minutes = calendar.get(Calendar.MINUTE);
    hours = calendar.get(Calendar.HOUR);
    date = String.valueOf(calendar.get(Calendar.DATE));
    if(date.length() != 2)  {
        date = "0{date}";
    }
    month = months[(calendar.get(Calendar.MONTH))];
    year = String.valueOf((calendar.get(Calendar.YEAR)));
    combination = "{date}-{month}-{year}";
}
//scene which can be dynamic
var scene:Scene = Scene {
    width: 233
    height: 287
    fill: Color.TRANSPARENT
    content: [
        Group {
            translateX: bind centerX 
            translateY: bind centerY 
            content: bind [
                Circle {
                    radius: radius + 20
                    fill: RadialGradient {
                        centerX: 0
                        centerY: 0
                        radius: radius + 20
                        proportional: false
                        stops: [

                            Stop {
                                offset: 0.9
                                color: Color.SILVER
                            },
                            Stop {
                                offset: 1.0
                                color: Color.BLACK
                            }
                        ]
                    }

                },
                Circle {
                    radius: radius + 10
                    stroke: Color.BLACK
                    fill: RadialGradient {
                        centerX: 0
                        centerY: 0
                        radius: 90
                        proportional: false
                        stops: [
                            Stop {
                                offset: 0.0
                                color: Color.WHITE
                            },
                            Stop {
                                offset: 1.0
                                color: Color.CADETBLUE
                            }
                        ]
                    }

                },
                Rectangle {
                    x: -35,
                    y: 2 * radius / 3 - 15
                    width: 71
                    height: 20
                    fill: Color.GRAY
                    opacity:0.4
                    strokeWidth: 2
                    stroke: Color.BLACK
                    arcHeight:10
                    arcWidth:10
                },
                Text {
                    font: Font {
                        size: 11
                        name: "Arial"
                    }
                    x: -31 ,
                    y: 2 * radius / 3
                    content: bind combination
                },
                for (i in [3, 6, 9, 12])        // Setting the main digits 3,6,9,12
                Text {
                    transforms:bind [
                        Transform.translate(-5, 5)
                    ]
                    x: radius * (( i + 0 ) mod 2 * ( 2 - i / 3))
                    y: radius * (( i + 1 ) mod 2 * ( 3 - i / 3))
                    content: "{i}"
                    font: Font {
                        size: 11
                        name: "Arial"
                    }
                },
                // making dots on rest of the place
                for (i in [1..12])
                if (i mod 3 != 0 ) then Circle {
                    transforms:Rotate {
                        angle: 30 * i
                    }
                    centerX: radius
                    radius: 3
                    fill: Color.BLACK
                } else [ ],

                // circle at the core center
                Circle {
                    radius: 5
                    fill: Color.BLACK
                },
                // one more circle inside the above circle
                Circle {
                    radius: 3
                    fill: Color.GRAY
                },
                // second arm
                Line {
                    transforms: Rotate {
                        angle: bind seconds * 6
                    }
                    endY: -radius - 3
                    strokeWidth: 2
                    stroke: Color.RED

                },
                // hour arm
                Path {
                    transforms: Rotate {
                        angle: bind (hours + minutes / 60) * 30 - 90
                    }
                    fill: Color.BLACK
                    elements: [
                        MoveTo { x: 4, y: 4 },
                        ArcTo {
                            x: 4
                            y: -4
                            radiusX: 1
                            radiusY: 1
                        },
                        LineTo { x: radius - 15 y: 0 },
                    ]
                },
                // minute arm
                Path {
                    transforms: Rotate {
                        angle: bind minutes * 6 - 90
                    }
                    fill: Color.BLACK
                    elements: [
                        MoveTo { x: 4, y: 4 },
                        ArcTo {
                            x: 4
                            y: -4
                            radiusX: 1
                            radiusY: 1
                        },
                        LineTo{ x: radius y: 0 },
                    ]
                }
            ]
        }
    ]
};

Stage {
    style: StageStyle.TRANSPARENT 
    title: "Analog Clock[JavaFX Samples]"
    scene: bind scene
    width: 240
    height: 320
}