Purpose of the script: Take a byte array coming from a binary file and arrange it into an image. The byte array is being generated and written by Matlab. I know the values are making it into the script correctly.
Problem: I'm getting this attribute error on the display statement.
Caused by: org.python.core.PyException: Traceback (most recent call last):
File "event:actionPerformed", line 29, in
AttributeError: 'java.awt.event.ActionEvent' object has no attribute 'graphics'
Note: I can find a similar drawimage statement on example pages such as:
I also have almost this same code working on another page. The difference is that it generates a random number for each element (pixel) rather than reading it from a file, and the image is 100x100 instead of 10x10.
Check the code, I'm not even importing java.awt.event, unless its inherited.
So, where is this going wrong?
{start script}
from java.awt.image import BufferedImage
from java.awt.image import Raster
from java.awt import Point
fred = system.file.readFileAsBytes('D:\ProjectsCurrent\Miles\VisionSystem\\test3.dat')
#This works to get the correct (0-255) value coming in.
bob=[ ]
for item in fred:
joe=item & 0xFF
bob.append(joe)
#print(joe)
print(bob)
image = BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
raster = image.getData();
sm = raster.getSampleModel();
writeRaster = Raster.createWritableRaster(sm,Point(0,0))
for i in range(10):
for j in range(10):
writeRaster.setSample(i, j, 0, bob[i*10+j])
image.setData(writeRaster);
print(image)
event.graphics.drawImage(image,0,0,event.source)
print('made it to the end')
Are you trying to display this in a Paintable canvas, or some other component.
event.graphics is in the repaint event of the Paintable Canvas as a java.awt.Graphics2D instance, but it looks like you're trying to use it in a button.
Ah....you mean the icon on the menu bar....? I found the icon in replying to this post after identifying containing the code in a block from this post.
Yes, and Yes.
The reason I'm bringing it in via a button is because the automatic repaint on paintable canvas has a tendency to hang ignition if I screw it up badly enough.
and...
Oops. Yeah, I need to pass the array to the paintable canvas before I try to display it.
Thanks.
Consider using .putClientProperty to cache the image in BufferedImage form, so the repaint event has the least possible work to do (Swing can call it at inconvenient times). Some topics:
That often happens if that script calls out across the network for any data it needs. That is bad. All necessary data should be brought to the component separately, preferably via background bindings, so the repaint event only needs to access properties locally.
Edit: by locally, I mean in memory. You are reading a file inside your repaint event. Not a good idea.
Also, manipulating Swing components from a background thread can crash your whole application (Ignition Vision components are Swing subclasses). See this topic (and linked ones):
Yep, that did the trick. It took me a bit to figure out how to use .putClientProperty and .getClientProperty (I am most definitely NOT a Java person), but I have successfully turned a Matlab cat into a binary file and reassembled him in ignition. Thanks.