Skip to content

Instantly share code, notes, and snippets.

@zcaudate
Last active April 10, 2019 19:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zcaudate/38a4e3ad9a68d8fcf6acb6662422f7c7 to your computer and use it in GitHub Desktop.
Save zcaudate/38a4e3ad9a68d8fcf6acb6662422f7c7 to your computer and use it in GitHub Desktop.
cljplot with javafx
(ns cljplot.sketches.fx
(:require [cljplot.render :as r]
[cljplot.build :as b]
[cljplot.common :refer :all]
[fastmath.interpolation :as in]
[fastmath.stats :as stats]
[clojure2d.color :as c]
[cljplot.scale :as s]
[fastmath.core :as m]
[fastmath.random :as rnd]
[cljplot.core :refer :all]
[java-time :as dt]
[clojure.string :as str]
[clojure2d.core :as c2d]
[clojure2d.pixels :as p]
[fastmath.vector :as v])
(:import (org.jfree.fx FXGraphics2D)
(javafx.scene.canvas Canvas)
(javafx.scene.layout Pane)
(javafx.scene Scene Node)
(javafx.stage Stage)
(javafx.application Platform)))
(comment
;; initialize fx
(Platform/startup (reify Runnable (run [this])))
(Platform/setImplicitExit false)
;; plot logo
(def -logo- (p/to-pixels (c2d/with-oriented-canvas-> :bottom-left+ (c2d/canvas 200 100)
(c2d/set-font "Raleway Thin")
(c2d/set-background :black)
(c2d/set-color :white)
(c2d/set-font-attributes 60)
(c2d/text "cljplot" 20 70))))
;; cljplot canvas
(def -canvas-
(let [gradient (c/gradient-presets :cubehelix)
side-conf {:color (nth (iterate c/brighten (gradient 0.1)) 3) :area? true :margins {:x [0.02 0.02]}}
pairs (->> (repeatedly #(v/vec2 (rnd/irand 200) (rnd/irand 100)))
(filter #(pos? (c/luma (apply p/get-color -logo- %))))
(map #(v/add % (v/generate-vec2 rnd/grand)))
(take 3000))]
(-> (b/series [:grid]
[:scatter pairs {:size (fn [_ _] (* 10 (m/pow (rnd/drand 0.1 1.0) 3)))
:color (fn [[x _] conf]
(let [[mn mx] (get-in conf [:extent :x 1])]
(c/set-alpha (gradient (m/norm x mn mx)) 50)))}])
(b/preprocess-series)
(b/add-side :top 25 (b/series [:density (map first pairs) side-conf]))
(b/add-side :right 25 (b/series [:density (map second pairs) side-conf]))
(b/add-axes :bottom)
(b/add-axes :left)
(r/render-lattice {:width 600 :height 300}))))
;; initialize fx canvas
(def -fx- (doto (Canvas.) (.setWidth 600) (.setHeight 300)))
;; draw BufferedImage to fx canvas
(def -graphics-
(doto (FXGraphics2D. (.getGraphicsContext2D -fx-))
(.drawImage (:buffer -canvas-) 0 0 nil)))
;; display
(def -scene- (Scene. (Pane. (into-array Node [-fx-]))))
(def -stage- (Platform/runLater (reify Runnable
(run [this]
(doto (Stage.)
(.setScene -scene-)
(.show)
(.toFront)))))))
;; Add to dependencies
[org.openjfx/javafx-controls "11.0.1"]
[org.openjfx/javafx-swing "11.0.1"]
[org.openjfx/javafx-base "11.0.1"]
[org.openjfx/javafx-graphics "11.0.1"]
[org.openjfx/javafx-web "11.0.1"]
[org.openjfx/javafx-media "11.0.1"]
[org.openjfx/javafx-fxml "11.0.1"]
[org.jfree/fxgraphics2d "1.8"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment