Trying to add a key keylistener to a program i am making but it wont work. cant find the reason :/ any help would be awesome:)
Display.class
package com.mono.main;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.mono.main.handler.Controller;
import com.mono.main.handler.Game;
import com.mono.main.handler.InputHandler;
public class Display extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public static final String TITLE = "Minefront Pre-Alpha 0.01";
private boolean running = false;
private Thread thread;
private InputHandler input;
private Dude d;
private Game gamer;
public Display(){
d = new Dude();
gamer = new Game();
input = new InputHandler();
addKeyListener(input);
System.out.println("HEllo");
}
public void run() {
while(running){
tick();
repaint();
}
}
private void tick(){
gamer.tick(input.key);
}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
d.drawDude(g2d);
}
public static void main(String[] args){
Display game = new Display();
JFrame frame = new JFrame();
frame.add(game);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
game.start();
}
private void start() {
if(running)
return;
running = true;
thread = new Thread(this);
thread.start();
}
}
InputHandler.class
package com.mono.main.handler;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class InputHandler implements KeyListener {
public boolean[] key = new boolean[68836];
public InputHandler(){
System.out.println("Input Conctructur");
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println("Key pressed");
int keyCode = e.getKeyCode();
if(keyCode > 0 && keyCode < key.length){
key[keyCode] = true;
}
}
@Override
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode > 0 && keyCode < key.length){
key[keyCode] = false;
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
Game.class
package com.mono.main.handler;
import java.awt.event.KeyEvent;
public class Game {
public Controller control;
public Game(){
control = new Controller();
}
public void tick(boolean[] key){
//System.out.println(key);
boolean left = key[KeyEvent.VK_A];
boolean right = key[KeyEvent.VK_D];
boolean jump = key[KeyEvent.VK_SPACE];
control.tick(left, right, jump);
}
}
Controller.class
package com.mono.main.handler;
public class Controller {
public int x, y;
public void tick(boolean left, boolean right, boolean jump){
//System.out.println();
if(left){
System.out.println("Game Left");
x--;
}
if(right){
x++;
}
if(jump){
}
}
}
Dude.class
package com.mono.main;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.ImageIcon;
import com.mono.main.handler.Controller;
public class Dude {
Image dudeImage;
Controller c;
public Dude(){
ImageIcon i = new ImageIcon("Dude.gif");
dudeImage = i.getImage();
c = new Controller();
}
public void drawDude(Graphics2D g2d){
g2d.drawImage(dudeImage,c.x,c.y,null);
}
}