2

I have an application where I have a JPanel with some rectangles painted on it, based on informations retrived from a list (ref ()), later I change the list and need to repaint the JPanel.

I don't think I'm doing it right, it's getting slow. I which I could draw each rectangle in an autonomous way, I mean, if the rectangle 'wants' to change it's color, it changes and that rectangle be automatically changed in the panel.

Can anybody help me? Here are some important snippets of codes I'm using.

(defn fill [g x y c]
  (doto g
    (.setColor c)
    (.fillRect 
      (+ *margin* (* y 15)) 
      (+ *margin* *margin-top* (* x 15)) 
              15 15)))

(defn draw [g x y c]
  (doto g
    (.setColor c)
    (.drawRect 
      (+ *margin* (* y 15)) 
      (+ *margin* *margin-top* (* x 15)) 
    15 15)))

(defn make-panel 
      ([]
        (proxy [JPanel] []
          (paintComponent 
            [g]
            (doseq [i (range *size*)
                    j (range *size*)]
              (let [ v (:color @(get-obj i j))]
                (cond 
                  (= v :blue) (fill g i j Color/BLUE)
                  (= v :red)  (fill g i j Color/RED)
                  :else       (draw g i j Color/LIGHT_GRAY))))))))

;; This is how I repaint the frame, after changing the list
(doto *frame*
      (.setContentPane (make-panel))
      .repaint
      (.setVisible true))

1 Answer 1

3

Firstly, you don't need to replace your panel (the (.setContentPane (make-panel)) part), just repaint the old panel.

To do the minimal amount of work only when something actually changes you could use the watch facility. If information on all your rectangles is held in a single Ref, you might do something like this:

(add-watch the-ref :update-rects
  (fn [_ _ old new]
    (update-rects-as-appropriate old new)))

Here update-rects-as-appropriate stands for code which calculates rectangle colours based on the old and new data and performs the updates when -- and only when -- results differ. Depending on the precise form of the data held in the Ref, it might become obvious early in the process that there will be no difference, so the whole calculation doesn't need to be carried out.

If each rectangle has a Ref to itself, you can just update based on the new state:

(add-watch the-ref :update-rect
  (fn [_ _ _ new]
    (update-rect the-rect new)))

Here the-rect is the rectangle corresponding to the-ref and update-rect a function which knows how to update it based on the state of the-ref.

4
  • Really Nice! I didn't know this watch facility. It will help a lot, I have just one question: in this update-rects functions I will change the state (color) of the rectangle but I will have another side effect that is the repainting of JPanel, do you know how to change only the rectangle I want in the panel and not have to repaint the whole thing? Jan 31, 2012 at 13:09
  • @jonathancardoso: You'll have to implement paintComponent appropriately and devise a way for the watches to register pending updates for the rectangles. The watches will need to (.repaint panel) in any case; one possible approach for them would be to add [[i j] color] entries (with [i j] corresponding to their rectangle) to a map held in an Atom / a Ref, then paintComponent could iterate over this map, e.g. (doseq [[[i j] color] updates-atom] (cond (= color :blue) (fill g i j Color/BLUE) ...)). Jan 31, 2012 at 13:27
  • Or maybe you could not have the watches call .repaint, but do it from a separate thread, say, 60 times per second... Well, to be honest, I'm not doing much GUI programming at all. Perhaps someone who knows more about drawing in Swing will have a better solution... I'm willing to transcribe it into Clojure if they happen to be a Java person and are ok with me doing that. :-) Jan 31, 2012 at 13:30
  • You helped me a lot, that works and is faster. The thing with Swing GUI I fixed by repainting just the panel and calling (SwingUtilities/updateComponentTreeUI *frame*) Jan 31, 2012 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.