Passing parameters from swing to jme3 (controling animation)

http://img513.imageshack.us/img513/6787/patternto.jpg


I have a task to create an application (GUI) as shown in the diagram above. I want to add to Swing a window with animations which are controlled in Swing. I was able to launch it nicely in the Canvas. What I need now is to pass parameters which will launch a specific animation. Do you have any advice on the best way to do the transmission of parameters to a running thread and updating it? I would be grateful for providing a sample code.

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:multithreading

Thank you! I saw this tutorial and now you confirmed my assumption that is the right solution.

@g-manio007 said:
Thank you! I saw this tutorial and now you confirmed my assumption that is the right solution.


Yeah, it's not necessarily obvious at first but swing runs on its own thread (AWT) and JME runs its render/update on a different thread (lwjgl render thread).

I want to control animation from swing by choosing animation name : for example transmission String from Swing thread to JME thread.



Which method should I use? :


  1. Control Update() Method
  2. The Callable
  3. The jME3 Threading Model
  1. You should not call control update yourself and even if you did then you should do it from the JME thread.
  2. Do you mean enqueueing a callable on the JME thread?
  3. …which is enqueueing a callable on the JME thread.



    So, I think they are almost all the same answer.

I have idea like this:



[java]public void simpleUpdate(float tpf)

{

channel.setAnim(“animation_name”);

}

[/java]





I want to change String=“animation_name” from swing by pressing JButton. Should I call method (which?) from JME in order to get value of String or instead of this should I call method from Swing which can change value of String stored in JME?



Sorry for this simple question, I am beginner in multi-threading.

At your level of threading knowledge it is probably safest just to enqueue a callable that sets the animation name. I again refer you to:

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:multithreading

Hi again. Thanks for help , I have read article carefully and learn some tutorials about threads and I think I know how this work. I’ve written simple example which generally work. But I know that there is big difference about only working and working properly for application. I would ask someone experienced if my code is correct in respect of multi-threading. Here is my simple ideological code :

[java]

(…)



//HERE IS MY SWING PANEL WHERE I AM ADDING JME CANVAS

class forum extends JPanel implements Callable<Integer>{



private static final long serialVersionUID = 1L;

public forum() {



prawo=new animation_test();

prawo.createCanvas();

rootNode=prawo.getRootNode();

JmeCanvasContext ctx = (JmeCanvasContext) prawo.getContext();

ctx.setSystemListener(prawo);

Dimension dim = new Dimension(100, 100);

canvas = ctx.getCanvas();

canvas.setPreferredSize(dim);

add(canvas);



listener sluchacza=new listener();

Timer czas=new Timer(2000,sluchacza);

czas.start();



}



private class listener implements java.awt.event.ActionListener

{

public void actionPerformed(ActionEvent e) {

a++;

if(a==2)

a=0;

}



}



public Integer call() throws Exception {

return a;

}



private Node rootNode;

private Canvas canvas;

private animation_test prawo;

private int a=0;

}



//AND THERE IS MY SIMPLE JME CLASS (ONLY UPDATE LOOP)



class animation_test extends SimpleApplication {

public void simpleInitApp() {

(…)

}

public void simpleUpdate(float tpf) {





executor=Executors.newCachedThreadPool();





Future<Integer> future=executor.submit(panel);

int h=0;

try {

h=future.get();

} catch (InterruptedException | ExecutionException e) {

e.printStackTrace();

}

if(h==1)

{

player.rotate(0, 0, tpf);

h=-1;

}

}

private static ExecutorService executor=Executors.newCachedThreadPool();

private static Panel panel=new Panel();

}





[/java]

Can anyone tell me is it right approach?

You just need to enqueue the call like shown in the bottom of that page.

[java]



app.enqueue(new Callable(){

public Object call(){

//DO SCENEGRAPH STUFF HERE

return null;

}

});

[/java]

it’s mean this??? this works. But what is the difference??





[java]

public Object call() throws Exception {



HelloAnimation2 H=new HelloAnimation2();

H.enqueue(new Callable<Integer>()

{



@Override

public Integer call() throws Exception

{

return 0;

}



});



return a;

}



[/java]

@g-manio007 said:
it's mean this??? this works. But what is the difference??


[java]
public Object call() throws Exception {

HelloAnimation2 H=new HelloAnimation2();
H.enqueue(new Callable&lt;Integer&gt;()
{

@Override
public Integer call() throws Exception
{
return 0;
}

});

return a;
}

