Solved

KeyEvent doesn' work ....

Posted on 2004-10-03
1
255 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 92

Accepted Solution

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

add the following to your ctor:

addKeyListener(this);
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.
Suggested Courses

630 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