Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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 924 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 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
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):

1  
2  
3  
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:

1  
2  
3  
4  
5  
6  
7  
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
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:

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

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (33 views)
2014-12-15 09:26:44

Mr.CodeIt (24 views)
2014-12-14 19:50:38

BurntPizza (51 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (46 views)
2014-12-05 12:39:02

SHC (60 views)
2014-12-03 16:27:13

CopyableCougar4 (62 views)
2014-11-29 21:32:03

toopeicgaming1999 (124 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org 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‑gaming.org
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!