Paintable Canvas - Radial Gradient Throws Error

Hi All,

I’ve been trying to get the Java “RadialGradientPaint” class to work in the paintable canvas but without any success.

The standard “GradientPaint” class works fine. I can use the Point2D class do define my x, y coordinates, hold different color instances in Jython variables, etc. Everything works fine. But when I try to apply the same concepts to a “RadialGradientPaint” instance, Ignition throws me this error:


I am at my wits end! I cannot figure out why it will not work. Any help would be much appreciated. Below is a copy of the script that I have in the paintable canvas. Thanks in advance for your help.

import java.util.ArrayList as ArrayList
from java.awt import Color
from java.awt import Paint
from java.awt import GradientPaint
from java.awt import LinearGradientPaint
from java.awt import MultipleGradientPaint
from java.awt import RadialGradientPaint
from java.awt.geom import Ellipse2D
from java.awt.geom import Point2D
from java.lang import Float, Double

#define Java Arrays

coverageArray = ArrayList()
colorArray = ArrayList()

#declare Java Graphics2D instance

g = event.graphics

#declare colors and put into Java array

colorDarkGrey = Color(14,14,14)
colorLightBlue = Color(51,153,204)
colorArray.add(colorDarkGrey)
colorArray.add(colorLightBlue)

#add coverage distribution points to Java Array

coverageArray.add(0.0)
coverageArray.add(1.0)

#build an Java Ellipse2D instance

myEllipse = Ellipse2D.Float(1, 1, 98, 98)

#find the center of the ellipse and put into a Java Point2D instance

myCenter = Point2D.Float((event.width / 2), (event.height / 2))

#create a Java RadialGradientPaint instance

newPaint = RadialGradientPaint(myCenter, 20.0, coverageArray, colorArray)

#build the ellipse object and fill with the radial gradient

g.setPaint(newPaint)
g.fill(myEllipse)
g.draw(myEllipse)

In Python the first argument to RadialGradientPaint needs to be a Rectangle2D object, not a Point2D.Float. That is the problem.

Calling constructors and methods in Jython is not exactly the same as in Java. In Jython, which constructor or method is called is not based on the type of its arguments (as in Java), it is based on the number of arguments used. This conflicts with Java.

To pass in a Point2D.Float instead of a RadialGradientPaint you might consider calling a 5-argument form of the constructor.

Best,

Hi Nick,

Thank you very much for the quick reply, and all the resources you post online here and on your website. It’s much appreciated.

I tried your suggestion, and with a few tweaks was able to get it to work. Below is the functioning code:

from 	java.awt 		import Color
from 	java.awt 		import Paint
from 	java.awt		import MultipleGradientPaint
from 	java.awt 		import RadialGradientPaint
from 	java.awt.geom 	import Ellipse2D
from 	java.awt.geom 	import Rectangle2D

#declare Java Graphics2D instance

g = event.graphics

#declare colors and put into a Jython list

colorDarkGrey 	= Color(14,14,14)
colorLightBlue	= Color(51,153,204)
colorArray		= [colorDarkGrey,colorLightBlue]

#add coverage distribution points to Jython list

coverageArray	= [0.0, 1.0]

#build an Java Ellipse2D instance and a Java Rectangle2D instance

myEllipse	= Ellipse2D.Float(1, 1, 98, 98)
myRectangle	= Rectangle2D.Float(1,1,98,98)

#create a Java RadialGradientPaint instance

newPaint = RadialGradientPaint(myRectangle,coverageArray, colorArray, MultipleGradientPaint.CycleMethod.NO_CYCLE) 

#build the ellipse object and fill with the radial gradient

g.setPaint(newPaint)
g.fill(myEllipse)
g.draw(myEllipse)

I guess what was confusing me was the Java documentation. According to their online resources (https://docs.oracle.com/javase/8/docs/api/java/awt/RadialGradientPaint.html) the following options are available for declaring a “RadialGradientPaint” instance:


I though you could use any of these methods in Jython, but apparently not. The only one that works is, as you suggested, the last, which requires a Rectangle2D object. Trying to use any of the other methods always throws an error. Great catch, I would never have thought to try that.

By the way, do you know of any good resources (in addition to Google) that help with this Java/Jython converting. Generally it is pretty straight forward, I just look up the Java structure, and try to code it in a more ‘Jythonic’ manner, but every once in a while some method (like this one) will have me banging my head against the wall.

Many thanks again for the help.

Derrick

1 Like

Hi Derrick,

Glad to help. I don’t know of good Jython resources other than what comes up with Google. There are some Jython books on Amazon that may be useful.

Best,