3
我一直在嘗試幾個小時,通過在Clojure ref
中存儲的BufferedImage中繪圖來獲得我的繪圖方法,然後將其繪製到組件上這個案例是一個JPanel
)爲了顯示它。不幸的是,這並不能很好地發揮作用。Clojure/Swing/Java中的BufferedImage&ImageObserver問題
我的代碼是這樣的(相比下來,但顯示的相關部分:
(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)))))))
而且,下面,XY-plot2(這似乎並不成爲問題,但我會包括它完整性:
(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))
當我運行它,我得到這個錯誤,導致相信我已經在paintComponent()
方法的最後一部分搞砸
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException:
No matching method found: drawImage for class sun.java2d.SunGraphics2D
我試過用nil
代替ImageObserver
,但無濟於事。我已經嘗試了其他的arg命令(其他drawImage
方法用於其他類型的Graphics
類)。一切都無濟於事。
對不起,如果我聽起來有點難以理解,這個bug一直困擾着我。如果需要,我會在早上編輯!
謝謝你這麼,這麼多, 艾薩克
就是這樣。好神。有沒有藥物可以幫助?此外,你想要提供餅乾的人之一? :)非常感謝,顯然我的代碼審查技能不好。 – Isaac 2010-08-03 04:01:04