Fork me on GitHub
#cljfx
<
2022-02-03
>
hairfire18:02:47

Does anyone know of a cljfx example that renders an image from a resources directory?

phronmophobic18:02:16

if you use ( fname) to get a url, you can pass that to something like https://github.com/phronmophobic/membrane/blob/master/src/membrane/cljfx.clj#L54 to create an javafx.scene.image.Image. You can then draw the image like https://github.com/phronmophobic/membrane/blob/master/src/membrane/cljfx.clj#L571

👍 1
hairfire20:02:48

Here: https://clojurians.slack.com/archives/CGHHJNENB/p1589214377102900 @vlaaad mentions the :image prop of :fx/type :image-view can load resources from the class path. When I set the :url prop value to (http://clojure.java.io/resource "logo.png") I get this exception java.lang.ClassCastException: class java.net.URL cannot be cast to class java.lang.String (java.net.URL and java.lang.String are in module java.base of loader 'bootstrap').

hairfire21:02:33

This seems to work:

hairfire21:02:44

(defn resource-image [{:keys [resource-file]}]
  {:fx/type :image-view
   :image   (str ( resource-file))})

👍 1
phronmophobic21:02:25

If you're deploying as a jar, it might be worth double checking if it still works when you jar your application. There are sometimes differences in behavior between file based resources and resources included in the jar.

✔️ 1