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

Thread: keyListener not working

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Location
    Middle Georgia
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default keyListener not working

    Just for fun I am trying to make my own version of the old game Crazy Taxi. I am only to the point of having the character however. I am at the stage of trying to make sure the character (goku icon) can move between the three lanes. I have it set up so that when the user presses the right or left arrow keys, the icon will jump from one lane to another, however even though my code was error free, the icon did not move. So I added a label that allowed me to keep track of the x-coordinate of the icon to see if that was even changing when I pressed one of the arrow keys. It does not. Is there an error in my code that is keeping the keyListener from working? Here is my code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class ThreeLanesPanel extends JPanel
    {
    	private final int WIDTH = 400, HEIGHT = 400;
    	private final int DELAY = 100, GOKU_SIZE = 45;
     
    	private ImageIcon goku;
    	private Timer timer;
    	private int gokux, gokuy, moveX, moveY, testing;
    	private JLabel label;
     
    	public ThreeLanesPanel()
    	{
    		timer = new Timer(DELAY, new ThreeLanesListener());
     
    		addKeyListener(new GokuListener());
     
    		label = new JLabel("Goku's X coordinate: " + gokux);
    		goku = new ImageIcon("Goku.gif");
     
    		gokux = 40;
    		gokuy = 355;
    		moveX = moveY = 3;
     
    		add(label);
    		setPreferredSize(new Dimension(WIDTH,HEIGHT));
    		setBackground(Color.white);
    		timer.start();
    	}
     
    	public void paintComponent(Graphics page)
    	{
    		super.paintComponent(page);
    		goku.paintIcon(this,page,gokux,gokuy);
    		page.drawLine(133,0,133,400);
    		page.drawLine(266,0,266,400);
    	}
     
    	private class ThreeLanesListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
     
    		}
    	}
     
    	private class GokuListener implements KeyListener
    	{
    		public void keyPressed (KeyEvent event)
    		{
    			switch (event.getKeyCode())
    			{
    				case KeyEvent.VK_RIGHT:
    					gokux += 130;
    					testing = 100;
    					break;
    				case KeyEvent.VK_LEFT:
    					gokux -= 130;
    					testing = 150;
    					break;
    			}
    			repaint();
    			label.setText("Test number: " + testing);
    		}
     
    		public void keyTyped(KeyEvent event){}
    		public void keyReleased(KeyEvent event) {}
    	}
    }
    And here is the main method that runs the applet:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class ThreeLanes
    {
    	public static void main(String[] args)
    	{
    		JFrame frame = new JFrame("ThreeLanes");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		frame.getContentPane().add(new ThreeLanesPanel());
     
    		frame.pack();
    		frame.setVisible(true);
    	}
    }


  2. #2
    Junior Member
    Join Date
    Feb 2012
    Location
    Middle Georgia
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: keyListener not working

    also here is the icon if anyone wants to run the program for themselves:

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Location
    Middle Georgia
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: keyListener not working

    Nevermind. I just realized I forgot to include the setFocusable(true) command in the constructor. Sorry I didn't notice before.

Similar Threads

  1. KeyListener bug
    By nivangerow in forum AWT / Java Swing
    Replies: 1
    Last Post: September 5th, 2011, 07:10 AM
  2. [SOLVED] KeyListener won't listen
    By Fermen in forum AWT / Java Swing
    Replies: 8
    Last Post: June 22nd, 2011, 09:11 PM
  3. help with KeyListener
    By all_pro in forum AWT / Java Swing
    Replies: 4
    Last Post: April 14th, 2011, 07:51 AM
  4. My KeyListener is not Working!!
    By DarrenReeder in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2010, 05:18 PM
  5. Problem with KeyListener
    By r12ki in forum AWT / Java Swing
    Replies: 3
    Last Post: October 1st, 2009, 02:18 PM