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: implementing KeyListener

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default implementing KeyListener

    I'm trying to implement a key listener to a program I'm doing for school. I got a little help with it already elsewhere but I'm still having trouble getting it to work. I will include the two classes that should be of importance. I have two others that handle the images and variable data but I don't think they have any effect with the problem at hand.

    As you can see the listener is there and I did addKeyListener(this) in the constructer like I was told. When I press any buttons on the keyboard nothing happens. All I really need is to get it to respond to a key stroke and I think I can handle everything else in the program. Everything else is also currently working properly. Any help with what I'm doing wrong would be greatly appreciated. Thank you all very much for your time and have a good day.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.util.*;
    import java.util.Timer;
     
    public class ArcadeFighterPanel extends JPanel implements KeyListener
    {
    	//declare variables 
    	position xydata = new position();
    	int playerx = xydata.PlayerX(), playery = xydata.PlayerY();
    	int delay = 1000;   // delay for 1 sec.
    	int period = 1000;  // repeat every sec.
    	Timer timer = new Timer();
    	final int MAX_LEFT_X = 0, MAX_RIGHT_X = 250, MAX_Y = 171; 
    	BufferedImage loadImg = ImageControll.makeColorTransparent("fighter_idle.png", Color.WHITE); 
     
    	//set up the panel that displays
    	public ArcadeFighterPanel ()
    	{
    		/*JButton changeButton = new JButton ("Start Game");
    		changeButton.addActionListener(new ChangeListener()); 
    		add(changeButton); */
     
    		setBackground(Color.GRAY);
    		setPreferredSize(new Dimension(600,250));
    		addKeyListener(this);
    	}
     
    	//pant the screen with characters 
    	public void paintComponent (Graphics page)
    	{
    		super.paintComponent(page); 
    		page.setColor(Color.GREEN); 
    		page.fillRect(0, 225, 600, 25); 
     
    		page.drawImage(loadImg, playerx, playery, this);
     
    		timer.scheduleAtFixedRate(new TimerTask() 
    		{
    			public void run() 
    			{
    				playerx = xydata.PlayerX(); 
    				playery = xydata.PlayerY();
    				repaint();
    			}
    		}, delay, period);
     
    	}
     
    	@Override
    	public void keyPressed(KeyEvent e) 
    	{
    		System.out.println("Key pressed");
    	}
     
    	@Override
    	public void keyTyped(KeyEvent e) 
    	{
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void keyReleased(KeyEvent e) {
    		// TODO Auto-generated method stub
     
    	}
    }

    import javax.swing.*;
     
    public class arcadeFighter 
    {
    	private static JFrame frame; 
     
    	public static void main(String[] args) 
    	{
    		JFrame frame = new JFrame ("Awsome Arcade Fighter Extreem"); 
    		frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); 
    		frame.setResizable( false );
    		frame.getContentPane().add(new ArcadeFighterPanel());
    		frame.pack(); 
    		frame.setVisible(true); 
    	}
     
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: implementing KeyListener

    Does the JPanel have the focus?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Error on KeyListener
    By Colino in forum AWT / Java Swing
    Replies: 12
    Last Post: January 14th, 2012, 03:58 AM
  2. KeyListener bug
    By nivangerow in forum AWT / Java Swing
    Replies: 1
    Last Post: September 5th, 2011, 06:10 AM
  3. [SOLVED] KeyListener won't listen
    By Fermen in forum AWT / Java Swing
    Replies: 8
    Last Post: June 22nd, 2011, 08:11 PM
  4. help with KeyListener
    By all_pro in forum AWT / Java Swing
    Replies: 4
    Last Post: April 14th, 2011, 06:51 AM
  5. 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