大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这期内容当中小编将会给大家带来有关利用JavaFX制作一个简单的时钟,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
创新互联始终坚持【策划先行,效果至上】的经营理念,通过多达十载累计超上千家客户的网站建设总结了一套系统有效的全网营销解决方案,现已广泛运用于各行各业的客户,其中包括:成都混凝土搅拌站等企业,备受客户赞许。首先要在面板中显示一个时钟,我们可以设计一个ClockPane类来显示一个时钟。
最终效果:
若要绘制一个时钟,需要绘制一个圆并为秒钟、分钟和小时绘制三个指针。为了画一个指针,需要确定一条直线的两端:一端是时钟的中央,位于(centerX,centerY);另外一端位于(endX,endY),由一下公式来确定:
endX=centerX+handLength×sin(θ)
endY=centerY-handLength×cos(θ)
(其中θ是指针和竖直方向12点的夹角)
因为一分钟有60秒,所以第2个指针的角度是:second×(2π/60)
分钟的位置由分钟和秒钟来决定。包含秒数的确切分钟数是minu+second/60。例如,如时间是3分30秒,那么总的分钟数就是3.5。由于一小时有60分钟,因此分针的角度是: (minute+second/60)×(2π/12)
由于一个圆被分为12个小时,所以时针的角度是: (hour+minute/60+second/(60×60))×(2π/12)
为了简化计算,在计算分针和时针角度的时候,可以忽略秒针,因为它们数字太小,基本可以忽略。因此,秒针、分针以及时针的端点可以如下计算:
secondX = centerX + secondHandLength × sin(second × (2π/60))
secondY = centerY - secondHandLength × cos(second × (2π/60))
minuteX = centerX + minuteHandLength × sin(minute × (2π/60))
minuteY = centerY - minuteHandLength × cos(minute × (2π/60))
hourX = centerX + hourHandLength × sin((hour+minute/60) × (2π/12))
hourX = centerX + hourHandLength × sin((hour+minute/60) × (2π/12))
这样就得到了ClockPane类的实现程序:
package com.company; 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; 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(); } 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(); } protected 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); 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 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); 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)*(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); } }