Discussion:
Need some help with Java - Kawa interop
Arie van Wingerden
2018-11-01 13:44:47 UTC
Permalink
Trying to generate a random color for a shape in JavaFX.
(define (randomColor)
(let (

(rnd (java.lang.Math:random))

(r (java.util.Random:nextInt 255))

(g (java.util.Random:nextInt 255))

(b (java.util.Random:nextInt 255)))

(Color:rgb r g b)))


Generates warnings:
.\test.scm:7:34: warning - type integer is incompatible with required type
java.util.Random
.\test.scm:8:34: warning - type integer is incompatible with required type
java.util.Random
.\test.scm:9:34: warning - type integer is incompatible with required type
java.util.Random
.\test.scm:7:8: warning - cannot convert literal (of type gnu.math.IntNum)
to ClassType java.util.Random
.\test.scm:8:8: warning - cannot convert literal (of type gnu.math.IntNum)
to ClassType java.util.Random
.\test.scm:9:8: warning - cannot convert literal (of type gnu.math.IntNum)
to ClassType java.util.Random

What am I doing wrong?

TIA
Per Bothner
2018-11-02 05:08:11 UTC
Permalink
Post by Arie van Wingerden
Trying to generate a random color for a shape in JavaFX.
(define (randomColor)
(let (
(rnd ((java.lang.Math:random))
(r (java.util.Random:nextInt 255))
(g (java.util.Random:nextInt 255))
(b (java.util.Random:nextInt 255)))
(Color:rgb r g b)))
nextInt is not static method, so you need an instance.

(define (randomColor)
(let* ((rnd (java.util.Random))
(r (rnd:nextInt 255))
(g (rnd:nextInt 255))
(b (rnd:nextInt 255)))
(java.awt.Color r g b)))
--
--Per Bothner
***@bothner.com http://per.bothner.com/
Loading...