java时间钟设计_java基础:10.5 Java FX之设计时钟

ClockPane.java

package DesignClock;

import javafx.scene.layout.Pane;

import javafx.scene.paint.Color;

import javafx.scene.shape.Circle;

import javafx.scene.shape.Line;

import javafx.scene.text.Text;

import java.util.Calendar;

import java.util.GregorianCalendar;

public class ClockPane extends Pane{

private int hour;

private int minute;

private int second;

//clock pane's width and height

private double w=250,h=250;

public ClockPane() {

setCurrentTime();

}

public ClockPane(int hour,int minute,int second) {

this.hour=hour;

this.minute=minute;

this.second=second;

paintClock();

}

public int getHour() {

return hour;

}

public void setHour(int hour) {

this.hour=hour;

paintClock();

}

public int getMinute() {

return minute;

}

public void setMinute(int minute) {

this.minute=minute;

paintClock();

}

public int getSecond() {

return second;

}

public void setSecond(int second) {

this.second=second;

paintClock();

}

public double getW() {

return w;

}

public void setW(double w) {

this.w=w;

paintClock();

}

public double getH() {

return h;

}

public void setH(double h) {

this.h=h;

paintClock();

}

// set the current time for the clock

public void setCurrentTime() {

Construct a calendar for the current date and time

Calendar calendar=new GregorianCalendar();

this.hour=calendar.get(Calendar.HOUR_OF_DAY);

this.minute=calendar.get(Calendar.MINUTE);

this.second=calendar.get(Calendar.SECOND);

paintClock();

}

protected void paintClock() {

double clockRadius=Math.min(w,h)*0.8*0.5;

double centerX=w/2;

double centerY=h/2;

//draw circle

Circle circle=new Circle(centerX,centerY,clockRadius);

circle.setFill(Color.WHITE);

circle.setStroke(Color.BLACK);

// show the number 3 6 9 12

Text t1=new Text(centerX-5,centerY-clockRadius+12,"12");

Text t2=new Text(centerX-clockRadius+3,centerY+5,"9");

Text t3=new Text(centerX+clockRadius-10,centerY+3,"3");

Text t4=new Text(centerX-3,centerY+clockRadius-3,"6");

// endX = centerX + handLength * sin(x)

// endY = centerY - handLength * cos(x)

// second's degree : second x (2pi/60)

double sLength=clockRadius*0.8;

double scondX=centerX+sLength*Math.sin(second*(2*Math.PI/60));

double scondY=centerY-sLength*Math.cos(second*(2*Math.PI/60));

Line sline=new Line(centerX,centerY,scondX,scondY);

sline.setStroke(Color.RED);

// minute's degree : (minute + second/60) x (2pi/60)

double mLength=clockRadius*0.65;

double minuteX=centerX+mLength*Math.sin(minute*(2*Math.PI/60));

double minuteY=centerY-mLength*Math.cos(minute*(2*Math.PI)/60);

Line mline=new Line(centerX,centerY,minuteX,minuteY);

mline.setStroke(Color.BLUE);

// hour's degree : (hour + minute/60 + second/(60 x 60)) x (2pi/12)

double hLength=clockRadius*0.5;

double hourX=centerX+hLength*Math.sin((hour%12+minute/60.0)*(2*Math.PI/12));

double hourY=centerY-hLength*Math.cos((hour%12+minute/60)*(2*Math.PI/12));

Line hline=new Line(centerX,centerY,hourX,hourY);

hline.setStroke(Color.GREEN);

getChildren().clear();

getChildren().addAll(circle,t1,t2,t3,t4,sline,mline,hline);

}

}

DisplayClock.java

package DesignClock;

import javafx.application.Application;

import javafx.animation.Timeline;

import javafx.application.Application;

import javafx.geometry.Pos;

import javafx.scene.Scene;

import javafx.scene.layout.BorderPane;

import javafx.stage.Stage;

import javafx.util.Duration;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.animation.KeyFrame;

import javafx.scene.control.Label;

public class DisplayClock extends Application {

@Override

public void start(Stage primaryStage) {

ClockPane clock=new ClockPane();

BorderPane borderPane=new BorderPane();

EventHandler eventHandler=e -> {

clock.setCurrentTime();

String timeString=clock.getHour()+":"+clock.getMinute()+":"+clock.getSecond();

Label lblCurrentTime=new Label(timeString);

borderPane.setCenter(clock);

borderPane.setBottom(lblCurrentTime);

BorderPane.setAlignment(lblCurrentTime, Pos.TOP_CENTER);

};

Timeline animation=new Timeline(new KeyFrame(Duration.millis(1000),eventHandler));

animation.setCycleCount(Timeline.INDEFINITE);

animation.play();

Scene scene=new Scene(borderPane,250,250);

primaryStage.setTitle("ClockAnimation");

primaryStage.setScene(scene);

primaryStage.show();

borderPane.widthProperty().addListener(ov ->

clock.setW(borderPane.getWidth())

);

borderPane.heightProperty().addListener(ov ->

clock.setH(borderPane.getHeight())

);

}

public static void main(String[] args) {

Application.launch(args);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值