# Paintable Canvas - Radial Gradient Throws Error

**URL:** <https://forum.inductiveautomation.com/t/paintable-canvas-radial-gradient-throws-error/10432>\
**Category:** General Discussion\
**Created:** [September 3, 2015, 9:08pm UTC](https://forum.inductiveautomation.com/t/paintable-canvas-radial-gradient-throws-error/10432 "2015-09-03T21:08:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Derrick\_Anseth](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@Derrick\_Anseth](https://forum.inductiveautomation.com/u/Derrick_Anseth)\
**Post date:** [September 3, 2015, 9:08pm UTC](https://forum.inductiveautomation.com/t/paintable-canvas-radial-gradient-throws-error/10432/1 "2015-09-03T21:08:18Z")

</div>

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:

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/d/d460cc117d30d67f3dade5511973f1046386dcdb.PNG)  

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)

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [September 3, 2015, 10:04pm UTC](https://forum.inductiveautomation.com/t/paintable-canvas-radial-gradient-throws-error/10432/2 "2015-09-03T22:04:30Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![Derrick\_Anseth](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@Derrick\_Anseth](https://forum.inductiveautomation.com/u/Derrick_Anseth)\
**Post date:** [September 4, 2015, 3:01pm UTC](https://forum.inductiveautomation.com/t/paintable-canvas-radial-gradient-throws-error/10432/3 "2015-09-04T15:01:57Z")

</div>

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:

```auto
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](https://docs.oracle.com/javase/8/docs/api/java/awt/RadialGradientPaint.html)) the following options are available for declaring a “RadialGradientPaint” instance:

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/2/2d90b09c19c2ac571547e9b8316dd26e4fab83e1.PNG)  

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

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [September 4, 2015, 7:18pm UTC](https://forum.inductiveautomation.com/t/paintable-canvas-radial-gradient-throws-error/10432/4 "2015-09-04T19:18:37Z")

</div>

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,
