Problem with BufferedImage & ImageObserver in Clojure / Swing / Java

I have been trying for many hours to get my drawing method working by creating a BufferedImage stored in Clojure refand then drawing it on a component (in this case a JPanel) to display it. Unfortunately, this does not work at all.

My code is this (reduced, but showing the relevant parts:

(defn create-graph
  "Data-ref is [xs ys], buffered-image-ref holds the basic graph."
  [data-ref buffered-image-ref & {:keys [width height image]}]
  (proxy [JPanel]
      []
    (getPreferredSize [] (Dimension. width  height))
    (paintComponent [g]
                    (proxy-super paintComponent g)
                    (if-not @buffered-image-ref
                      (dosync
                       (ref-set buffered-image-ref
                                (xy-plot2
                                 (first @data-ref)
                                 (second @data-ref)
                                 (.getGraphics
                                  (BufferedImage. width height 
                                                  BufferedImage/TYPE_INT_ARGB))
                                 :axis? true
                                 :width width
                                 :height height))))
                    (.drawImage g @buffered-image-ref
                                0 0 
                                (proxy [ImageObserver]
                                         []
                                       (imageUpdate []
                                          (proxy-super imageUpdate)))))))

And, below, xy-plot2 (which does not seem to be a problem, but I will include it for completeness:

(defn xy-plot2
  "Draws an xy-plot in the given Graphics context.
   'xs must be equal in length to 'ys."
  [xs ys gfx
   & {:keys [color max-y axis? y-buffer width height]
     :or {color Color/RED y-buffer 0}}]
  (let [h (/ height 2) ;; since we have -h to h (not 0 to h)
        w width ;; since we graph 0 to w
        len (count xs)
        min-x (apply min xs)
        xs (if-not (zero? min-x)
             (map #(- % min-x) xs)
             xs)
        max-y (or max-y (apply max ys))
        max-x (apply max xs)]
    (.setColor gfx color)
    (dorun ;; this is the key part, along with scale-xs and draw-l
     (take len
           (iterate (partial d-line gfx h)
                    [(scale-xs xs w 0)
                     (scale-xs ys h y-buffer)])))
    (and axis? (or (.setColor gfx Color/BLACK) (.drawLine gfx 0 h w h)))
    gfx))

When I run this, I get this error, which leads to the fact that I messed up in the last part of the method paintComponent().

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException:
No matching method found: drawImage for class sun.java2d.SunGraphics2D

I tried replacing nilwith ImageObserver, but to no avail. I tried other arg orders (for other methods drawImagefor other class types Graphics). All to no avail.

, , . , !

, ,

+3
1

, buffered-image-ref BufferedImage, .

+3

Source: https://habr.com/ru/post/1757761/


All Articles