KeyListener Example
/*
*its the first class
*save is as Car.java
/*
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
public class Car {
static int CARWIDTH = 40;
static int CARLENGTH = 70;
int x = Game.WIDTH/2 - CARWIDTH/2;
int y = Game.HEIGHT - 105;
int dx = 0;
int dy = 0;
private Game game;
public Car(Game game){
this.game = game;
}
public void paint(Graphics2D g2d){
g2d.setColor(Color.blue);
g2d.fillRect(x, y, CARWIDTH, CARLENGTH);
}
public void move(){
if(x+dx > 0 && x+dx < Game.WIDTH-56) x = x+dx;
if(y+dy > 0 && y+dy < Game.HEIGHT-105) y = y+dy;
}
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_LEFT) dx = -1;
if(e.getKeyCode() == KeyEvent.VK_RIGHT) dx = 1;
if(e.getKeyCode() == KeyEvent.VK_UP) dy = -1;
if(e.getKeyCode() == KeyEvent.VK_DOWN) dy = 1;
}
public void keyReleased(){
dx = 0;
dy = 0;
}
}
//////////////EnD of First Class////////////////import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
public class Car {
static int CARWIDTH = 40;
static int CARLENGTH = 70;
int x = Game.WIDTH/2 - CARWIDTH/2;
int y = Game.HEIGHT - 105;
int dx = 0;
int dy = 0;
private Game game;
public Car(Game game){
this.game = game;
}
public void paint(Graphics2D g2d){
g2d.setColor(Color.blue);
g2d.fillRect(x, y, CARWIDTH, CARLENGTH);
}
public void move(){
if(x+dx > 0 && x+dx < Game.WIDTH-56) x = x+dx;
if(y+dy > 0 && y+dy < Game.HEIGHT-105) y = y+dy;
}
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_LEFT) dx = -1;
if(e.getKeyCode() == KeyEvent.VK_RIGHT) dx = 1;
if(e.getKeyCode() == KeyEvent.VK_UP) dy = -1;
if(e.getKeyCode() == KeyEvent.VK_DOWN) dy = 1;
}
public void keyReleased(){
dx = 0;
dy = 0;
}
}
/*
*its the SECOND class
*save is as Game.java
/*
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Game extends JPanel{
static int WIDTH = 500;
static int HEIGHT = 700;
Car car = new Car(this);
public Game(){
addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
car.keyReleased();
}
@Override
public void keyPressed(KeyEvent e) {
car.keyPressed(e);
}
});
setFocusable(true);
}
private void move(){
car.move();
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
car.paint(g2d);
}
public static void main(String args[]) throws InterruptedException{
Game game = new Game();
JFrame frame = new JFrame("Car Game");
frame.add(game);
frame.getContentPane().setBackground(Color.white);
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
while(true){
game.move();
game.repaint();
Thread.sleep(1);
}
}
}
////////////////////EnD of Second Class////////////////////////////