多线程 – 使用Clojure中的代理程序进行多线程弹跳球程序

前端之家收集整理的这篇文章主要介绍了多线程 – 使用Clojure中的代理程序进行多线程弹跳球程序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在clojure中编写了一个多线程弹跳球程序.启动动画线程后,我做 –
(send-balls)

开始弹跳球线程.球不移动,这显示在控制台上 –

(#<Agent@5675b3ee Failed: #<Ref@313d21d6: {:x 759,:y 629,:x-speed 3,:y-speed 1}>> #<Agent@22cf3539 Failed: #<Ref@247881db: {:x 794,:y 258,:x-speed 2,:y-speed 3}>> #<Agent@51af4309 Failed: #<Ref@445ead9d: {:x 831,:y 251,:x-speed 4,:y-speed 2}>>)

有人能指出这里发生了什么?

(import
 '(java.awt Color Graphics Dimension)
 '(java.awt.image BufferedImage)
 '(javax.swing JPanel JFrame))

(def width 1000)
(def height 1000)

(def number-of-balls 3)

(def rad 20)

(def origin-x 100)
(def origin-y 100)
(def Box-height 500)
(def Box-width 700)
(def max-x (+ origin-x Box-width (* 4 rad)))
(def max-y (+ origin-y Box-height (* 4 rad)))
(def min-x origin-x)
(def min-y origin-y)

(defn init-x
 []
 (+ (rand-int (- max-x min-x)) min-x))

(defn init-y
 []
  (+ (rand-int (- max-y min-y)) min-y))

(defstruct ball :x :y :x-speed :y-speed)

(def balls
 (apply vector (map (fn [_] (ref (struct ball (init-x) (init-y)
(rand-int 10) (rand-int 10))))
                 (range number-of-balls))))

(def ball-agents (apply vector (map agent balls)))

(defn get-ball
 [n]
 (balls n))


(defn set-new-x
 [ball]
 (let [x (@ball :x)
       x-speed (@ball :x-speed)
       new-x (+ x x-speed)]
   (dosync
     (if (and (>= new-x min-x) (<= new-x max-x))
      (ref-set ball (assoc @ball :x new-x))
            (ref-set ball (assoc @ball :x-speed (* -1 x-speed)))))
   (println "the new x is " @(ball :x)))
 @ball)

(defn set-new-y
 [ball]
 (let [y (@ball :y)
       y-speed (@ball :y-speed)
       new-y (+ y y-speed)]
   (dosync
     (if (and (>= new-y min-y) (<= new-y max-y))
             (ref-set ball (assoc @ball :y new-y))
             (ref-set ball (assoc @ball :y-speed (* -1 y-speed))))))
 @ball)

(defn paint-balls
 [bg x y]
 (doto bg
   (.setColor (. Color red))
   (.fillOval x y rad rad)))


(defn render
 [g]
 (let [img (new BufferedImage width height
                (. BufferedImage TYPE_INT_ARGB))
      bg (. img (getGraphics))]
  (doto bg
     (.setColor (. Color white))
     (.fillRect 0 0 (. img (getWidth)) (. img (getHeight)))
     (.setColor (. Color red))
     (.drawRect origin-x origin-y (+ origin-x Box-width) (+ origin-y Box-height)))
  (dorun
    (for [i (range number-of-balls)]
      (do
        (paint-balls bg (@(get-ball i) :x) (@(get-ball i) :y)))))
  (. g (drawImage img 0 0 nil))
  (. bg (dispose))))

(def panel (doto (proxy [JPanel] []
                       (paint [g] (render g)))
            (.setPreferredSize (new Dimension
                                    width
                                    height))))

(def frame (doto (new JFrame) (.add panel) .pack .show))

(def animator (agent nil))

(defn bounce
 [x]
 (while true
   (set-new-x @*agent*)
   (set-new-y @*agent*)
   (. Thread (sleep 100))
   (println "here in bounce " *agent*)))




(defn animation
 [x]
 (send-off *agent* animation)
 (. panel (repaint))
 (. Thread (sleep 100)))

(defn send-balls
 []
 (doall
   (for [i (range number-of-balls)]
     (do
       (send-off (ball-agents i) bounce)))))


(send-off animator animation)

解决方法

正如我所看到的主要问题 – 您向代理发送的功能不是与代理一起运行,而是与其值(引用)一起运行.通过消除set-new-x和set-new-y函数中的@,可以使它工作.
(ns balls)

(import
  '(java.awt Color Graphics Dimension)
  '(java.awt.image BufferedImage)
  '(javax.swing JPanel JFrame))

(def width 1000)
(def height 1000)

(def number-of-balls 3)

(def rad 20)

(def origin-x 100)
(def origin-y 100)
(def Box-height 500)
(def Box-width 700)
(def max-x (+ origin-x Box-width (* 4 rad)))
(def max-y (+ origin-y Box-height (* 4 rad)))
(def min-x origin-x)
(def min-y origin-y)

(defn init-x
 []
 (+ (rand-int (- max-x min-x)) min-x))

(defn init-y
 []
  (+ (rand-int (- max-y min-y)) min-y))

(defstruct ball :x :y :x-speed :y-speed)

(def balls
 (apply vector (map (fn [_] (ref (struct ball (init-x) (init-y)
(rand-int 10) (rand-int 10))))
                 (range number-of-balls))))

(def ball-agents (apply vector (map agent balls)))

(defn get-ball
 [n]
 (balls n))


(defn set-new-x
  [ball]
  (let [x (ball :x)
        x-speed (ball :x-speed)
        new-x (+ x x-speed)]
    (dosync
      (if (and (>= new-x min-x) (<= new-x max-x))
        (alter ball assoc :x new-x)
        (alter ball assoc :x-speed (* -1 x-speed)))))
    ball)

(defn set-new-y
  [ball]
  (let [y (ball :y)
        y-speed (ball :y-speed)
        new-y (+ y y-speed)]
    (dosync
      (if (and (>= new-y min-y) (<= new-y max-y))
        (alter ball assoc :y new-y)
        (alter ball assoc :y-speed (* -1 y-speed))))
   ball))

(defn paint-balls
 [bg x y]
 (doto bg
   (.setColor (. Color red))
   (.fillOval x y rad rad)))


(defn render
 [g]
 (let [img (new BufferedImage width height
                (. BufferedImage TYPE_INT_ARGB))
      bg (. img (getGraphics))]
  (doto bg
     (.setColor (. Color white))
     (.fillRect 0 0 (. img (getWidth)) (. img (getHeight)))
     (.setColor (. Color red))
     (.drawRect origin-x origin-y (+ origin-x Box-width) (+ origin-y Box-height)))
  (dorun
    (for [i (range number-of-balls)]
      (do
        (paint-balls bg (@(get-ball i) :x) (@(get-ball i) :y)))))
  (. g (drawImage img 0 0 nil))
  (. bg (dispose))))

(def panel (doto (proxy [JPanel] []
                       (paint [g] (render g)))
            (.setPreferredSize (new Dimension
                                    width
                                    height))))

(def frame (doto (new JFrame) (.add panel) .pack .show))

(def animator (agent nil))

(defn bounce
 [ball_cur]
 (do
   (Thread/sleep 100)
   (send-off *agent* bounce)
   (set-new-x (set-new-y ball_cur))))

(defn animation
 [x]
 (send-off *agent* animation)
 (. panel (repaint))
 (. Thread (sleep 100)))

(defn send-balls
 []
 (doall
   (for [i (range number-of-balls)]
     (do
       (send-off (ball-agents i) bounce)))))


(send-off animator animation)
(send-balls)

猜你在找的Java相关文章