[/java]


Why are you creating your application again? How many applications do you need in your application?

Why doesn't your callable actually do anything?

You said you "have a task" to do this... who gave you that task? Is it for work or school or just something you decided on your own?

Learning to program in Java is hard. Learning to write 3D games is really really hard. Learning to do both at the same time is nearly impossible. Maybe pick a simpler project to start with until you are better at Java.

I thought that this form can provide help even in basic matters instead of discourage. I think that there is misunderstanding. I can promise that I won’t bother you once again. And don’t tell me what is impossible. Just two month ago I didn’t know Java at all and I learnt java by myself and no one helped me. In my opinion it’s not a sin to ask some advanced user for advice.

@g-manio007 said:
I thought that this form can provide help even in basic matters instead of discourage. I think that there is misunderstanding. I can promise that I won't bother you once again. And don't tell me what is impossible. Just two month ago I didn't know Java at all and I learnt java by myself and no one helped me. In my opinion it's not a sin to ask some advanced user for advice.

Nobody tried to discourage you, @pspeed suggested you start at simpler projects.
Why didn't you answer the questions in the post from @pspeed? Because you cannot. I'm afraid that makes it hard to help you.
Also I suggest starting with the things our tutorials show instead of applying your own ideas (Swing + OpenGL) which make it only harder for you.
@g-manio007 said:
I thought that this form can provide help even in basic matters instead of discourage. I think that there is misunderstanding. I can promise that I won't bother you once again. And don't tell me what is impossible. Just two month ago I didn't know Java at all and I learnt java by myself and no one helped me. In my opinion it's not a sin to ask some advanced user for advice.


Don't be insulted when people offer you a more gradual way of learning something that you are clearly struggling with. From your perspective you don't see how badly you are struggling but we can easily see the dozens of concepts you are still not understanding about this subject. You still don't "know Java". It takes years. I'm just offering advice. You will struggle daily on this path for about a year if you keep going, I think.

But to be nice. I will give you the exact answer you need because I'm feeling generous today.

First, a few things:
-if you ever find yourself creating your main class or "application" over and over... you are doing something wrong. Take a step back and rethink what you are doing.
-If you ever find yourself having your main class or a panel or something implement an interface like Callable or XXXListener then you are also probably doing something wrong. Take a step back and rethink what you are doing.
-read carefully the examples people give you until you understand them. Look for many existing examples because they are everywhere. You haven't really understood the examples provided yet. If you can't explain why every line of code is there then you didn't really understand it.
I will try to modify your own example as best as I can...
[java]
(....)

//HERE IS MY SWING PANEL WHERE I AM ADDING JME CANVAS
class forum extends JPanel {

private static final long serialVersionUID = 1L;
public forum() {

prawo=new animation_test();
prawo.createCanvas();
rootNode=prawo.getRootNode();
JmeCanvasContext ctx = (JmeCanvasContext) prawo.getContext();
ctx.setSystemListener(prawo);
Dimension dim = new Dimension(100, 100);
canvas = ctx.getCanvas();
canvas.setPreferredSize(dim);
add(canvas);

listener sluchacza=new listener();
Timer czas=new Timer(2000,sluchacza);
czas.start();

}

private class listener implements java.awt.event.ActionListener
{
public void actionPerformed(ActionEvent e) {
a++;
if(a==2)
a=0;
}

}

public void doTheStuffInJme() {
animation_test.enqueue( new Callable() {
public Object call() {
return animation_test.doSomeStuff();
}
});
}

private Node rootNode;
private Canvas canvas;
private animation_test prawo;
private int a=0;
}

//AND THERE IS MY SIMPLE JME CLASS (ONLY UPDATE LOOP)

class animation_test extends SimpleApplication {
public void simpleInitApp() {
(...)
}

public Object doSomeStuff() {
// Do the stuff here that needs to happen on the JME thread

// And we have nothing to return but we could
return null;
}

public void simpleUpdate(float tpf) {


if(h==1)
{
player.rotate(0, 0, tpf);
h=-1;
}
}
}


[/java]


Or something like that.

If you want to learn Java, I suggest writing something simpler like a text adventure. You get to learn most of the elements of Java structure and game design without all of the 3D graphics, threading, etc. complexity. A decent Java programmer could churn one out over a weekend... which is a pretty good gauge of skill level achieved.