It is probably happening because a bare bones JPanel is a white canvas every time swing paints it, it paints a white canvas than your routines paint over that giving you the flashing behavior, right way to paint on a JPanel is to extend it using proxy and overwrite paintComponent method, this is the method Java calls when it wants to paint a component. However it won't call it all the time, it will be called only when swing thinks component needs a repaint (i.e when you resize the frame). In order to create animations on the panel you have two choices, you can use a swing timer and call repaint on the panel say ever 100 ms or if your state is held in a ref you can add a watch to the ref that will call repaint whenever ref is updated.
You can copy/paste the last snippet from, http://nakkaya.com/2010/07/21/physics-with-clojure/ which uses the former method for painting on a JPanel. Hope this helps... -- Nurullah Akkaya http://nakkaya.com On Sat, Jul 24, 2010 at 4:11 PM, Mate Toth <[email protected]> wrote: > Hi, > > my problem is that during execution my presentation java applet is > flashing. There are many components which paint to a JPanel using it's > Graphics (.getGraphics). I think maybe the problem is that I don't > have any "paint everything now" routine..(I don't know which Java > routine to use, how I have to synchronize it etc..) Thanks for your > time and help. > > > I've the following code for the gui part (fe stands for "flocking- > enviroment", I'm making a flocking library): > > The main loop: > > (defn main [starting-fe] > (let [first-fe starting-fe > panel (new JPanel) > frame (new JFrame)] > > ;; configuring panel > (. panel setPreferredSize (new Dimension width height)) > > ;; configuring frame > (. frame add panel) > (. frame pack) > (. frame setVisible true) > > (loop [fe first-fe] > (draw-fe fe panel) > (Thread/sleep 100) > (recur ((:update-f fe) fe))))) > > > Blanking the screen: > > (defn blank-panel [panel] > (let [g (. panel getGraphics)] > (. g setColor (. Color BLACK)) > (. g fillRect 0 0 width height)))) > > At the end every boid drawed by this(called by the draw-fe function): > > (defn draw-boid [boid panel] > (let [[x0 x1] (:pos boid)] > (let [g (. panel getGraphics)] > (. g setColor (:color boid)) > (. g fillRect (- x0 2) (- x1 2) 5 5))))) > > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to [email protected] > Note that posts from new members are moderated - please be patient with your > first post. > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to [email protected] Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/clojure?hl=en
