Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sample;
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.image.Image;
- import javafx.scene.image.ImageView;
- import javafx.scene.layout.HBox;
- import javafx.scene.layout.VBox;
- import javafx.stage.Stage;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.time.LocalTime;
- import java.time.format.DateTimeFormatter;
- public class Main extends Application {
- Image[] images;
- {
- try {
- images = new Image[]{ new Image(new FileInputStream("D:\\0.png")), new Image(new FileInputStream("D:\\1.png")) };
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- ImageView[] hourFirst = new ImageView[2];
- ImageView[] hourSecond = new ImageView[4];
- ImageView[] minuteFirst = new ImageView[4];
- ImageView[] minuteSecond = new ImageView[4];
- ImageView[] secondFirst = new ImageView[4];
- ImageView[] secondSecond = new ImageView[4];
- @Override
- public void start(Stage primaryStage) throws Exception {
- HBox hBox = new HBox();
- hBox.getChildren().add(getHoursHBox());
- hBox.getChildren().add(getMinutesHBox());
- hBox.getChildren().add(getSecondsHBox());
- primaryStage.setScene(new Scene(hBox));
- primaryStage.show();
- boolean f = true;
- while(f) {
- Thread.sleep(1000);
- String[] time = LocalTime.now().format(DateTimeFormatter.ofPattern("hh:mm:ss")).split(":");
- int[] hours = getHours(Integer.valueOf(time[0]));
- for (int i = 0; i < hours.length ; i++) {
- if(i <= 1) {
- if(hours[i] == 0) { hourFirst[i].setImage(images[0]); } else { hourFirst[i].setImage(images[1]); }
- } else {
- if(hours[i] == 0) { hourSecond[i-2].setImage(images[0]); } else { hourSecond[i-2].setImage(images[1]); }
- }
- }
- int[] minutes = getMinutesAndSeconds(Integer.valueOf(time[1]));
- for (int i = 0; i < minutes.length ; i++) {
- if(i <= 3) {
- if(minutes[i] == 0) { minuteFirst[i].setImage(images[0]); } else { minuteFirst[i].setImage(images[1]); }
- } else {
- if(minutes[i] == 0) { minuteSecond[i-4].setImage(images[0]); } else { minuteSecond[i-4].setImage(images[1]); }
- }
- }
- int[] seconds = getMinutesAndSeconds(Integer.valueOf(time[2]));
- for (int i = 0; i < seconds.length ; i++) {
- if(i <= 3) {
- if(seconds[i] == 0) { secondFirst[i].setImage(images[0]); } else { secondFirst[i].setImage(images[1]); }
- } else {
- if(seconds[i] == 0) { secondSecond[i-4].setImage(images[0]); } else { secondSecond[i-4].setImage(images[1]); }
- }
- }
- Thread.sleep(1000);
- }
- }
- private HBox getHoursHBox() {
- VBox vBoxFirstDigit = new VBox();
- for(int i = 0 ; i < hourFirst.length ; i++) {
- hourFirst[i] = new ImageView();
- hourFirst[i].setImage(images[0]);
- vBoxFirstDigit.getChildren().add(hourFirst[i]);
- }
- VBox vBoxSecondDigit = new VBox();
- for(int i = 0 ; i < hourSecond.length ; i++) {
- hourSecond[i] = new ImageView();
- hourSecond[i].setImage(images[0]);
- vBoxSecondDigit.getChildren().add(hourSecond[i]);
- }
- HBox hBox = new HBox();
- hBox.getChildren().add(vBoxFirstDigit);
- hBox.getChildren().add(vBoxSecondDigit);
- return hBox;
- }
- private HBox getMinutesHBox() {
- VBox vBoxFirstDigit = new VBox();
- for(int i = 0 ; i < minuteFirst.length ; i++) {
- minuteFirst[i] = new ImageView();
- minuteFirst[i].setImage(images[0]);
- vBoxFirstDigit.getChildren().add(minuteFirst[i]);
- }
- VBox vBoxSecondDigit = new VBox();
- for(int i = 0 ; i < minuteSecond.length ; i++) {
- minuteSecond[i] = new ImageView();
- minuteSecond[i].setImage(images[0]);
- vBoxSecondDigit.getChildren().add(minuteSecond[i]);
- }
- HBox hBox = new HBox();
- hBox.getChildren().add(vBoxFirstDigit);
- hBox.getChildren().add(vBoxSecondDigit);
- return hBox;
- }
- private HBox getSecondsHBox() {
- VBox vBoxFirstDigit = new VBox();
- for(int i = 0 ; i < secondFirst.length ; i++) {
- secondFirst[i] = new ImageView();
- secondFirst[i].setImage(images[0]);
- vBoxFirstDigit.getChildren().add(secondFirst[i]);
- }
- VBox vBoxSecondDigit = new VBox();
- for(int i = 0 ; i < secondSecond.length ; i++) {
- secondSecond[i] = new ImageView();
- secondSecond[i].setImage(images[0]);
- vBoxSecondDigit.getChildren().add(secondSecond[i]);
- }
- HBox hBox = new HBox();
- hBox.getChildren().add(vBoxFirstDigit);
- hBox.getChildren().add(vBoxSecondDigit);
- return hBox;
- }
- private int[] getHours(int hours) {
- int[] powerArray = {8, 4, 2, 1}, binaryHours = new int[6];
- if(hours / 10 == 0) {
- binaryHours[0] = 0;
- binaryHours[1] = 0;
- for(int i = 0 ; i < powerArray.length ; i++) {
- if(powerArray[i] <= hours) {
- binaryHours[i+2] = 1;
- hours =- powerArray[i];
- } else {
- binaryHours[i+2] = 0;
- }
- }
- } else {
- int firstDigit = hours / 10;
- int lastDigit = hours % 10;
- switch(firstDigit) {
- case 1:
- binaryHours[0] = 0;
- binaryHours[1] = 1;
- break;
- case 2:
- binaryHours[0] = 1;
- binaryHours[1] = 0;
- break;
- }
- for(int i = 0 ; i < powerArray.length ; i++) {
- if(powerArray[i] <= lastDigit) {
- binaryHours[i+2] = 1;
- lastDigit =- powerArray[i];
- } else {
- binaryHours[i+2] = 0;
- }
- }
- }
- return binaryHours;
- }
- private int[] getMinutesAndSeconds(int minutesOrSeconds) {
- int[] powerArray = {8, 4, 2, 1}, binaryMinutesOrSeconds = new int[8];
- if(minutesOrSeconds / 10 == 0) {
- binaryMinutesOrSeconds[0] = 0;
- binaryMinutesOrSeconds[1] = 0;
- binaryMinutesOrSeconds[2] = 0;
- binaryMinutesOrSeconds[3] = 0;
- for(int i = 0 ; i < powerArray.length ; i++) {
- if(powerArray[i] <= minutesOrSeconds) {
- binaryMinutesOrSeconds[i+4] = 1;
- minutesOrSeconds =- powerArray[i];
- } else {
- binaryMinutesOrSeconds[i+4] = 0;
- }
- }
- } else {
- int firstDigit = minutesOrSeconds / 10;
- int lastDigit = minutesOrSeconds % 10;
- for(int i = 0 ; i < powerArray.length ; i++) {
- if(powerArray[i] <= firstDigit) {
- binaryMinutesOrSeconds[i] = 1;
- firstDigit =- powerArray[i];
- } else {
- binaryMinutesOrSeconds[i] = 0;
- }
- }
- for(int i = 0 ; i < powerArray.length ; i++) {
- if(powerArray[i] <= lastDigit) {
- binaryMinutesOrSeconds[i+4] = 1;
- lastDigit =- powerArray[i];
- } else {
- binaryMinutesOrSeconds[i+4] = 0;
- }
- }
- }
- return binaryMinutesOrSeconds;
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
Add Comment
Please, Sign In to add comment