2

I am making an alarm clock kind of program and I need a way to make the clock face a specific font. I have tried multiple times in multiple ways. If that is not possible can you please provide another solution? Thank you in advance!

import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.print.DocFlavor.URL;

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Menu extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {

    //Frame stuff (works)
    GridPane grid = new GridPane();
    grid.setAlignment(Pos.CENTER);
    grid.setHgap(10);
    grid.setVgap(10);

    //Frame Size
    Scene scene = new Scene(new DigitalClock(),1080, 720);

    //Icon
    primaryStage.getIcons().add(new Image(getClass().getResourceAsStream(("/lib/logo.png"))));
    primaryStage.setTitle("Clock: 140 Edition");

    //Necessities
    primaryStage.setScene(scene);
    primaryStage.show();
}

public static void main(String args[]) {
    launch(args);
}

}

class Util {
    public static String pad(int fieldWidth, char padChar, String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = s.length(); i < fieldWidth; i++) {
          sb.append(padChar);
        }
        sb.append(s);

        return sb.toString();
        }
}

class DigitalClock extends Label {

    public DigitalClock() {
        bindToTime();
        }

    // the digital clock updates once a second.
    private void bindToTime() {
    Timeline timeline = new Timeline(
      new KeyFrame(Duration.seconds(0),
        new EventHandler<ActionEvent>() {
          @Override public void handle(ActionEvent actionEvent) {
            Calendar time = Calendar.getInstance();
            String hourString = Util.pad(2, ' ', time.get(Calendar.HOUR) == 0 ? "12" : time.get(Calendar.HOUR) + "");
            String minuteString = Util.pad(2, '0', time.get(Calendar.MINUTE) + "");
            String secondString = Util.pad(2, '0', time.get(Calendar.SECOND) + "");
            String ampmString = time.get(Calendar.AM_PM) == Calendar.AM ? "AM" : "PM";
            setText(hourString + ":" + minuteString + ":" + secondString + " " + ampmString);
          }
        }
      ),
      new KeyFrame(Duration.seconds(1))
    );
    timeline.setCycleCount(Animation.INDEFINITE);
    timeline.play();
    }

I also know some of the imports are not used, I would prefer to keep them. Thanks again!

1 Answers1

0

The Font has to do with the Label you are using in the example.

<-----------------Ways to do----------------->


1)Using external css:

/*The font path*/
@font-face{
    src: url("../fonts/Younger than me Bold.ttf");
}

/* An element which has this id*/
#LabelID{
 -fx-font-family:"Younger than me";
 -fx-font-size:18.0;
}

//or for all labels
.label{
  -fx-font-family:"Younger than me";
  -fx-font-size:18.0;
}

2)Using setStyle(...):

`label.setStyle("-fx-font-family:monospace; -fx-font-size:16px; -fx-text-fill:black; -fx-border-color:red;");`

3)Using setFont(...):

b.setFont(new Font("Arial", 24));

4)Tricky and not recommended ( not included in JavaFX docs) : here


Relative posts:

Community
  • 1
  • 1
GOXR3PLUS
  • 6,877
  • 9
  • 44
  • 93
  • In order to change the font I have to make a label, and there is no label in the clock section. The setText requires a String, which cannot be changed in font. I know this from the trial and error of the setFont & setStyle techniques. However, can I use CSS to change the font of an animation (timeline)? If so, how would I annotate the java program so I can connect it with css. By the way, my scene constructor already has a parent class, the clock. So if there was a way to bypass the need of having the clock as the parent class and set it to Group() for css. I do appreciate your answer GOXR3... – Jake Armstrong Dec 18 '16 at 04:39
  • @Jake ArmStrong `class DigitalClock extends Label` so there is a `Label` , try inside the Constructor of class DigitalClock to add this line `setStyle("-fx-background-color:black; -fx-text-fill:white; -fx-font-weight:bold; -fx-border-color:white;");` everything other on how to use external css on your application is in the answer , i modified the links at the bottom. – GOXR3PLUS Dec 18 '16 at 06:03