How to access specific java.awt.Color constructor?

170 views
Skip to first unread message

stu

unread,
Apr 9, 2011, 6:13:28 PM4/9/11
to Clojure
Hi,

I'm trying to create a java.awt.Color class instance from within
Clojure, using this constructor:

Color(ColorSpace cspace, float[] components, float alpha)

But always end up with a "No matching ctor found for class
java.awt.Color" exception.

I can create the ColorSpace instance as I expected, but I think I'm
missing something with the rest of the Color constructor. I've tried
changing the components to a vector of floats or a Java array of
floats with no luck.

This is with Clojure 1.2 and the Java Platform SE 6 AWT classes.

Any advice much appreciated.

Stu

Sean Corfield

unread,
Apr 9, 2011, 6:21:11 PM4/9/11
to clo...@googlegroups.com
Can you show a bit more of the code you have so we can see what you've tried?

Alan

unread,
Apr 9, 2011, 6:35:37 PM4/9/11
to Clojure
You probably weren't putting it into a java float array, but instead a
Float array (boxed type). Here's a repl session doing what you want. I
don't know anything about colorspaces so it's probably nonsense, but
it creates a Color with that constructor.

user=> (import '(java.awt.color ICC_Profile ColorSpace ICC_ColorSpace)
java.awt.Color)
java.awt.Color
user=> (def prf (ICC_Profile/getInstance (ColorSpace/CS_sRGB)))
#'user/prf
user=> (def spc (ICC_ColorSpace. prf))
#'user/spc
user=> (class spc)
java.awt.color.ICC_ColorSpace

;; the magic words
user=> (def c (Color. spc (float-array [1 1 1]) (float 1)))
#'user/c
user=> (def c (Color. spc (into-array Float/TYPE [1 1 1]) (float 1)))
#'user/c

stu

unread,
Apr 10, 2011, 12:11:52 AM4/10/11
to Clojure
On Apr 10, 8:35 am, Alan <a...@malloys.org> wrote:

> You probably weren't putting it into a java float array, but instead a
> Float array (boxed type). Here's a repl session doing what you want. I
> don't know anything about colorspaces so it's probably nonsense, but
> it creates a Color with that constructor.

Thanks Alan -- I realize now there's a subtle but important
distinction there when doing Java interop.

Stu
Reply all
Reply to author
Forward
0 new messages