Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (601)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (649)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Using KeyListener to modify a transform  (Read 1028 times)
0 Members and 1 Guest are viewing this topic.
Offline stroh

Senior Newbie

Java games rock!

« Posted 2004-12-03 05:25:17 »

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 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 since it is too long to post in this forum.

Much thanks in advance!
Offline Trond_A

Senior Newbie

Java games rock!

« Reply #1 - Posted 2004-12-04 09:33:20 »

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):



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();

This should be sufficient to transform an object.

Offline stroh

Senior Newbie

Java games rock!

« Reply #2 - Posted 2004-12-09 00:56:12 »

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


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!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline stroh

Senior Newbie

Java games rock!

« Reply #3 - Posted 2004-12-10 00:37:53 »

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

TransformGroup userRot = new TransformGroup();

public void keyPressed( KeyEvent evt ){

   Transform3D xRott3d = new Transform3D();
   Transform3D yRott3d = new Transform3D();
   Transform3D zRott3d = new Transform3D();
         case KeyEvent.VK_UP   :   System.out.println("Up");
         case KeyEvent.VK_DOWN :   System.out.println("Down");

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(
     at java.awt.Component.processKeyEvent(
     at java.awt.Component.processEvent(
     at java.awt.Component.dispatchEventImpl(
     at java.awt.Component.dispatchEvent(
     at java.awt.KeyboardFocusManager.redispatchEvent(
     at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(
     at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(
     at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(
     at java.awt.DefaultKeyboardFocusManager.dispatchEvent(
     at java.awt.Component.dispatchEventImpl(
     at java.awt.Component.dispatchEvent(
     at java.awt.EventQueue.dispatchEvent(
     at java.awt.EventDispatchThread.pumpOneEventForHierarchy(
     at java.awt.EventDispatchThread.pumpEventsForHierarchy(
     at java.awt.EventDispatchThread.pumpEvents(
     at java.awt.EventDispatchThread.pumpEvents(

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

Thanks in advance!
Offline stroh

Senior Newbie

Java games rock!

« Reply #4 - Posted 2004-12-10 23:54:27 »

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.
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Jesse (12 views)
2015-07-29 04:35:27

Riven (33 views)
2015-07-27 16:38:00

Riven (18 views)
2015-07-27 15:35:20

Riven (20 views)
2015-07-27 12:26:13

Riven (11 views)
2015-07-27 12:23:39

BurntPizza (30 views)
2015-07-25 00:14:37

BurntPizza (40 views)
2015-07-24 22:06:39

BurntPizza (24 views)
2015-07-24 06:06:53

NoxInc (27 views)
2015-07-22 22:16:53

NoxInc (18 views)
2015-07-22 22:13:39
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!