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

  1. #1

    Default KeyListener bug

    Because of some odd reason, my key listener doesn't work. If you can help me that would be great.
    Here is my eclipse workspace: Pong_WORKSPACE.zip
    and here are all the class files on their own.

    Pong.java (main class)
    package net.nivangerow.pong;
    import javax.swing.*;
    import java.awt.*;
    public class Pong {
    	public static void main(String[] args)
    	{
    		JFrame frame = new JFrame("Pong by nivangerow");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    		frame.setSize(640, 480);
    		frame.add(new Game());
    	}
    }

    Game.java
    package net.nivangerow.pong;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Game extends JPanel implements ActionListener{
    	Timer time;
    	Image ball;
    	public Bat b3;
    	public Game()
    	{
    		b3=new Bat();
    		setVisible(true);
    		setSize(640, 480);
    		setBackground(Color.BLACK);
    		time = new Timer(5, this);
    		time.start();
    		ImageIcon b2 = new ImageIcon(getClass().getResource("ball.png"));
    		ball = b2.getImage();
    		addKeyListener(new AL());
    	}
    	public void actionPerformed(ActionEvent e)
    	{
    		//System.out.println(b3.y+" "+b3.py);
    		b3.move();
    		repaint();
    	}
    	public void paint(Graphics g)
    	{
    		super.paint(g);
    		Graphics2D g2d = (Graphics2D)g;
    		g2d.drawImage(b3.bat, 0, b3.y, null);
    	}
    	public class AL extends KeyAdapter
    	{
    		public void keyPressed(KeyEvent e)
    		{
    			int key = e.getKeyCode();
    			if(key==KeyEvent.VK_W)
    			{
    				b3.py=-1;
    			}
    		}
    		public void keyReleased(KeyEvent e)
    		{
    			int key = e.getKeyCode();
    			if(key==KeyEvent.VK_W)
    			{
    				b3.py=0;
    			}
    		}
    	}
    }

    Bat.java
    package net.nivangerow.pong;
    import java.awt.*;
    import javax.swing.ImageIcon;
    public class Bat {
    	public int y=180;
    	public int py=0;
    	Image bat;
    	public Bat()
    	{
    		ImageIcon b1 = new ImageIcon(getClass().getResource("bat.png"));
    		bat = b1.getImage();
    	}
    	public void move()
    	{
    		//System.out.println("MOVING");
    		y = y + py;
    	}
    }
    Last edited by nivangerow; September 5th, 2011 at 07:10 AM.


  2. #2

    Default Re: KeyListener bug

    I figured it out.

Similar Threads

  1. Help with KeyListener and graphics
    By 123099 in forum AWT / Java Swing
    Replies: 21
    Last Post: July 18th, 2011, 02:26 PM
  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

Tags for this Thread