Using KeyListener to modify a transform

Hello. I posted this under the Newbies category but haven’t gotten anywhere so I thought I’d post it here and see if I might be able to get some more assistance.

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

You need to apply the transformations to a transformgroup, and a transformgroup may contain geometry or it may have other transformgroups as children. If you want to apply a transform after a transformgroup is added to the branchgraph, you need to set write-capability for the transformgroup, something like this (before adding to branchgroup):


tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

bg.addChild(tg);

Then, assuming your ‘tg’ is a class variable, you can set transforms from the keylistener functions, something like this:


if (event.getKeyCode() == KeyEvent.VK_RETURN)
{
angle+= 0.1;
Transform3D t3d = new Transform3D();
t3d.rotZ(angle);
tg.setTransform(t3d);
}

This should be sufficient to transform an object.

-Trond

Thanks for the help but I now keep getting a NullPointerException when I run the program on the line:

tg.setTransform(t3d);

Any idea what my problem here could be? I’m running this in my keyPressed section whereas I am defining tg in a different section. My guess is that this isn’t working because I don’t have tg set up as a class variable as you have assumed in your reply. How do I do that?

Thanks again!

Here is some of the code I have changed in response to the help given above:

TransformGroup userRot = new TransformGroup();
userRot.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

public void keyPressed( KeyEvent evt ){

Transform3D xRott3d = new Transform3D();
Transform3D yRott3d = new Transform3D();
Transform3D zRott3d = new Transform3D();

  switch(evt.getKeyCode())
  {
      case KeyEvent.VK_UP   :   System.out.println("Up");
                                xRot+=0.1;
                                xRott3d.rotX(xRot);
                                userRot.setTransform(xRott3d);
                                break;
      case KeyEvent.VK_DOWN :   System.out.println("Down");
                                xRot-=0.1;
                                xRott3d.rotX(xRot);
                                userRot.setTransform(xRott3d);
                                break;

It compiles fine but when I try using one of my keys connected to a KeyEvent I get something like the following in reference to the userRot.setTransform lines in KeyPressed.

Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException
at SphereMotion.keyPressed(SphereMotion.java:294)
at java.awt.Component.processKeyEvent(Component.java:5446)
at java.awt.Component.processEvent(Component.java:5265)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1810)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:668)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:916)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:794)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:632)
at java.awt.Component.dispatchEventImpl(Component.java:3841)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:234)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

I’ve also updated my code at the address given in the first comment of this section incase that is useful.

Thanks in advance!
Michael

I figured out that NullPointerException problem and reworked some of the code, but the transforms still don’t make the rotations work. Any ideas? Sourcecode has been updated.