0

I have made a desktop clock app in java which uses a specific font (Digital Dream Fat) in order to achieve the desired look but if someone doesn't have the font installed on their computer, it will default with Arial. How would I compensate for that?

Here is my code:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.collections.FXCollections;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Tooltip;
import javafx.stage.StageStyle;
import javafx.scene.paint.Color;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import java.text.DateFormat;
import java.util.Date;
import java.text.SimpleDateFormat;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.util.Duration;

    public class Clock extends Application {

        public Text text;

            @Override 
            public void start(Stage stage) {

                    DateFormat df = new SimpleDateFormat("EEE,MMM d yyyy - h:mm:ss a");
                    Date date = new Date();
                    String stringDate = df.format(date);

                    text = new Text(10, 60, stringDate);
                    text.setFont(Font.font ("Digital Dream Fat", 30f));
                    text.setFill(Color.RED);


            Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(0), new EventHandler<ActionEvent>() {

                    @Override
                    public void handle(ActionEvent actionEvent) {
                    Date update = new Date();
                    String stringNewDate = df.format(update);
                    text.setText(stringNewDate);
                    }
            }), new KeyFrame(Duration.seconds(1)));

            timeline.setCycleCount(Animation.INDEFINITE);
            timeline.play();

                    ChoiceBox colorChoice = new ChoiceBox(FXCollections.observableArrayList("Red", "Blue", "Green", "Grey", "Black", "White"));

                    colorChoice.setOnAction(new EventHandler<ActionEvent>(){
                        public void handle(ActionEvent e) {
                            if (colorChoice.getSelectionModel().getSelectedItem().toString().equals("Red")) {
                            colorChoice.setStyle("-fx-base: red");
                            text.setFill(Color.RED);
                            } else if (colorChoice.getSelectionModel().getSelectedItem().toString().equals("Blue")) {
                            colorChoice.setStyle("-fx-base: blue");
                            text.setFill(Color.BLUE);
                            } else if (colorChoice.getSelectionModel().getSelectedItem().toString().equals("Green")) {
                            colorChoice.setStyle("-fx-base: green");
                            text.setFill(Color.GREEN);
                            } else if (colorChoice.getSelectionModel().getSelectedItem().toString().equals("Grey")) {
                            colorChoice.setStyle("-fx-base: grey");
                            text.setFill(Color.GREY);
                            } else if (colorChoice.getSelectionModel().getSelectedItem().toString().equals("White")) {
                            colorChoice.setStyle("-fx-base: white");
                            text.setFill(Color.WHITE);
                            } else if (colorChoice.getSelectionModel().getSelectedItem().toString().equals("Black")) {
                            colorChoice.setStyle("-fx-base: black");
                            text.setFill(Color.BLACK);
                            } else {
                            colorChoice.setStyle("-fx-base: red");
                            text.setFill(Color.RED);
                            }
                        }
                    });

                    HBox hbox = new HBox(colorChoice);

                    colorChoice.setTooltip(new Tooltip("Select A Colour"));
                    colorChoice.getSelectionModel().selectFirst();


                    Scene scene = new Scene(new Group(text, hbox));
                    scene.setFill(Color.TRANSPARENT);

                    stage.setScene(scene);
                    stage.initStyle(StageStyle.TRANSPARENT);
                    stage.setX(0);
                    stage.setY(0);
                    stage.setWidth(710);
                    stage.setHeight(80);
                    stage.show(); 
            }

            public static void main(String[] args) {
                    launch(args);
            }
    }
Herseept72
  • 25
  • 1
  • 6
  • have you seen http://stackoverflow.com/questions/5652344/how-can-i-use-a-custom-font-in-java? – marktani Dec 18 '16 at 21:34
  • You can try and include the font file in with your source, e.g. in another source folder such as 'deps' – ack Dec 18 '16 at 21:34

0 Answers0