利用javafx编写一个时钟制作程序
1.首先创建一个时钟类,用于编写时钟的各种特有属性
package javaclock;
/**
*
* @author admin
*/
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;
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 javafx.scene.control.Button;
public class ClockPane extends Pane{
private int hour;
private int minute;
private int second;
private double w = 250,h = 250;
//无参构造函数
public ClockPane() {
setCurrentTime();//调用方法
}
//有参构造函数
public ClockPane(int hour,int minute,int second) {
// Button btn = new Button("调整时间");
//btn.setCurrentTime1();
this.hour = hour;
this.minute = minute;
this.second = second;
paintClock();//调用方法
}
//set与get方法
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();
}
//修改当前时间
public void setCurrentTime1(int hour,int minute,int second){
this.hour = hour;
this.minute = minute;
this.second = second;
paintClock();
}
//方法setCurrentTime()设置获取当前时间
public void setCurrentTime() {
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();
}
//创建paintClock()显示时间
public void paintClock() {
double clockRadius = Math.min(w, h)*0.8*0.5;//时钟半径
double centerX = w/2;//圆心坐标
double centerY = h/2;
//创建时钟圆形
Circle circle = new Circle(centerX,centerY,clockRadius);//创建圆
circle.setFill(Color.WHITE);//圆形背景色为白色
circle.setStroke(Color.BLACK);//圆形边缘色为黑色
//创建四个文本对象用于将12,9,6,3填入圆形中,注意不要压在圆形上面,需要计算坐标
Text text1 = new Text(centerX-5,centerY-clockRadius+12,"12");
Text text2 = new Text(centerX-clockRadius+3,centerY+5,"9");
Text text3 = new Text(centerX+clockRadius-10,centerY+3,"3");
Text text4 = new Text(centerX-3,centerY+clockRadius-3,"6");
//分别绘制时针,分针,秒针
//秒针
double secLength = clockRadius*0.8;//秒针长度
double secondX = centerX + secLength*Math.sin(second*(2*Math.PI/60));//因为一分钟有60秒,故求出当前秒针的角度,利用三角函数公式即可计算出秒针的端点x坐标
double secondY = centerY - secLength*Math.cos(second*(2*Math.PI/60));//同理求出秒针的端点Y坐标
Line secline = new Line(centerX,centerY,secondX,secondY);//创建线段对象实现秒针
secline.setStroke(Color.RED);//将秒针定义为红色
//分针
double minLength = clockRadius*0.65;//分针长度
double minuteX = centerX + minLength*Math.sin((minute )*(2*Math.PI/60));//因为一小时有六十分钟,利用分针的时间加上秒针的时间,利用三角函数对应的角度,即可计算出分针的端点x坐标
double minuteY = centerY - minLength*Math.cos((minute )*(2*Math.PI/60));//同理求出分针的端点Y坐标
Line minline = new Line(centerX,centerY,minuteX,minuteY);//创建线段对象实现秒针
minline.setStroke(Color.GREEN);//将秒针定义为绿色
//时针
double houLength = clockRadius*0.5;//时针长度
double hourX = centerX + houLength*Math.sin((hour%12 + minute/60.0 )*(2*Math.PI/12));//因为一天有十二小时,一小时有六十分钟,利用小时的时间加上分针的时间再加上秒针的时间,利用三角函数对应的角度,即可计算出分针的端点x坐标
double hourY = centerY - houLength*Math.cos((hour%12 + minute/60.0 )*(2*Math.PI/12));//同理求出时针的端点Y坐标
Line houline = new Line(centerX,centerY,hourX,hourY);//创建线段对象实现时针
houline.setStroke(Color.BLUE);//将秒针定义为蓝色
getChildren().clear();//每调用执行一次paintClock()方法就会清空面板
getChildren().addAll(circle,text1,text2,text3,text4,secline,minline,houline);//将几个控件添加到面板中
}
}
2、然后编写一个测试类,用于通过多线程创建呈现时钟的动画效果
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaclock;
import java.util.*;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.layout.BorderPane;
import javafx.animation.Timeline;
import javafx.scene.control.Label;
import javafx.animation.KeyFrame;
import javafx.geometry.Pos;
import javafx.util.Duration;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
/**
*
* @author admin
*/
public class JavaClock extends Application {
//JavaClock jc = new JavaClock();
ClockPane clock = new ClockPane();//
BorderPane borderPane = new BorderPane();//
@Override
public void start(Stage primaryStage) throws Exception {
// TODO 自动生成的方法存根
//绑定事件源
EventHandler<ActionEvent> eventHandler = e ->{
clock.setCurrentTime();
String timeString = clock.getHour()+":"+clock.getMinute()+":"+clock.getSecond();
Label labelCurrentTime = new Label(timeString);
borderPane.setCenter(clock);
borderPane.setBottom(labelCurrentTime);
BorderPane.setAlignment(labelCurrentTime, Pos.TOP_CENTER);
};
Timeline animation = new Timeline(new KeyFrame(Duration.millis(1000),eventHandler));//设定时钟动画每1秒变一次,关键帧时间间隔
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
//Scene
Scene scene = new Scene(borderPane,250,250);
primaryStage.setTitle("JavaClock");
primaryStage.setScene(scene);
primaryStage.show();//展示场景
borderPane.widthProperty().addListener(o->
clock.setW(borderPane.getWidth()));//保持时间面板与场景同步
borderPane.heightProperty().addListener(o->
clock.setH(borderPane.getHeight()));
//设置一个按钮,用于调整时间
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}