• 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<ActionEvent> 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);
        }
    }
    
    
    
  • 相关阅读:
    laravel 服务提供者
    乐观锁和悲观锁
    MySQL索引原理及慢查询优化
    Laravel Session保存机制和terminate中间件
    laravel session踩坑
    理解 JavaScript 的 async/await(转)
    知识点
    js异步
    Office使用笔记
    YUM常用命令
  • 原文地址:https://www.cnblogs.com/l20902/p/10610913.html
Copyright © 2020-2023  润新知