Monday Dec 01, 2008

A simple bad digital Clock

Here how to use Java code inside JavaFX and make a simple digital clock. Bad because i have not put the proper makeup :).

//Clock.fx

package digitalclock;

import javafx.scene.\*;
import javafx.scene.paint.\*;
import javafx.scene.text.\*;
import javafx.scene.transform.\*;
import javafx.animation.\*;
import javafx.scene.shape.\*;

// Java Legacy
import java.util.\*;

var months:String[] = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN",
                "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];

public class Clock extends CustomNode {

   var radius = -20;
    // shifting the X center
    var centerX: Number = 50 ;
    // shifting the Y center
    var centerY: Number = 50 ;
    var hours:String;
    var minutes:String;
    var seconds:String;
    var date:String;
    var month:String;
    var year:String;
    var combination: String;

    // intializing the watch
    init {
        var timeline = Timeline {
            repeatCount: Timeline.INDEFINITE
            keyFrames : [
                KeyFrame {
                    time : 1s
                    action: function() {
                        actionOnTick();
                    }
                }
            ]
        }
        timeline.play();
    }
    function createCalendar() {
        var date = new Date();
        def calendar = Calendar.getInstance();
        calendar.setTime(new Date()); // fix for mobile
        calendar
    }
    // action taken on one tick
    public function actionOnTick () {
        var calendar = createCalendar();
        seconds = String.valueOf(calendar.get(Calendar.SECOND));
        if(seconds.length()!=2)  {
            seconds = "0{seconds}";
        }
        minutes = String.valueOf(calendar.get(Calendar.MINUTE));
        if(minutes.length()!=2)  {
            minutes = "0{minutes}";
        }
        hours = String.valueOf(calendar.get(Calendar.HOUR));
         if(hours.length()!=2)  {
            hours = "0{hours}";
        }
        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 = "{hours}:{minutes}:{seconds}  {date}-{month}-{year}";
    }

    // overriding the method create()
    public override function create(): Node {
        return Group {
            transforms: [
                Transform.translate(centerX, centerY)
            ]
            content: [

                Rectangle {
                    x: -35, y: 2\*radius/3 - 15
                    width: 120 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
                },
                  ]
                }
        };
} 


//Main.fx
package digitalclock;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.text.Font;

Stage {
    title: "Digital Clock"
    width: 160
    height: 100
    scene: Scene {
        content: Clock{}
    }
}
About

Hi, I am Vaibhav Choudhary working in Sun. This blog is all about simple concept of Java and JavaFX.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today