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 6 of 6

Thread: Keyboard imput not working!

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Keyboard imput not working!

    My Keyboard imput doesn't seem to work and i don't know why. The program is a little lengthy and I'm sorry about that. I'd really appreciate an answer.

    -Main-
    /* This code was produced by Kevin Spaeth.
     * Please do not use without permission.
     * Copyright May, 2011. Sp333th Productions.
     */
     
    import java.applet.*;
    import java.awt.*;
     
    public class Main extends Applet implements Runnable {
     
    	//Keys
    	private boolean keyLeft;
    	private boolean keyRight;
    	private boolean keySpace;
    	//Images
    	private Image Greenman1;
    	private Image Greenman2;
    	private Image Greenman3;
    	private Image Greenman4;
    	private Image Greenman5;
    	//Thread used for the game.
    	private Thread gameThread;
    	//Used for double-buffering.
    	private Image dbImage;
    	private Graphics dbGraphics;
    	//Greenman enters!
    	private Player Greenman;
     
    	public void init() {
     
    		//Sets the applet size.
    		setSize(Constants.applet_width, Constants.applet_height);
    		//Sets the name of the applet.
    		setName(Constants.applet_name);
    		//Gets the player images.
    		getImages();
    		//Initializes the player.
    		Greenman = new Player(300,300,this);
     
    		//Sets keys to be false initially.
    		keyLeft = false;
    		keyRight = false;
    		keySpace = false;
     
    		//Sends images to the player class.
    		Greenman.setImages(Greenman1, Greenman2, Greenman3, Greenman4, Greenman5);
    	}
     
    	public void start() {
     
    		//Create a new thread.
    		gameThread = new Thread(this);
    		//Start thread.
    		gameThread.start();
    	}
     
    	public void run() {
     
    		//GreenMan is REAL important!
    		Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
     
    		//Loops the run part of the string.
    		while(true) {
     
    			//Helps to keep the applet from being resized.
    			setSize(Constants.applet_width, Constants.applet_height);
     
    			Greenman.playerMove();
     
    			//Repaints the graphics.
    			repaint();
     
    			try {
    				//Sleep for the game frame rate.
    				Thread.sleep((1000/Constants.applet_framerate));
    			}
    			catch(InterruptedException gameException) {
    				System.out.println("Error occourred inside of the run loop.");
    				//Don't do anything.
    			}
    			//Ensures that this thread still has the max priority.
    			Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    		}
    	}
     
    	public void stop() {
     
    		//Handles stopping the thread.
    		gameThread.stop();
    	}
     
    	public void destroy() {
     
    		//Handles stopping the thread.
    		gameThread.stop();
    	}
     
    	public void paint (Graphics gameGraphics) {
     
    		Greenman.paintPlayer(gameGraphics);
    		gameGraphics.drawImage(Greenman1, 200, 200, this);
    	}
     
    	public void update (Graphics gameGraphics) {
     
    		//This code is used to help double-buffer the enviornment.
    		if (dbImage == null)
    		{
    			dbImage = createImage (this.getSize().width, this.getSize().height);
    			dbGraphics = dbImage.getGraphics ();
    		}
     
    		dbGraphics.setColor (getBackground ());
    		dbGraphics.fillRect (0, 0, this.getSize().width, this.getSize().height);
     
    		dbGraphics.setColor (getForeground());
     
    		paint(dbGraphics);
     
    		gameGraphics.drawImage (dbImage, 0, 0, this);
    	}
     
    	private void getImages() {
     
    		//Mediatracker picks up images.
    		MediaTracker tracker = new MediaTracker(this);
     
    		Greenman1 = getImage(getCodeBase(), "Sprites/GreenmanRun-01.png");
    		tracker.addImage(Greenman1, 1);
    		Greenman1 = getImage(getCodeBase(), "Sprites/GreenmanRun-02.png");
    		tracker.addImage(Greenman2, 2);
    		Greenman1 = getImage(getCodeBase(), "Sprites/GreenmanRun-03.png");
    		tracker.addImage(Greenman3, 3);
    		Greenman1 = getImage(getCodeBase(), "Sprites/GreenmanRun-04.png");
    		tracker.addImage(Greenman4, 4);
    		Greenman1 = getImage(getCodeBase(), "Sprites/GreenmanRun-05.png");
    		tracker.addImage(Greenman5, 5);
     
    		//Makes sure that all images are properly loaded.
    		try {
    			tracker.waitForAll();
    		}
    		catch (Exception exception) {}
    	}
     
    	//Handles variables when a key is presed.
    	public boolean keyDown (Event gameEvent, int key)
    	{
     
    		if(key == Event.LEFT)
    		{
    			keyLeft = true;
    			Greenman.playerWalkLeft(true);
    		}
    		else if (key == Event.RIGHT)
    		{
    			keyRight = true;
    			Greenman.playerWalkRight(true);
    		}
    		else if (key == 97)
    		{
    			keySpace = true;
     
    			Greenman.playerJump(true);
    		}
    		System.out.println ("Charakter: " + (char)key + " Integer Value: " + key); 
    		return true;
    	}
     
     
    }

    -Player-
    /* This code was produced by Kevin Spaeth.
     * Please do not use without permission.
     * Copyright May, 2011. Sp333th Productions.
     */
     
    import javax.swing.ImageIcon;
    import java.awt.*;
     
    public class Player { 
     
    		//Counters
    		private int countPicture;
    		private int countStep;
    		private int countJump;
    		//Determines Directions.
    		private boolean lookLeft;
    		private boolean walkLeft;
    		private boolean walkRight = true;
    		//Determines jumping actions.
    		private boolean jumping;
    		private boolean falling;
    		private boolean ableToJump;
    		//Provides location of all angles of a sprite.
    		private int xPosLeft;
    		private int xPosRight;
    		private int yPosUp;
    		private int yPosDown;
    		private int gamexPos;
    		//Images
    		private Image Greenman1;
    		private Image Greenman2;
    		private Image Greenman3;
    		private Image Greenman4;
    		private Image Greenman5;
    		//What is this used for?!
    		private Component parent;
     
    		public Player(int x, int y, Component parent) {
     
    			xPosLeft = x;
    			yPosUp = y;
    			xPosRight = Constants.xSize+xPosLeft;
    			yPosDown = Constants.ySize+yPosUp;
    			gamexPos = x+Constants.xSize/2;
     
    			countPicture = 0;
    			countStep = 0;
    			countJump = 0;
     
    			ableToJump = true;
    			lookLeft = false;
     
    			this.parent = parent;
    		}
     
    		public void setImages(Image Greenman1, Image Greenman2, Image Greenman3, Image Greenman4, Image Greenman5) {
     
    			//Fetches images from the Mediatracker in the Main class.
    			this.Greenman1 = Greenman1;
    			this.Greenman2 = Greenman2;
    			this.Greenman3 = Greenman3;
    			this.Greenman4 = Greenman4;
    			this.Greenman5 = Greenman5;
    		}
     
    		//Determines if the player is moving left.
    		public void playerWalkLeft(boolean value)
    		{
    			walkLeft = value;
    		}
     
    		//Determines if the player is moving right.
    		public void playerWalkRight(boolean value)
    		{
    			walkRight = value;
    		}
     
    		//Determines if the player is jumping.
    		public void playerJump(boolean value)
    		{
    			//Reset the jump counter.
    			if(!jumping && ableToJump && value)
    			{
    				countJump = 0;
    			}
     
    			//Sets the value of jumping. (Can't jump if falling.)
    			if(falling)
    			{
    				jumping = false;
    			}
    			else
    			{
    				jumping = value;
    			}
    		}
     
    		public void playerMove() {
     
    			if(walkLeft) {
     
    				xPosLeft -= Constants.x_speed;
    				xPosRight -= Constants.x_speed;
    				gamexPos -= Constants.x_speed;
    			}
    		}
    		public void paintPlayer(Graphics gameGraphics) {
     
    			gameGraphics.drawImage(Greenman1, xPosLeft, yPosUp, parent);
    		}
    }

    -Constants-
    /* This code was produced by Kevin Spaeth.
     * Please do not use without permission.
     * Copyright May, 2011. Sp333th Productions.
     */
     
    public class Constants {
     
    	//Name of the Applet.
    	public static final String applet_name = "GreenMan Rev. 1.01";
    	//Dimensions of the game.
    	public static final int applet_width = 800;
    	public static final int applet_height = 600;
    	//Sets the framerate.
    	public static final int applet_framerate = 30;
    	//Variables for player speed.
    	public final static int x_speed = 3;
    	public final static int y_speed1 = 1;
    	public final static int y_speed2 = 2;
    	public final static int y_speed3 = 4;
    	public final static int y_speed4 = 8;
    	//Keys to be pressed.
    	public boolean keyDown;
    	public boolean keyUp;
    	public boolean keyLeft;
    	public boolean keyRight;
    	public boolean keySpace;
    	//Size of Greenman (while running.)
    	public final static int xSize = 75;
    	public final static int ySize = 200;
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,199
    Thanks
    49
    Thanked 2,174 Times in 2,147 Posts

    Default Re: Keyboard imput not working!

    Please explain what "not working" means?
    Do you get errors or what happens?

    Can you make a small, simple program that demonstrates your problem?
    Last edited by Norm; May 15th, 2011 at 07:51 AM.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Skyhigh32 (May 15th, 2011)

  4. #3
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Keyboard imput not working!

    How are you reading from the keyboard? On first glance, I don't see any keyboard listeners...

    ETA - oh wait, you're using keyDown(..)? the deprecated method replaced in JDK 1.1 ?

    That's not the way to do it, anyway. Have a read of How To Write A Key Listener.
    Last edited by dlorde; May 15th, 2011 at 12:52 PM.

  5. The Following User Says Thank You to dlorde For This Useful Post:

    Skyhigh32 (May 15th, 2011)

  6. #4
    Junior Member
    Join Date
    May 2011
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Keyboard imput not working!

    Thank you to both for the timely response. I appreciate the help. To elaborate on the fact that it is "not working", I simply mean that my program is drawing where it needs to, and there are no run-time errors, it's simply not generating the keyboard imput. I put a line of code in that is supposed to return the key code if it not one of the ones i defined in the booleans.

    I really don't know what deprecated means. I only have one school year of Java under my belt, and I'm trying to make this program for a simple project. I find that I'm getting lost within my own code as it becomes more lengthy, despite that I'm commenting nearly everything. If you have any suggestions on how to overcome that I'd really appreciate it.

    The code is borrowed from a game tutorial website, so that's probably why it doesn't work. But I'll take a read through the "how to Write a Key Listener" article, and then check back to let you all know if i need more help. Thanks for the response.

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,199
    Thanks
    49
    Thanked 2,174 Times in 2,147 Posts

    Default Re: Keyboard imput not working!

    it's simply not generating the keyboard imput.
    Keyboard input usually comes from the user. I'm not sure how your program would generate it.

    If its a question of the program seeing input, click on the applet to give it focus and then press some keys. It works for me using AppletViewer.

  8. #6
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Keyboard imput not working!

    Quote Originally Posted by Skyhigh32 View Post
    I really don't know what deprecated means.
    Easy to find out: deprecated.

    I find that I'm getting lost within my own code as it becomes more lengthy, despite that I'm commenting nearly everything. If you have any suggestions on how to overcome that I'd really appreciate it.
    I thought you said the code was 'borrowed from a game tutorial website' (how do you 'borrow' code?), which would explain why you're getting lost in it, but would also mean it isn't your code...

    If you're talking about your code in general, think about how to break the task(s) down into simpler elements. Write simple classes that have a single, clearly defined role, with simple methods that have a single, clearly defined function. Try to keep classes and methods short. Use clear and meaningful class, method, and variable names that say what they are or what they do. Don't be afraid to use long names and lots of classes. Try to make your code read well so you don't need so many comments. Ideally, comments should only be necessary where something complicated or unusual needs explaining.
    Last edited by dlorde; May 16th, 2011 at 08:49 AM.

Similar Threads

  1. How to connect keyboard through COM?
    By yogesh01 in forum Java Theory & Questions
    Replies: 0
    Last Post: July 6th, 2010, 05:00 AM
  2. Virtual Keyboard in Java
    By Brian in forum Java Theory & Questions
    Replies: 2
    Last Post: June 2nd, 2010, 11:35 PM
  3. Keyboard.class error
    By block g raptor in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 23rd, 2010, 01:37 AM
  4. how to know user pressed a key in the keyboard
    By cilang in forum File I/O & Other I/O Streams
    Replies: 16
    Last Post: September 11th, 2009, 10:08 AM
  5. Replies: 4
    Last Post: January 26th, 2009, 11:03 PM