2011/1/21 WoodHacker <ramsa...@comcast.net> > I'm converting the java code examples in Killer Game Programming in > Java by Andrew Davison to Clojure and am having great fun doing it. > But I've hit wall where I can't seem to get the code to work. > > The following code moves an animated gif strip to a java array: > > public BufferedImage[] loadStripImageArray(String fnm, int number) > { > if (number <= 0) { > System.out.println("number <= 0; returning null"); > return null; > } > > BufferedImage stripIm; > if ((stripIm = loadImage(fnm)) == null) { > System.out.println("Returning null"); > return null; > } > > int imWidth = (int) stripIm.getWidth() / number; > int height = stripIm.getHeight(); > int transparency = stripIm.getColorModel().getTransparency(); > > BufferedImage[] strip = new BufferedImage[number]; > Graphics2D stripGC; > > // each BufferedImage from the strip file is stored in strip[] > for (int i=0; i < number; i++) { > strip[i] = gc.createCompatibleImage(imWidth, height, > transparency); <===== > > // create a graphics context > stripGC = strip[i].createGraphics(); > <===== > // stripGC.setComposite(AlphaComposite.Src); > > // copy image > stripGC.drawImage(stripIm, > 0,0, imWidth,height, > i*imWidth,0, (i*imWidth)+imWidth,height, > null); > stripGC.dispose(); > } > return strip; > } // end of loadStripImageArray() > > The problem I'm having is with the for loop. If I create an abject > array of say 6 buffered images, how do I reference the indexed image > array to match the lines pointed to? In other words what is the > equivalent of strip[i] in Clojure? >
Hello, As far as possible, the equivalent of a java anArray[anIndex] expression will be of not using an index in the first place in Clojure (*). In your example, I'm not sure there is a compelling reason for having strip be a true java array. A clojure vector would probably be sufficient. So AFAICT, I would write your "loop" as: (defn with-gc* [gc f] (try (f gc) (finally (when gc (.dispose gc))))) (defmacro with-gc [[name gc-expr] & body] `(with-gc* ~gc-expr (fn [~name] ~@body))) (let [get-strip (fn [offset] (let [strip (.createCompatibleImage gc imWidth height transparency)] (with-gc [gc (.createGraphics strip ....)] (.drawImage gc) strip)))] (into [] (map get-strip (map #(* imWidth) (range number))))) Not tested, would probably place the fn on more than one line (*): of course, for when you /really/ want to do that / can't possibly do without that, you can , by using nth or get functions. Or by directly using java arrays. Sometimes. When you really don't see how to avoid this. -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en