Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Update:</strong> As was pointed out, the original answer did not directly answer the OP's question. I've created a <a href="http://clarkonium.net/2013/08/loading-fonts-like-css/" rel="nofollow">blog post</a> about the original technique in case anyone is interested.</p> <p>Here is a short program that does what you want.</p> <pre><code>(ns demo.core (:gen-class :extends javafx.application.Application) (:import [javafx.application Application] [javafx.event EventHandler] [javafx.scene Scene] [javafx.scene.control Button] [javafx.scene.layout StackPane] [javafx.scene.text Font]) (:require [clojure.java.io :as jio])) (defn- get-font-from-resource "Load the named font from a resource." [font-name] (let [prefix "demo/resources/" url (jio/resource (str prefix font-name)) fnt (Font/loadFont (.toExternalForm url) 20.0)] fnt)) (defn -start "Build the application interface and start it up." [this stage] (let [root (StackPane.) scene (Scene. root 600 400) fnt (get-font-from-resource "ITCBLKAD.TTF") btn (Button. "Press Me!")] (.setOnAction btn (reify EventHandler (handle [this event] (doto btn (.setText (str "This is " (.getName fnt))) (.setFont fnt))))) (.add (.getChildren root) btn) (doto stage (.setTitle "Font Loading Demo") (.setScene scene) (.show)))) (defn -main [&amp; args] (Application/launch demo.core args)) </code></pre> <p>In this project, I placed the font file in <code>resources</code>, a sub-directory of <code>demo</code> - where the Clojure source is stored - hence the "prefix" in the function <code>get-font-from-resource</code>.</p> <p>It looks like the problem you might have been having with <code>loadFont</code> was in your conversion from the <code>URL</code> to the <code>String</code> form. The external form is an absolute path starting at the root directory on the drive.</p> <p><strong>Tip</strong>: You probably know this, but one thing that continually screws me up are methods in JavaFX that require <code>double</code> parameters, like <code>Font/loadFont</code>. I'm used to Java just promoting integer arguments to double. In Clojure, if you use an integer where a double is required, the program fails with a less than useful error message.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload