Results 1 to 3 of 3
  1. #1
    Eric is offline Senior Member
    Join Date
    Jun 2007
    Posts
    111
    Rep Power
    0

    Default Help with pong game

    Hi , I am trying to build a pong game. I just need to find some type of Keylistener that will help me know what the person typed. Like I know what they typed if I add a textbox, but I want them to type anywhere in my window and for my program to still be able to know what they did.

    Eric

  2. #2
    Marcus is offline Member
    Join Date
    Jun 2007
    Posts
    92
    Rep Power
    0

    Default

    There are several ways to approach this.

    Implement KeyListener:
    Java Code:
     import java.applet.Applet;
     
     import java.awt.event.KeyListener;
     import java.awt.event.KeyEvent;
     
     public class KeyListenerExample extends Applet implements KeyListener {
         public void init() {
             addKeyListener(this);
         }
         
         public void keyPressed(KeyEvent e) {
             System.out.println("keyPressed: " + e.getKeyChar());
         }
         
         public void keyTyped(KeyEvent e) {
             System.out.println("keyTyped: " + e.getKeyChar());
         }
         
         public void keyReleased(KeyEvent e) {
             System.out.println("keyReleased: " + e.getKeyChar());
         }
     }
    Create an inner private class that extends a class that implements KeyListener, or that implements KeyListener itself:
    Java Code:
    import java.applet.Applet;
     
     import java.awt.event.KeyAdapter;
     import java.awt.event.KeyEvent;
     
     public class KeyListenerExample extends Applet {
         public void init() {
             addKeyListener(new MyKeyListener());
         }
         
         private class MyKeyListener extends KeyAdapter {
             public void keyPressed(KeyEvent e) {
                 System.out.println("keyPressed: " + e.getKeyChar());
             }
         
             public void keyTyped(KeyEvent e) {
                 System.out.println("keyTyped: " + e.getKeyChar());
             }
         
             public void keyReleased(KeyEvent e) {
                 System.out.println("keyReleased: " + e.getKeyChar());
             }
         }
     }
    Create an anonymous inner class:
    Java Code:
    import java.applet.Applet;
     
     import java.awt.event.KeyListener;
     import java.awt.event.KeyEvent;
     
     public class KeyListenerExample extends Applet {
         public void init() {
             addKeyListener(new KeyListener() {
                 public void keyPressed(KeyEvent e) {
                     System.out.println("keyPressed: " + e.getKeyChar());
                 }
                 
                 public void keyTyped(KeyEvent e) {
                     System.out.println("keyTyped: " + e.getKeyChar());
                 }
                 
                 public void keyReleased(KeyEvent e) {
                     System.out.println("keyReleased: " + e.getKeyChar());
                 }
             });
         }
     }
    Greetings.

    Marcus:cool:

  3. #3
    Felissa is offline Member
    Join Date
    Jun 2007
    Posts
    95
    Rep Power
    0

    Default

    If you look at the JFrame API, you'll notice that it inherits from Component, which provides the addKeyListener(KeyListener) method.
    JFrame
    Java Code:
    import java.awt.Container;
     import java.awt.Dimension;
     import java.awt.event.KeyListener;
     import java.awt.event.KeyEvent;
     
     import javax.swing.JFrame;
     import javax.swing.JPanel;
     
     public class KeyListenerExample extends JFrame {
         public KeyListenerExample() {
             super ("KeyListener Example");
             
             JPanel panel = (JPanel) getContentPane();
             panel.setPreferredSize(new Dimension(800, 600));
             panel.setLayout(null);
             
             pack();
             setResizable(false);
             setVisible(true);
     
             addKeyListener(new KeyListener() {
                 public void keyPressed(KeyEvent e) {
                     System.out.println("keyPressed: " + e.getKeyChar());
                 }
                 
                 public void keyTyped(KeyEvent e) {
                     System.out.println("keyTyped: " + e.getKeyChar());
                 }
                 
                 public void keyReleased(KeyEvent e) {
                     System.out.println("keyReleased: " + e.getKeyChar());
                 }
             });
          }
         
         static public void main(String[] args) {
              new KeyListenerExample();
          }
     }
    Greetings.

    Felissa:p

Similar Threads

  1. Ping pong game
    By adam405 in forum New To Java
    Replies: 8
    Last Post: 10-20-2010, 04:52 PM
  2. Replies: 2
    Last Post: 08-05-2010, 05:49 AM
  3. TicTacToe Game
    By Ebtihal in forum New To Java
    Replies: 0
    Last Post: 01-09-2008, 12:01 PM
  4. Programming a Game?
    By gt123 in forum New To Java
    Replies: 4
    Last Post: 01-01-2008, 01:41 PM
  5. Need help with random game!
    By silverq_82 in forum New To Java
    Replies: 4
    Last Post: 08-07-2007, 03:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •