# event.graphics.drawImage conflict?

**URL:** <https://forum.inductiveautomation.com/t/event-graphics-drawimage-conflict/67455>\
**Category:** Ignition\
**Created:** [December 9, 2022, 11:31pm UTC](https://forum.inductiveautomation.com/t/event-graphics-drawimage-conflict/67455 "2022-12-09T23:31:30Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![rod.holland](https://avatars.discourse-cdn.com/v4/letter/r/e495f1/32.png) [@rod.holland](https://forum.inductiveautomation.com/u/rod.holland)\
**Post date:** [December 9, 2022, 11:31pm UTC](https://forum.inductiveautomation.com/t/event-graphics-drawimage-conflict/67455/1 "2022-12-09T23:31:30Z")

</div>

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:

> **[Working with the Paintable Canvas Component](https://support.inductiveautomation.com/hc/en-us/articles/360047155032-Working-with-the-Paintable-Canvas-Component)**
>
> The Paintable Canvas is a very powerful advanced component that allows displaying custom graphic content using the Java Graphics2D API. Basic information about the component is available in the App...

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}

```auto
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')

```

{end script}

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [December 10, 2022, 11:39am UTC](https://forum.inductiveautomation.com/t/event-graphics-drawimage-conflict/67455/2 "2022-12-10T11:39:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 10, 2022, 3:58pm UTC](https://forum.inductiveautomation.com/t/event-graphics-drawimage-conflict/67455/3 "2022-12-10T15:58:14Z")

</div>

{ You can edit your post to format your script as code. Highlight all the code then click the \</\> button. }

---

<div class="post-metadata">

**Author:** ![rod.holland](https://avatars.discourse-cdn.com/v4/letter/r/e495f1/32.png) [@rod.holland](https://forum.inductiveautomation.com/u/rod.holland)\
**Post date:** [December 12, 2022, 4:55pm UTC](https://forum.inductiveautomation.com/t/event-graphics-drawimage-conflict/67455/4 "2022-12-12T16:55:58Z")

</div>

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.

> **[How to Expedite Answers on the Forum | Inductive Automation](https://inductiveautomation.com/news/how-expedite-answers-forum)**
>
> Getting help fast is easy on the Inductive Automation Forum. If you’ve got questions about our software, need help getting through a tricky part of your project, or think you may have found a bug, you’ve come to the right place. Please take a minute...

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 12, 2022, 4:57pm UTC](https://forum.inductiveautomation.com/t/event-graphics-drawimage-conflict/67455/5 "2022-12-12T16:57:33Z")

</div>

Yes. You should use it for any line-oriented plain text that you paste, like error backtraces, too.

---

<div class="post-metadata">

**Author:** ![rod.holland](https://avatars.discourse-cdn.com/v4/letter/r/e495f1/32.png) [@rod.holland](https://forum.inductiveautomation.com/u/rod.holland)\
**Post date:** [December 12, 2022, 5:02pm UTC](https://forum.inductiveautomation.com/t/event-graphics-drawimage-conflict/67455/6 "2022-12-12T17:02:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 12, 2022, 5:08pm UTC](https://forum.inductiveautomation.com/t/event-graphics-drawimage-conflict/67455/7 "2022-12-12T17:08:23Z")

</div>

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:

[https://forum.inductiveautomation.com/search?q=putClientProperty%20canvas%20order%3Alatest](https://forum.inductiveautomation.com/search?q=putClientProperty%20canvas%20order%3Alatest)

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 12, 2022, 5:10pm UTC](https://forum.inductiveautomation.com/t/event-graphics-drawimage-conflict/67455/8 "2022-12-12T17:10:10Z")

</div>

> [@rod.holland](#):
>
> repaint on paintable canvas has a tendency to hang ignition

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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 12, 2022, 5:13pm UTC](https://forum.inductiveautomation.com/t/event-graphics-drawimage-conflict/67455/9 "2022-12-12T17:13:44Z")

</div>

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):

> [@Update progress bar from within a script](https://forum.inductiveautomation.com/t/update-progress-bar-from-within-a-script/21995):
>
> I have this script on a button to copy data to a PLC from a database. Since this script takes 10 - 20 seconds to run, i would like to have the progress bar show progress. Issue I have is the progress bar updates only when the script has completed. Is there anyway to get value outside of the script to update as the script is running?

---

<div class="post-metadata">

**Author:** ![rod.holland](https://avatars.discourse-cdn.com/v4/letter/r/e495f1/32.png) [@rod.holland](https://forum.inductiveautomation.com/u/rod.holland)\
**Post date:** [December 13, 2022, 10:37pm UTC](https://forum.inductiveautomation.com/t/event-graphics-drawimage-conflict/67455/10 "2022-12-13T22:37:59Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/9/59521904e090ff86deead5ed824265afab35dee3.jpeg)
