Search code examples
clojurejava-2d

Clojure2d: drawing on the canvas silently fails if it has been resized


The following code successfully displays a blank canvas with a small red square:

(ns test-rect
  (:require [clojure2d.core :as c2d])
  (:require '[clojure2d.extra.utils :refer [show-image]]))

(c2d/with-canvas->
  (c2d/canvas 100 100)
;  (c2d/resize 100 100)
  (c2d/set-color :red)
  (c2d/rect 50 50 10 10)
  show-image)

However if I uncomment the call to resize, the red square is not drawn any more.


Solution

  • resize creates a new canvas that has to be passed into its own with-canvas.

    For example:

    (let [canvas (c2d/canvas 100 100)]
      (c2d/with-canvas-> (c2d/resize canvas 100 100)
        (c2d/set-color :red)
        (c2d/rect 50 50 10 10)
        show-image))