本文实例为大家分享了javafx实现简易时钟效果的具体代码,供大家参考,具体内容如下
效果图
用当前时间创建时钟,绘制表盘。
钟表是静止的。让指针动起来,请参照:
主函数文件 showclock:
package primier;
import javafx.application.application;
import javafx.geometry.insets;
import javafx.geometry.pos;
import javafx.scene.scene;
import javafx.stage.stage;
import javafx.scene.paint.color;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.scene.image.image;
import javafx.scene.image.imageview;
import javafx.scene.shape.line;
public class showclock extends application {
@override //override the start method in the application class
public void start(stage primarystage) {
// 创建时钟面板
clockpane clock = new clockpane();
// 当前时间整理为字符串
string timestring = clock.gethour() + ":" + clock.getminute()
+ ":" + clock.getsecond();
label lbcurrenttime = new label(timestring);
borderpane pane = new borderpane();
pane.setcenter(clock);
pane.setbottom(lbcurrenttime);
// 将时钟字符串设为靠上居中
borderpane.setalignment(lbcurrenttime, pos.top_center);
scene scene = new scene(pane, 250,250);
primarystage.settitle("display clock");
primarystage.setscene(scene);
primarystage.show();
}
public static void main (string[] args) {
application.launch(args);
}
}
clockpane 类
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;
// 时钟面板的宽度和高度
private double w = 250, h = 250;
/** 用当前时间创建时钟 */
public clockpane() {
setcurrenttime();
}
/** return hour */
public int gethour() { return hour; }
/** return minute */
public int getminute() { return minute; }
/** return second */
public int getsecond() { return second; }
/** set the current time for the clock */
public void setcurrenttime() {
// 用当前时间创建calendar类
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.4; // 时钟半径
// 时钟中心x, y坐标
double centerx = w/2;
double centery = h/2;
// 绘制钟表
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");
// 秒针
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);
// 分针
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);
// 时针
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);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
