I just discovered jME and it looks like just the thing I was after.
I’m looking into mixing jME with Swing GUI.
I followed the tutorial and all worked well, managed to get the jME and Swing GUI in one JFrame, so I moved on to the next thing, trying to control the speed of rotation (from the simpleUpdate loop tutorial) using JButtons from the GUI, that’s when things stopped working.
Nothing happens as I click the button (i’m using addActionListener). I’m guessing because of threading issues and stuff. I have no idea how to use callables within a JButton. I’d appreciate if someone could walk me thru this.
this is my code:
[java]package test1;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.system.AppSettings;
import com.jme3.system.JmeCanvasContext;
/**
Sample 2 - How to use nodes as handles to manipulate objects in the scene.
You can rotate, translate, and scale objects by manipulating their parent
nodes. The Root Node is special: Only what is attached to the Root Node
appears in the scene.
*/
public class SwingCanvasTest extends SimpleApplication {
private Geometry geom;
private static float rotate = 2.0f;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
AppSettings settings = new AppSettings(true);
settings.setHeight(480);
settings.setWidth(640);
SwingCanvasTest canvasApp = new SwingCanvasTest();
ah so your trying to share the same variable between threads. I’m not so good at threads myself, but i think you may need to look at synchronizing them, as there isn’t an Atomic equivalent.
@Setekh said:
It works ether way :P
You don't need synchronization, the update loop gets the value asynchronously when he needs it.
Which code are you looking at?
The one I saw was sharing an unsynchronized non-atomic static variable. This means that if you are on a multi-core system that one thread could see changes to that value very late because there is no memory barrier forcing a refresh of the thread's local cache. (basically)
You could achieve the same thing by either marking the variable as volatile or by passing it into the enqueued task as a local variable. Making it atomic also works though so you have a few options
@zarch said:
You could achieve the same thing by either marking the variable as volatile or by passing it into the enqueued task as a local variable. Making it atomic also works though so you have a few options :)
Yeah, I usually recommend the atomics over volatile just because they are more flexible and require slightly less explanation. :) The atomics are nice because you can do test-and-set style operations which frequently come up.
I was talking about the update loop, not how he coded it. Tho thank you for enlightening me, i would of use volatile but really if i was faced with the issue i couldn’t explain it
@Setekh said: @pspeed
I was talking about the update loop, not how he coded it. Tho thank you for enlightening me, i would of use volatile but really if i was faced with the issue i couldn't explain it :P
I learned more than I ever wanted to know about Java's threading model back before JDK 1.5 when "double checked locking" was impossible to implement (volatile had no real semantic meaning prior to 1.5). Reading and re-reading those articles provided lots of interesting info on the underbelly of the threading model. Reading anything from Doug Lea or going through the source for the concurrent package is also very educational.