欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JavaFX实现简易时钟效果(二)

程序员文章站 2022-06-15 12:01:59
本文实例为大家分享了javafx实现简易时钟效果的具体代码,供大家参考,具体内容如下在前一篇博客中,我们已经绘制了一个静止时钟。首先进行一个微调:让表盘根据窗口大小自动调整大小:在 showclock...

本文实例为大家分享了javafx实现简易时钟效果的具体代码,供大家参考,具体内容如下

在前一篇博客中,我们已经绘制了一个静止时钟。

首先进行一个微调:让表盘根据窗口大小自动调整大小:

在 showclock.start() 中,添加对面板长宽的监听。

pane.widthproperty().addlistener(ov -> clock.setw(pane.getwidth()));
pane.heightproperty().addlistener(ov -> clock.seth(pane.getheight()));

添加对时间和钟表大小的更改方法

在 clockpane 类中添加:

/** construct a clock with specified hour, minute, and second */
 public clockpane(int hour, int minute, int second) {
  this.hour = hour;
  this.minute = minute;
  this.second = second;
  paintclock();
 }

 /** set a new hour */
 public void sethour(int hour) {
  this.hour = hour;
  paintclock();
 }

 /** set a new minute */
 public void setminute(int minute) {
  this.minute = minute;
  paintclock();
 }

 /** set a new second */
 public void setsecond(int second) {
  this.second = second;
  paintclock();
 }

 /** return clock pane's width */
 public double getw() {
  return w;
 }

 /** set clock pane's width */
 public void setw(double w) {
  this.w = w;
  paintclock();
 }

 /** return clock pane's height */
 public double geth() {
  return h;
 }

 /** set clock pane's height */
 public void seth(double h) {
  this.h = h;
  paintclock();
 }

用 timeline 实现动态钟表

在 showclock 类中添加:

//设置事件处理对象
eventhandler<actionevent> eventhandler = e -> {
   clock.setcurrenttime();
  };
//每秒结束后触发eventhandler
timeline animation = new timeline(
    new keyframe(duration.millis(1000), eventhandler));
  animation.setcyclecount(timeline.indefinite); //无限循环
  animation.play(); //开始动画

就可以让时钟动起来了。

完整代码

showclock.java

package primier;

import javafx.application.application;
import javafx.geometry.pos;
import javafx.scene.scene;
import javafx.stage.stage;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.event.actionevent;
import javafx.event.eventhandler;
import javafx.util.duration;
import javafx.animation.keyframe;
import javafx.animation.timeline;

public class showclock extends application {
 @override //override the start method in the application class
 public void start(stage primarystage) {
  clockpane clock = new clockpane();

  //设置事件处理对象
  eventhandler<actionevent> eventhandler = e -> {
   clock.setcurrenttime();
  };
  //每秒结束后触发eventhandler
  timeline animation = new timeline(
    new keyframe(duration.millis(1000), eventhandler));
  animation.setcyclecount(timeline.indefinite); //无限循环
  animation.play(); //开始动画

  borderpane pane = new borderpane();
  pane.setcenter(clock);
  scene scene = new scene(pane, 250,250);
  primarystage.settitle("display clock");
  primarystage.setscene(scene);
  primarystage.show();

  pane.widthproperty().addlistener(ov ->
    clock.setw(pane.getwidth()));
  pane.heightproperty().addlistener(ov ->
    clock.seth(pane.getheight()));
 }

 public static void main (string[] args) { application.launch(args); }
}

clockpane.java

package primier;

import java.util.calendar;
import java.util.gregoriancalendar;
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;

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;

 /** construct a default clock with the current time*/
 public clockpane() {
  setcurrenttime();
 }

 /** construct a clock with specified hour, minute, and second */
 public clockpane(int hour, int minute, int second) {
  this.hour = hour;
  this.minute = minute;
  this.second = second;
  paintclock();
 }

 /** return hour */
 public int gethour() {
  return hour;
 }

 /** set a new hour */
 public void sethour(int hour) {
  this.hour = hour;
  paintclock();
 }

 /** return minute */
 public int getminute() {
  return minute;
 }

 /** set a new minute */
 public void setminute(int minute) {
  this.minute = minute;
  paintclock();
 }

 /** return second */
 public int getsecond() {
  return second;
 }

 /** set a new second */
 public void setsecond(int second) {
  this.second = second;
  paintclock();
 }

 /** return clock pane's width */
 public double getw() {
  return w;
 }

 /** set clock pane's width */
 public void setw(double w) {
  this.w = w;
  paintclock();
 }

 /** return clock pane's height */
 public double geth() {
  return h;
 }

 /** set clock pane's height */
 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();
  //set current hour, minute and second
  this.hour = calendar.get(calendar.hour_of_day);
  this.minute = calendar.get(calendar.minute);
  this.second = calendar.get(calendar.second);
  paintclock();
 }

 /** paint the clock */
 protected void paintclock() {
  // initialize clock parameters
  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);
  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");

  // draw second hand
  double slength = clockradius * 0.8;
  double secondx = centerx + slength * math.sin(second * (2 * math.pi / 60));
  double secondy = centery - slength * math.cos(second * (2 * math.pi / 60));
  line sline = new line(centerx, centery, secondx, secondy);
  sline.setstroke(color.gray);

  // draw minute hand
  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);

  // draw hour hand
  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.0) * (2 * math.pi / 12));
  line hline = new line(centerx, centery, hourx, houry);
  sline.setstroke(color.green);

  getchildren().clear();
  getchildren().addall(circle, t1, t2, t3, t4, sline, mline, hline);
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: JavaFX 时钟