I am combining Swing and Java3D together. Manipulations of swing components should be done using the Event Dispatcher Thread, while manipulations of Java3D components should be done in the BehaviourSchedulerThread.
Java3D renders the scene and then executes all of the behavior associated to the scene.
I have a MouseListener enabled on the Canvas3D. Events are posted into the AWT Event queue. I then want to modify the Java3D environment based on these events, so I use a special Behavior where I can post Runnable's to. This makes sure the Runnable's are executed during the Behavior cycle of Java3D (and do not modify anyting during the Render cycle).
Suppose some operations in the Behavior want to modify the Swing model. I then have to post a new Runnable to the EDT.
Is this the correct way to do it?
Using this technique, I experience a lot of problems on a mouse listener. I update a point in my Java3D model in the behaviour, and I update the swing GUI at the same time.
Update: The problem can be more clearly defined as follows:
I have a JButton "spin cube" which has an ActionListener. Once the ActionListener is fired, it pushes the AWTEvent into a Java3D Behavior. Once the Behavior fires, it modifies the Scene graph and then modifies the JButton actionListener and text to become "Stop spinning".
StopSpinningActionListener
.SpinActionListener
.What actually happens is the following:
AWTEvent
s get dispatched to SpinActionListener
. This creates a Runnable to execute inside the J3D Behavior.Obviously, I should not be depending on AWTEvent's getting processed sequentially. I cannot wait in the EDT for the behavior to fire, because any SwingUtilities.invokeAndWait() will then cause a deadlock.
Which WakeupCriterion
is used to wake-up your special Behavior object?
Java 3D's source code includes the utility classes
com.sun.j3d.utils.behaviors.mouse.MouseBehavior/MouseRotate
which listens to Canvas3D's AWTEvents
. One of two alternatives can be chosen:
MouseListener
with WakeupOnBehaviorPost
or WakeupOnAWTEvent
. This code sample might be helpful.
Initiating a Swing component update from within the Behavior.processStimulus
method via SwingUtilities.invokeLater
shouldn't cause any problems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With