2010-08-03 55 views
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一直困扰着我。如果需要,我会在早上编辑!

谢谢你这么,这么多, 艾萨克

回答

3

它看起来像缓冲图像-REF设置为的BufferedImage的图形,而不是图像本身。

+0

就是这样。好神。有没有药物可以帮助?此外,你想要提供饼干的人之一? :)非常感谢,显然我的代码审查技能不好。 – Isaac 2010-08-03 04:01:04