Paintable Canvas Basics

This is a copy/paste from the provided paintable canvas. I commented out most of it just trying to get a color to change based on a tag status. I must be missing something simple. Your help is much appreciated.

from java.awt import Color
from java.awt import GradientPaint
from java.awt.geom import GeneralPath
from java.awt.geom import Rectangle2D
from java.awt.geom import Ellipse2D

g = event.graphics

Create shapes assuming 100x100 size, then scale the graphics

to proper size later

Pump Output

##nozzle = Rectangle2D.Float(92, 0, 8, 28)
##pipe = Rectangle2D.Float(50, 4, 45 , 20)

Body

outerBody = Ellipse2D.Float(4,4,8,80)
innerBody = Ellipse2D.Float(8,8,72,72)

Feet

##feet = GeneralPath()
##feet.moveTo(2,98)
##feet.lineTo(28,50)
##feet.lineTo(58,50)
##feet.lineTo(82,98)
##feet.lineTo(62,98)
##feet.lineTo(50,60)
##feet.lineTo(36,60)
##feet.lineTo(22,98)
##feet.closePath()
##foot1 = Rectangle2D.Float(0,92,28,8)
##foot2 = Rectangle2D.Float(56,92,28,8)

#Status
##status = Ellipse2D.Float(20,30,7,7)

Tip - make the bounds of these rectangles dynamic for real status

##amps = Rectangle2D.Float(25,53,5,15)
##hz = Rectangle2D.Float(30,45,5,23)

Scale graphics to actual component size

dX = (event.width-1)/100.0
dY = (event.height-1)/100.0
g.scale(dX,dY)

Paint Shapes

Paint Feet

##g.setColor(Color.GRAY)
##g.fill(feet)
##g.setColor(Color.DARK_GRAY)
##g.draw(feet)
##g.fill(foot1)
##g.fill(foot2)

Paint output pipe and nozzle

##g.setPaint(GradientPaint(0,12,Color.WHITE, 0,30, Color.DARK_GRAY,1))
##g.fill(pipe)
##g.setColor(Color.DARK_GRAY)
##g.draw(pipe)
##g.setPaint(GradientPaint(0,8,Color.WHITE, 0,28, Color.DARK_GRAY,1))
##g.fill(nozzle)
##g.setColor(Color.DARK_GRAY)
##g.draw(nozzle)

Paint body

g.setPaint(GradientPaint(40,40,Color.WHITE, 0,0, Color.DARK_GRAY,1))
g.fill(outerBody)
g.setColor(Color.DARK_GRAY)
g.draw(outerBody)
g.setPaint(GradientPaint(0,40,Color.WHITE, 0,100, Color.DARK_GRAY,1))
g.fill(innerBody)
if “motor 2” == 1:
g.setColor(Color.GREEN)
else:
g.setColor(Color.DARK_GRAY)
g.draw(innerBody)

Paint status indications - These would draw from dynamic

properties in a real implementation

##g.setColor(Color.GREEN)
##g.fill(status)
##g.setColor(Color.GREEN.darker())
##g.draw(status)
##g.drawString(“AUTO”,30,38)

##g.setFont(g.font.deriveFont(g.font.size-2.0))
##g.setColor(Color.BLUE)
##g.fill(amps)
##g.drawString(“Amps”, 42,55)
##g.setColor(Color.MAGENTA)
##g.fill(hz)
##g.drawString(“Hz”, 42,65)

It looks like you have to explicitly tell the canvas to repaint itself.

Create a custom boolean property on your canvas called changeColor:

Add a binding for the custom property to point to a tag that changes:

Change the if statement in your code:

[...snip...]
if event.source.changeColor:
	g.setColor(Color.GREEN)
else:
	g.setColor(Color.DARK_GRAY)
[...snip...]

And add the following to the propertyChange event of the canvas:

if event.propertyName == "changeColor":
	event.source.repaint()