Adding a KeyListener to SphereMotion.java

Hello. I am currently trying to take Sun’s SphereMotion.java file and work it into a black hole simulator for a college project. I am trying to setup my basic geometry and am currently trying to add a KeyListener whereby I can use keyboard commands (the arrows in particular) to rotate the view around my ellipsoids. Also note that I am a newbie.

The KeyListener is added to my canvas as it is the only think I could find that would allow a KeyListener to be added to. I then read online that it would be good to create a switch to tell the program when to process the rotations however I can’t get it to work. It simply doesn’t do anything.

Thus far I have tried adding my transforms to the BranchGroup createSceneGraph but that didn’t work. Recently I tried placing the rotation transforms in their own BranchGroup and adding the other transforms for the ellispoids as a child under that BranchGroup. But this also has not worked. I’m fairly certain that my KeyListener is working.

Does anybody have any suggestions on the proper way to pull this off?

I have posted my code at http://www.astroh.org/SphereMotion.java since it is too long to post in this forum.

Much thanks in advance!
Michael

Where you have:

c.addKeyListener( this );

try:

addKeyListener( this );

No, that doesn’t help.

The KeyListener works and does read my keys but it is the link from my keys changing my rotation variables and the transforms is where I’m having problems.

You can pretty much skip the first branchgroup in my code. That just sets up my basic geometry. I have my transforms in that portion where you found the c.addKeyListener(this) and for some reason they aren’t working.

Thanks for the suggestion though, however, without that “c.” portion my KeyListener doesn’t even work.

I couldn’t get it to run without changing 2 lines. I had to add a call to getContentPane(), otherwise it threw an exception. Lines 437 and 456.

What should you see when things are working correctly?

Dr. A>

Try downloading the file again. It should work without those calls (at least it seems to for me using netBeans). If you do need those calls could you specify how you added those to the lines?

Once it runs it should show a couple of nested ellipsoids with a smaller sphere in the center of them and another larger sphere outside of them. I also have a JPanel added to the bottom of the applet but haven’t had time to work on it yet. I want to first get this keyboard command interface working before moving on to that.

Thanks!