ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Clojure / Swing / Java中的BufferedImage和ImageObserver出现问题

2019-12-09 23:05:04  阅读:250  来源: 互联网

标签:clojure swing java user-interface


我一直在尝试数小时,以便通过绘制存储在Clojure引用中的BufferedImage来使我的绘制方法起作用,然后将其绘制到组件(在本例中为JPanel)上以显示它.不幸的是,这根本无法正常工作.

我的代码是这样(已缩减,但显示了相关部分:

(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命令(用于其他类型的Graphics类的其他drawImage方法).一切都无济于事.

抱歉,如果我听起来有点难以理解,则此错误一直困扰着我.如有需要,我会在早上编辑!

非常感谢你
以撒

解决方法:

看起来buffered-image-ref设置为BufferedImage的图形,而不是图像本身.

标签:clojure,swing,java,user-interface
来源: https://codeday.me/bug/20191209/2097461.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有