Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: keyboard listener not working

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default keyboard listener not working

    I am using jPCT as a 3d engine and I cannot get any form of keyboard listeners to work. I have tried multiple different methods, and have gotten a variety of errors. If anybody can tell me how to implement a keyboard listener, I would really appreciate it. This is the most recent version of the program that compiles without error.

    import com.threed.jpct.*;
    import javax.swing.*;
    import java.io.*;
     
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
     
    import com.threed.jpct.*;
    import com.threed.jpct.util.*;
     
    /**
     * A simple HelloWorld using the Software-renderer and rendering into a frame
     * using active rendering.
     * @author EgonOlsen
     *
     */
    public class gun {
     
    	private World world;
    	private FrameBuffer buffer;
    	private JFrame frame;
    	private boolean kUP = false;
    	private boolean kDOWN = false;
    	private boolean kLEFT = false;
    	private boolean kRIGHT = false;
    	Object3D rifle;
    	Camera cam;
     
    	public static void main(String[] args) throws Exception {
    		new gun().loop();
    	}
     
    	public gun() throws Exception {
     
    		frame=new JFrame("Hello world");
    		frame.setSize(800, 600);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    		world = new World();
    //		world.setAmbientLight(255, 255, 255);
     
    		TextureManager.getInstance().addTexture("shoe1", new Texture("shoe1.jpg"));
     
     
    		Object3D[] miss=Loader.load3DS("rifle.3ds", 4);
            rifle = miss[0];
    		rifle.rotateX((float)-1.46);
    		rifle.setEnvmapped(Object3D.ENVMAP_ENABLED);
    		rifle.build();
    		world.addObject(rifle);
     
     
    		world.getCamera().setPosition(-60, -25, -5);
    		world.getCamera().lookAt(rifle.getTransformedCenter());
    		cam = world.getCamera();
    		world.addLight(new SimpleVector(60, -70, -20), 65, 23,8);
    		rifle.translate(new SimpleVector(0, 0, -10));
    	}
     
    	private void loop() throws Exception {
    		buffer = new FrameBuffer(800, 600, FrameBuffer.SAMPLINGMODE_NORMAL);
    		while (frame.isShowing()) {
     
    //			rifle.rotateY((float).005);
    //			cam.rotateCameraX((float).009);
    			cam.moveCamera(new SimpleVector(2, .5, 0), (float).1);
    			cam.lookAt(rifle.getTransformedCenter());
     
     
    			buffer.clear(java.awt.Color.BLUE);
    			world.renderScene(buffer);
    			world.draw(buffer);
    			buffer.update();
    			buffer.display(frame.getGraphics());
    			Thread.sleep(10);
    		}
    		buffer.disableRenderer(IRenderer.RENDERER_OPENGL);
    		buffer.dispose();
    		frame.dispose();
    		System.exit(0);
     
    	}
     
       }


  2. #2
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: keyboard listener not working

    Never mind, I got my code to work, here is the updated version:

    import com.threed.jpct.*;
    import javax.swing.*;
    import java.io.*;
     
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
     
    import com.threed.jpct.*;
    import com.threed.jpct.util.*;
     
    /**
     * A simple HelloWorld using the Software-renderer and rendering into a frame
     * using active rendering.
     * @author EgonOlsen
     *
     */
    public class gun {
     
    	private World world;
    	private FrameBuffer buffer;
    	private JFrame frame;
    	private boolean kUP = false;
    	private boolean kDOWN = false;
    	private boolean kLEFT = false;
    	private boolean kRIGHT = false;
    	KeyMapper keymap;
    	Object3D rifle;
    	Camera cam;
     
    	public static void main(String[] args) throws Exception {
    		new gun().loop();
    	}
     
    	public gun() throws Exception {
     
    		frame=new JFrame("Hello world");
    		frame.setSize(800, 600);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    		world = new World();
    //		frame.addWindowListener(new WindowEvents());
    		keymap = new KeyMapper(frame);
    //		world.setAmbientLight(255, 255, 255);
     
    		TextureManager.getInstance().addTexture("shoe1", new Texture("shoe1.jpg"));
     
     
    		Object3D[] miss=Loader.load3DS("rifle.3ds", 4);
            rifle = miss[0];
    		rifle.rotateX((float)-1.46);
    		rifle.setEnvmapped(Object3D.ENVMAP_ENABLED);
    		rifle.build();
    		world.addObject(rifle);
     
     
    		world.getCamera().setPosition(-60, -25, -5);
    		world.getCamera().lookAt(rifle.getTransformedCenter());
    		cam = world.getCamera();
    		world.addLight(new SimpleVector(60, -70, -20), 65, 23,8);
    		rifle.translate(new SimpleVector(0, 0, -10));
    	}
     
    	private void loop() throws Exception {
    		buffer = new FrameBuffer(800, 600, FrameBuffer.SAMPLINGMODE_NORMAL);
    		while (frame.isShowing()) {
     
    //			rifle.rotateY((float).015);
    //			cam.rotateCameraX((float).009);
    //			cam.moveCamera(new SimpleVector(2, .5, 0), (float).1);
    //			cam.lookAt(rifle.getTransformedCenter());
     
     
    			buffer.clear(java.awt.Color.BLUE);
    			world.renderScene(buffer);
    			world.draw(buffer);
    			buffer.update();
    			buffer.display(frame.getGraphics());
    			Thread.sleep(10);
    			poll();
    		}
    		buffer.disableRenderer(IRenderer.RENDERER_OPENGL);
    		buffer.dispose();
    		frame.dispose();
    		System.exit(0);
     
    	}
     
    	private void poll() {
          KeyState state=null;
          do {
             state=keymap.poll();
             if (state!=KeyState.NONE) {
                keyAffected(state);
             }
          } while (state!=KeyState.NONE);
       }
     
     
       private void keyAffected(KeyState state) {
          int code=state.getKeyCode();
          boolean event=state.getState();
    	  System.out.println("test");
          switch (code) {
             case (KeyEvent.VK_LEFT): {
                kLEFT=event;
                break;
             }
    		 case (KeyEvent.VK_UP): {
                kUP=event;
                break;
             }
             case (KeyEvent.VK_DOWN): {
                kDOWN=event;
                break;
             }
             case (KeyEvent.VK_RIGHT): {
                kRIGHT=event;
                break;
             }
          }
       }
     
       }


    NOTE: I realize my previous post wasn't exactly following forum etiquette , but I was rather frustrated at the time.

Similar Threads

  1. Keyboard imput not working!
    By Skyhigh32 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 16th, 2011, 08:47 AM
  2. How to connect keyboard through COM?
    By yogesh01 in forum Java Theory & Questions
    Replies: 0
    Last Post: July 6th, 2010, 05:00 AM
  3. Key Listener not working in jinternalframe
    By furqankhan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 12th, 2010, 09:48 AM
  4. Key listener help
    By airsim15 in forum AWT / Java Swing
    Replies: 2
    Last Post: December 13th, 2009, 03:15 PM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM

Tags for this Thread