JavaFx Date widget
JavaFx Calendar widget
During the weekend i embarked on a widget building mission of course in javafx,It simply displays the date and time ,with fade transition in the colons.,the fonts are from the enzo library.The setAnimation method its an AnimationTimer that updates the UI .Here's the snapshots
default |
The source code |
//Effect glow and reflection
Reflection ref=new Reflection();
ref.setFraction(1.0);
Glow glow=new Glow();
glow.setLevel(0.8);
Light.Spot light = new Light.Spot();
light.setX(50);
light.setY(-50);
light.setZ(350);
light.setPointsAtX(0);
light.setPointsAtY(0);
light.setPointsAtZ(-100);
light.setSpecularExponent(2);
Lighting lighting = new Lighting();
lighting.setLight(light);
lighting.setSurfaceScale(153.0);
//clode button
Button close_btn=new Button();
close_btn.setId("close_btn");
close_btn.setGraphic(new ImageView("file:close.png"));
close_btn.setVisible(false);
close_btn.setOnMouseEntered(new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent event) {
close_btn.setCursor(Cursor.HAND);
}
});
close_btn.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
System.exit(0);
}
});
//root pane
GridPane root=new GridPane();
root.setVgap(3);
root.setId("grid");
root.setPrefSize(150, 250);
//half top widget
StackPane stack_pane=new StackPane();
stack_pane.setPrefSize(150, 125);
Rectangle top_rect=new Rectangle(150,250);
top_rect.setArcHeight(20);
top_rect.setArcWidth(20);
stack_pane.setClip(top_rect);
stack_pane.setId("st1");
root.add(stack_pane, 0, 0);
//text node representing day of the week
Text day_txt=new Text();
day_txt.setUserData("day");
setAnimation(day_txt).start();
day_txt.setFill(Color.WHITE);
day_txt.setFont(Fonts.digital(50));
day_txt.setEffect(ref);
//text node representing day of the month
Text month_txt=new Text();
month_txt.setUserData("month");
setAnimation(month_txt).start();
month_txt.setId("month_txt");
month_txt.setEffect(new Glow(0.8));
month_txt.setFill(Color.AQUA);
month_txt.setFont(Fonts.robotoBold(30));
StackPane.setAlignment(month_txt, Pos.TOP_RIGHT);
StackPane.setMargin(month_txt,new Insets(0,5,0,0));
stack_pane.getChildren().addAll(day_txt,month_txt);
//half bottom widget
AnchorPane anchor_pane=new AnchorPane();
anchor_pane.setPrefSize(150, 125);
anchor_pane.setRotate(180);
Rectangle r2=new Rectangle(150,250);
r2.setArcHeight(20);
r2.setArcWidth(20);
anchor_pane.setId("st2");
anchor_pane.setClip(r2);
//onMouse entered show icon
anchor_pane.setOnMouseEntered(new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent event) {
close_btn.setCursor(Cursor.HAND);
close_btn.setVisible(true);
}
});
//onMouse exited hide icon
close_btn.setOnMouseExited(new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent event) {
close_btn.setVisible(false);
}
});
//text node representing hour
Text hour_txt=new Text();
hour_txt.setUserData("hour");
setAnimation(hour_txt).start();
hour_txt.setEffect(ref);
hour_txt.setRotate(180);
hour_txt.setLayoutX(130-40);
hour_txt.setLayoutY(125/2+offset);
hour_txt.setFill(Color.AQUA);
hour_txt.setFont(Fonts.digital(55));
anchor_pane.getChildren().add(hour_txt);
//text node simulating seconds in a fadetransition
Text blink_txt=new Text(":");
blink_txt.setRotate(180);
blink_txt.setEffect(ref);
blink_txt.setLayoutX(130-60);
blink_txt.setLayoutY(125/2+offset);
blink_txt.setFill(Color.AQUA);
blink_txt.setFont(Fonts.robotoBold(55));
FadeTransition ft = new FadeTransition(Duration.millis(1000), blink_txt);
ft.setFromValue(1.0);
ft.setToValue(0.0);
ft.setByValue(0.1);
ft.setInterpolator(Interpolator.EASE_BOTH);
ft.setCycleCount(Timeline.INDEFINITE);
ft.play();
anchor_pane.getChildren().add(blink_txt);
//text node representing minutes
Text min_txt=new Text();
min_txt.setUserData("min");
setAnimation(min_txt).start();
min_txt.setEffect(ref);
min_txt.setRotate(180);
min_txt.setLayoutX(130-110);
min_txt.setLayoutY(125/2+offset);
min_txt.setFill(Color.AQUA);
min_txt.setFont(Fonts.digital(55));
anchor_pane.getChildren().add(min_txt);
//text node representing am/pm
Text am_pm_txt=new Text();
am_pm_txt.setUserData("am_pm");
setAnimation(am_pm_txt).start();
am_pm_txt.setFill(Color.AQUA);
am_pm_txt.setEffect(glow);
am_pm_txt.setFont(Fonts.digital(14));
am_pm_txt.setRotate(180);
am_pm_txt.setLayoutX(130-115);
am_pm_txt.setLayoutY(100);
anchor_pane.getChildren().addAll(am_pm_txt);
root.add(anchor_pane, 0, 1);
root.add(close_btn, 0, 2);
root.setEffect(lighting);
The Animation method
private AnimationTimer setAnimation(Text t){
AnimationTimer tm=new AnimationTimer() {
@Override
public void handle(long now) {
Calendar cal=Calendar.getInstance();
Platform.runLater(new Runnable(){
@Override
public void run() {
String data=t.getUserData().toString();
if(data.equalsIgnoreCase("min")){
t.setText(String.valueOf(cal.get(Calendar.MINUTE)).matches("\\d{1}")? 0+String.valueOf(cal.get(Calendar.MINUTE)):Str ing.valueOf(cal.get(Calendar.MINUTE)));
}else if(data.equalsIgnoreCase("hour")){
t.setText(String.valueOf(cal.get(Calendar.HOUR)).matches("\\d{1}")? 0+String.valueOf(cal.get(Calendar.HOUR)):String.valueOf(cal.get(Calendar.HOUR)));
}else if(data.equalsIgnoreCase("am_pm")){
t.setText(getAm_Pm(cal.get(Calendar.AM_PM)));
}else if(data.equalsIgnoreCase("day")){
t.setText(getDayofWeek(cal.get(Calendar.DAY_OF_WEEK)).matches("\\d{1}")?0+getDayofWeek(cal.get(Calendar.DAY_OF_WEEK)):getDayofWeek(cal.get(Calendar.DAY_OF_WEEK))+" "+getDayofTheMonth());
}else if(data.equalsIgnoreCase("month")){
t.setText( monthOption(cal.get(Calendar.MONTH)));
}
}
});
}
};
return tm;
}
Video DEMOprivate AnimationTimer setAnimation(Text t){
AnimationTimer tm=new AnimationTimer() {
@Override
public void handle(long now) {
Calendar cal=Calendar.getInstance();
Platform.runLater(new Runnable(){
@Override
public void run() {
String data=t.getUserData().toString();
if(data.equalsIgnoreCase("min")){
t.setText(String.valueOf(cal.get(Calendar.MINUTE)).matches("\\d{1}")? 0+String.valueOf(cal.get(Calendar.MINUTE)):Str ing.valueOf(cal.get(Calendar.MINUTE)));
}else if(data.equalsIgnoreCase("hour")){
t.setText(String.valueOf(cal.get(Calendar.HOUR)).matches("\\d{1}")? 0+String.valueOf(cal.get(Calendar.HOUR)):String.valueOf(cal.get(Calendar.HOUR)));
}else if(data.equalsIgnoreCase("am_pm")){
t.setText(getAm_Pm(cal.get(Calendar.AM_PM)));
}else if(data.equalsIgnoreCase("day")){
t.setText(getDayofWeek(cal.get(Calendar.DAY_OF_WEEK)).matches("\\d{1}")?0+getDayofWeek(cal.get(Calendar.DAY_OF_WEEK)):getDayofWeek(cal.get(Calendar.DAY_OF_WEEK))+" "+getDayofTheMonth());
}else if(data.equalsIgnoreCase("month")){
t.setText( monthOption(cal.get(Calendar.MONTH)));
}
}
});
}
};
return tm;
}
Comments
Post a Comment