Use a function from a class to open a 2nd window

Hello,
I am trying to open a 2nd window by using a function from a class (DHTable) i made. I plan on then plotting ellipses or rectangles based on variables in an instance of DHTable. I have seen other code on the forum that opens a 2nd window by creating a class for the 2nd window which then has functions inside that are used. The best I can think of is somehow nest one class into the other, but it doesn’t make sense to me to put an instance of DHTable inside the 2nd window class since the variables would not hold the correct values. As for putting the 2nd window class into DHTable I’m not sure how I would do that. Can someone give me advice on how to best approach this problem? If I didn’t make much sense or if code is required let me know and I will try to respond as soon as I can.

Thanks.

1 Like

Opening another window in Processing is akin to have a separate class w/ a new set of callbacks such as settings(), setup(), draw(), etc:



If your class DHTable directly issues “drawing” commands such as ellipse(), fill(), etc., not only it has to know which PApplet’s canvas those commands will act upon, it’s gotta make sure they’re executed under the same Thread as that PApplet’s reference!

2 Likes

Hello,
sorry about respoding so late I got busy and also wanted to wait until I had some working code(which I kinda still don’t have). So taking what you said (i think) and some of the code from the posts you gave I ended up with the following.
Code to run OtherWindow class

from OtherWindow import *

ellipse(0,0,20,20)
sw = OtherWindow()
sw.test([100,200])

OtherWindow class

class OtherWindow(PApplet):  
        
    def __init__(self):
        switches = ('--sketch-path=' + sketchPath(), '')
        PApplet.runSketch(switches, self)
    
    def settings(self):
        self.size(300,200)
        
    def setup(self):
        print("HERE")
    
    def test(self,a):
        print(a)
        self.ellipse(a[0],a[0],30,30)
        self.ellipse(a[1],a[1],30,30)

The above code opens the second window with the ellipses in the positions given by a. This is good but when I try to change the code or add certain things I start to get problems.

If I want to set the background to a certain color. Adding self.background(0) to setup() or test() gives me a null pointer exception but the background does change to black.

If I move the size() function from settings() into setup(). the test() function gets called correctly and the size of the 2nd window does not get changed.

Getting rid of setup() causes the test() function to not work but no errors. This is why I have the random print(“HERE”) code added.

The last problem that happens might not be an error but it is weird. After I exit out of the 2nd window if I try to run the code again the processing IDE will freeze for a little then when it unfreezes I have to click run again to actually run the code. This has happened each time I closed the 2nd window, but I can avoid it by only closing the 1st window which leaves the 2nd window up. This allows me to run the code on the first try but will cause multiple “2nd windows” to stay on screen since they never go away, unless I close one which closes them all.

All of these problems are really confusing me and I have no clue how to fix them. I’m sure they are happening due to my lack of understanding of the code I am using, but i’m not the best coder so a lot of this stuff goes over my head. If I could get some help with this it would be greatly appreciated.

Thanks.

1 Like

You can’t invoke method OtherWindow::test() from another Thread but OtherWindow’s own “Animation” Thread, as I had stated at the end of my 1st reply:

That’s b/c method OtherWindow::test() mutates OtherWindow’s canvas:

If that method did something else, it could be invoked by other threads.
Although concurrent modification race could still occur.

1 Like