We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am using processing 2.2.1 with minim and writing code in java with netbeans. I have a situation where my processing sketch is running that means the draw method is running. While it is running an event is triggered by other class. For that I wrote a custom event handler and registered it in the class which is running the sketch..But the class is unable to detect the event..Any help..TIA
Answers
Processing has its own event handling thread which is separate from the NetBeans event handling thread. Effectively you have two applications running in one window. NOTE regular Java applications have just a single event handling thread.
You might investigate a Java library called "EventsOnFire", it provides asynchronous event handling. I used it in the QScript library to fire events in one thread to be detected by Processing's event handling thread. Alternatively, if you are using NetBeans with Java 8 then you could try using JavaBeans to _bind _2 variables (one from each thread) and then a change handler in the class receiving the new value.
Whatever you use you will have to be careful that communicating between threads does not cause concurrent access exceptions.
If you don't need the Processing sketch to interact with the mouse or keyboard in other words just using it to display some graphics you could stop its event loop withnoLoop()
and the create a Java Swing timer to callredraw()
every 17 ms (60 fps). That way there is just a single event thread (NetBeans) so simplifies the problem.@quark Considering your answer, I would like to share what I have done-- I wrote a custom event handler for receiving a file over socket..The class which is drawing my processing sketch implements that listener and toggle a variable from true to false--for example-fileReceiveStatus..When the draw function is running I received a file and update the variable to true..My listener is also updating the variable..But If I keep a check on the variable in the draw() method it does not detect that the variable is updated to true from false...That's what my problem is..
Perhaps it detects so in the next frame?
@quark, noLoop() can't even stop the "Animation Thread", much less the "AWT-EventQueue-0"! :-@
It merely sets field looping to
false
:if (!looping) looping = true;
https://GitHub.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L2519-L2523
When the "Animation Thread" executes handleDraw(), it returns prematurely if looping is
false
:if (!looping && !redraw) return;
https://GitHub.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L2356-L2361
In short, neither threads ever stop. "Animation Thread" enters "standby", while "AWT-EventQueue-0" still keeps on checking for input events even when the former is "paused". B-)
So each "ignited" PApplet creates 2 Thread instances, not just 1.
"AWT-EventQueue-0" enqueues all user input events, while the "Animation Thread" dequeues them. :)>-
Then is it be possible to make some ^small^ changes to be able to add your own events?
Yes forget what I said about using noLoop etc.
Are you sure? Have you
println
it out?If it is changing but the change is not being seen in draw() then they can't both be looking at the same variable, its impossible!
Some extra corrections for the other renderers though. :-\"
"Animation Thread" & "AWT-EventQueue-0" are for the default renderer JAVA2D only! ~:>
For P2D & P3D, those threads are respectively:
And it seems like FX2D got 1 Thread only: "JavaFX Application Thread". @-)
@quark, it's still immensely useful to use noLoop() when it's not needed, b/c it stops the constant 60 FPS rendering, saving both CPU & GPU, even though the threads are still alive! O:-)
Make sure there's only 1
boolean
variable w/ declared w/ that name as a PApplet field.If it's still not working, declare it as
volatile
as well. :ar!I posted some code here which allows the user to see what threads are in existence at runtime. Might be useful to someone.
The reason for using noLoop() and redraw() was to bring the drawing of the sketch to be executed inside the main AWT event thread. I think the redraw() method simply 'invites' the applet to execute draw() at the next opportunity. If true then it is still inside the Processing thread so it would not do what I hoped.Might be worth trying but call the darw() method directly from the Swing timer. May or may not work I have no ides but probably worth trying.@quark, seems like you didn't pay attention on this statement at the beginning of handleDraw():
if (!looping && !redraw) return;
As I have stated in my 1st reply here, handleDraw() merely quits prematurely after checking out those 2
boolean
fields.It's useless to directly invoke draw() b/c it won't trigger the PSurface to render the current content of main canvas PGraphics. 8-X
Even when noLoop() is active, draw() is still executed under the "Animation Thread" after redraw().
Although inside input event callbacks, it's the "AWT-EventQueue-0" Thread which executes them:
I made some off-the-cuff remarks without thinking them through so I have crossed out all the extraneous comments, leaving the stuff I am happy to have said. :D
It might get us back to the OP's original problem.
I think code sharing might be of some help...Sorry for some unused code...If you could kindly take time to see the code and give me solution...
TIA
In Client.java I made the gui to load the processing sketch..I made a custom event listener to trigger when a file is received...I have an interface FileReceivingListener..Then the dessign.java class which does the work for spectrum analysis and implemetns the interface..
Client.java
dessign.java
I printed the fileReceived boolean variable in draw..but its always false... Apologies for posting long codes..But I'm stuck here badly :( @quark @GoToLoop
https://forum.Processing.org/two/discussions/tagged?Tag=papplet.runsketch()
Things to try.
1) Add a log info report for the catch clause in line 53/54 of the Client class. Are any exceptions being thrown here? I think you might have some other exceptions that you are not detecting. Either that or the connectionsss method never throws an IO exception because it is being caught and ignored in the try/catch. (in which case you don't get to see any output from (3) below.
2) Are the statements in line 222 & 224 of the Dessign class printing anything?, any why not use the logger?
3) Add a log info report in actionPerformed method line 216. Might as well test that as well.
Just noticed what else might be a problem.
In static void main you create a Dessign object and add a listener but then replace it in the connectionsss() method. NOT a good idea since this class is your main PApplet class, in other words effectively a Processing sketch. There should only be ONE Dessign class object instantiated.
At last solved.. thanks @quark... Problem was: I was instantiating multiple object and trying to work them on the single processing sketch..Now I instantiated one object and used it for listener and sketch and wherever I needed..I guess it was silly programming error. :p