javafx 시계 효과 구현
핵심 은 세 함수:
첫 번 째 는 Public void dials 입 니 다.디스크 를 그립 니 다.
 두 번 째 는 Public void scale 입 니 다.눈금 을 그립 니 다.여기 서 주의해 야 할 것 은 글꼴 회전 입 니 다.
 세 번 째 는 Public void point 입 니 다.초 분침 과 인쇄 시간 을 그립 니 다.주의해 야 할 것 은 진법 문제 입 니 다.
전체 소스 코드 는 다음 과 같 습 니 다.
package com.wu.demo;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Duration;
public class view extends Application{
 @Override
 public void start(Stage stage) throws Exception{
 AnchorPane root = new AnchorPane();
 Canvas canvas = new Canvas(800,650);
 root.getChildren().add(canvas);
 Scene scene = new Scene(root,800,650);
 stage.setScene(scene);
 stage.setResizable(false);
 stage.show();
 //       
 GraphicsContext gc = canvas.getGraphicsContext2D();
 //      
 Timeline timeLine = new Timeline();
 //          
 ObservableList<KeyFrame> keyFrames = timeLine.getKeyFrames();
 //      
 keyFrames.add(new KeyFrame(Duration.seconds(0.1),e->{
 //     
 gc.clearRect(0,0,800,650);
 //     
 dials(gc);
 //     
 scale(gc);
 //     
 point(gc);
 }));
 //             
 timeLine.setCycleCount(-1);
 //      
 timeLine.play();
 }
 /**
 *     
 * @param gc
 */
 public void dials(GraphicsContext gc) {
 //     
 gc.save();
 //                
 gc.translate(100,25);
 gc.setLineWidth(8);
 gc.setStroke(Color.GRAY);
 gc.strokeOval(0, 0, 600, 600);
 gc.restore();
 }
 /**
 *     
 * @param gc
 */
 
 public void scale(GraphicsContext gc) {
 //     
 gc.save();
 //             
 gc.translate(400,325);
 //          -90
 gc.rotate(-90);
 //       
 gc.setFont(Font.font(20));
 for(int i = 1 ; i < 61 ; i++) {
 //         6 
 gc.rotate(6);
 if(i % 5 == 0) {
 gc.save();
 //         (250,0)         
 gc.translate(250,0);
 //                    
 gc.rotate(90-i/5*30);
 gc.fillText(i/5+"",0,0);
 gc.restore();
 gc.fillRect(275,0,22,10);
 }
 else{
 gc.fillRect(285,0,12,5);
 }
 }
 //     
 gc.restore();
 }
 /**
 *     
 * @param gc
 */
 public void point(GraphicsContext gc) {
 LocalDateTime time = LocalDateTime.now();
 int seconds = time.getSecond();
 int minutes = time.getMinute();
 int hours = time.getHour();
 double[] pointX1 = new double[]{0,50,270,50};
 double[] pointY1 = new double[]{0,5,0,-5};
 double[] pointX2 = new double[]{0,30,210,30};
 double[] pointY2 = new double[]{0,10,0,-10};
 double[] pointX3 = new double[]{0,20,150,20};
 double[] pointY3 = new double[]{0,12,0,-12};
 gc.save();
 //        
 gc.translate(400, 325);
 //     
 {
 String timeText1 = time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
 gc.setFill(Paint.valueOf("#c0c0c0"));
 gc.setFont(Font.font(20));
 gc.fillText(timeText1,-40,-200);
 String timeText2 = time.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
 gc.setFill(Paint.valueOf("#c0c0c0"));
 gc.setFont(Font.font(115));
 gc.fillText(timeText2,-220,30);
 }
 //   
 {
 gc.save();
 gc.rotate(-90);
 gc.setFill(Color.RED);
 gc.rotate(seconds*6);
 //      
 gc.fillPolygon(pointX1,pointY1, 4);
 gc.restore();
 }
 //   
 {
 gc.save();
 gc.rotate(-90);
 gc.setFill(Color.BLUE);
 gc.rotate(minutes*6+0.1*seconds);
 //      
 gc.fillPolygon(pointX2,pointY2, 4);
 gc.restore();
 }
 //   
 {
 gc.save();
 gc.rotate(-90);
 gc.setFill(Color.BLACK);
 gc.rotate(hours*30+minutes*0.5+seconds*(0.5/60));
 //      
 gc.fillPolygon(pointX3,pointY3, 4);
 gc.restore();
 }
 //     
 gc.restore();
 
 }
 public static void main(String[] args) {
 launch(args);
 }
}효과 그림:
 
 이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
자바 Fx TextArea 커서 포 지 셔 닝 실현1. 자바 FX TextArea 가 코드 에서 커서 를 찾 으 려 면 TextArea 구성 요소 의 positionCaret 속성 만 설정 해 야 합 니 다. 이렇게 하면 커서 를 4 의 위치 로 찾 을 수 있 습 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.