Solved

KeyEvent doesn' work ....

Posted on 2004-10-03
1
215 Views
Last Modified: 2010-05-18
Hi experts,

   Does anyone know how come my keyevent doesn't work ? many thanks !!!

meow  
-----------------

import javax.swing.*;
import java.awt.* ;
import java.awt.event.* ;

public class Test extends JFrame implements KeyListener, ActionListener{
   
    private int snake_x[] = new int[150] ;
    private int snake_y[] = new int[150] ;
    private int snake_dir[] = new int[150] ;
    private final int Delta_x = 20, Delta_y = 20 ;
   
    private static final int UP = 0 ;
    private static final int RIGHT = 1 ;
    private static final int DOWN = 2 ;
    private static final int LEFT = 3 ;
   
   
     
    private javax.swing.Timer headTimer ;
   
    ImageIcon headImg = new ImageIcon(getClass().getResource("head.gif"));
    /** Creates a new instance of SnakeFrame */
 
    public Test() {
        snake_x[0] = 200 ; snake_y[0] = 200 ; snake_dir[0] = 3 ;
        headTimer = new Timer(400, this);
        headTimer.start();
       
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        setSize(600,600) ;
        show() ;
       
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Test mySnake = new Test() ;
        // TODO code application logic here
    }
   
 
   
    public void paint(Graphics g)
    {
        super.paint(g);
        g.drawImage( headImg.getImage(), snake_x[0], snake_y[0], 20, 20, null) ;  
     }

    public void actionPerformed(ActionEvent e) {
         if ( snake_dir[0] == 0) snake_y[0] -= Delta_y ;
         if ( snake_dir[0] == 1) snake_x[0] += Delta_x ;
         if ( snake_dir[0] == 2) snake_y[0] += Delta_y ;
         if ( snake_dir[0] == 3) snake_x[0] -= Delta_x ;
         repaint() ;
    }
   
    public void keyPressed(KeyEvent e) {
     
           
           if (e.getKeyCode()==KeyEvent.VK_UP) {
             snake_dir[0] = 0 ;
             System.out.println("UP") ;
           }
           else if (e.getKeyCode()==KeyEvent.VK_RIGHT) {
             snake_dir[0] = 1 ;
             System.out.println("RIGHT") ;
           }
           else if (e.getKeyCode()==KeyEvent.VK_DOWN) {
             snake_dir[0] = 2 ;
             System.out.println("DOWN") ;
           }
           else if (e.getKeyCode()==KeyEvent.VK_LEFT) {
             snake_dir[0] = 3 ;
             System.out.println("LEFT") ;
           }      
         
    }
   
    public void keyReleased(KeyEvent e) {
    }
   
    public void keyTyped(KeyEvent e) {
    }
   
}

0
Comment
Question by:meow00
1 Comment
 
LVL 92

Accepted Solution

by:
objects earned 450 total points
Comment Utility
you don't add the frame as a key listener

add the following to your ctor:

addKeyListener(this);
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
commonTwo  challenge 63 98
bunnyEars2 challenge 6 66
network + 7 72
Starting to learn JAVA, 7 46
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now