My Key Listener/Event не запускается вообще. Есть что-то, чего я здесь не хватает, а может быть, где-то еще?KeyListener/KeyEvent не работает?
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
public class Player implements KeyListener{
private final double speed = 5.0d;
private BufferedImage pSprite;
private Rectangle rect;
private double xPos, yPos;
private int width, height;
private boolean right = false, left = false;
//=============================================================================
public Player(double xPos, double yPos, int width, int height){
this.xPos = xPos;
this.yPos = yPos;
this.width = width;
this.height = height;
rect = new Rectangle((int)xPos, (int)yPos, width, height);
try{
URL url = this.getClass().getResource("/images/Player.png");
pSprite = ImageIO.read(url);
}catch(IOException e){};
}// Player
public void draw(Graphics2D g){
g.drawImage(pSprite, (int)xPos, (int)yPos, width, height, null);
}// draw
//=============================================================================
public void update(double delta){
if(right && !left){
xPos += speed * delta;
rect.x = (int) xPos;
}
else if(!right && left){
xPos -= speed * delta;
rect.x = (int) xPos;
}// if
}// update
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_D||key == KeyEvent.VK_RIGHT){
right = true;
System.out.println("right");
}// if
else if(key == KeyEvent.VK_A || key == KeyEvent.VK_LEFT){
left = true;
System.out.println("left");
}// else if
}// keyPressed
@Override
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_D||key == KeyEvent.VK_RIGHT){
right = false;
}
else if(key == KeyEvent.VK_A || key == KeyEvent.VK_LEFT){
left = false;
}
}// keyReleased
@Override
public void keyTyped(KeyEvent e) {
}// keyTyped
}
package game_screen;
import java.awt.Canvas;
import java.awt.Graphics2D;
import state.SuperStateMachine;
public class GameScreen implements SuperStateMachine{
private Player player;
public GameScreen(){
player = new Player(150, 150, 50, 50);
}// GameScreen
@Override
public void update(double delta) {
player.update(delta);
}// update
@Override
public void draw(Graphics2D g) {
player.draw(g);
}// draw
@Override
public void init(Canvas canvas) {
canvas.addKeyListener(player);
}
}// class GameScreen
пакет дисплей;
импорт java.awt.Canvas;
import java.awt.Color;
импорт java.awt.Graphics2D;
импорт java.awt.image.BufferStrategy;
импорт javax.swing.JFrame;
состояние импорта.StateMachine;
общественного класса Display расширяет Canvas реализует Runnable {
public static int WIDTH = 800, HEIGHT = 600;
private static final long serialVersionUID = 1L;
private Thread thread;
private boolean running = false;
public int FPS;
public static void main(String[] args){
// Display Window/Frame
Display display = new Display();
JFrame frame = new JFrame();
frame.add(display);
frame.pack();
frame.setTitle("Space Invaders");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
display.start();
}// main
public synchronized void start(){
if(running)
return;
running = true;
thread = new Thread (this);
thread.start();
}// Start
public synchronized void stop(){
if(!running)
return;
running = false;
try {
thread.join();
}// try
catch (InterruptedException e) {
e.printStackTrace();
}// catch
}// Stop
public static StateMachine state;
public Display(){
this.setSize(WIDTH, HEIGHT);
this.setFocusable(true);
state = new StateMachine(this);
state.setState((byte) 0);
}// Display Dimensions
@Override
public void run() {
long timer = System.currentTimeMillis();
long lastLoopTime = System.nanoTime();
final int TARGET_FPS = 60;
final long OPTIMAL_TIME = 1000000000/TARGET_FPS;
int frames = 0;
this.createBufferStrategy(3);
BufferStrategy bs = this.getBufferStrategy();
while(running){
long now = System.nanoTime();
long updateLength = now - lastLoopTime;
lastLoopTime = now;
double delta = updateLength/((double)OPTIMAL_TIME);
frames++;
if(System.currentTimeMillis() - timer > 1000){
timer += 1000;
FPS = frames;
frames = 0;
System.out.println(FPS);
}// if
draw(bs);
update(delta);
try{
Thread.sleep(((lastLoopTime - System.nanoTime()) + OPTIMAL_TIME)/1000000);
}// try
catch(Exception e){
}// catch
}// while
}// run
public void draw(BufferStrategy bs){
do{
do{
Graphics2D g = (Graphics2D) bs.getDrawGraphics();
g.setColor(Color.darkGray);
g.fillRect(0, 0, WIDTH + 50, HEIGHT + 50);
state.draw(g);
g.dispose();
}// do
while(bs.contentsRestored());
bs.show();
}// do
while(bs.contentsLost());
}// draw
public void update(double delta){
state.update(delta);
}
} // Runnable
пакет спрайт;
импорт java.awt.Graphics2D;
импорт java.awt.image.BufferedImage;
импорт java.util.ArrayList;
импорт таймер.Timer;
общественного класса SpriteAnimation {
private ArrayList<BufferedImage> sprites = new ArrayList<BufferedImage>();
private byte currentSprite;
private boolean loop = false;
private boolean play = false;
private boolean destroyAfterAnim = false;
private Timer timer;
private int animationSpeed;
private double xPos, yPos;
public SpriteAnimation(double xPos, double yPos, int animationSpeed){
this.animationSpeed = animationSpeed;
this.xPos = xPos;
this.yPos = yPos;
timer = new Timer();
}// SpriteAnimation
public void draw(Graphics2D g){
if(isSpriteAnimDestroyed())
return;
g.drawImage(sprites.get(currentSprite), (int)getxPos(), (int)getyPos(), null);
}// draw
public void update(double delta){
if(isSpriteAnimDestroyed())
return;
if(loop && !play)
loopAnimation();
if(play && !loop)
playAnimation();
}// update
public void stopAnimation(){
loop = false;
play = false;
}
public void resetSprite(){
loop = false;
play = false;
currentSprite = 0;
}
private void loopAnimation(){
if(timer.isTimerReady(animationSpeed) && currentSprite == sprites.size()-1){
currentSprite = 0;
timer.resetTimer();
}// if
else if(timer.timerEvent(animationSpeed) && currentSprite != sprites.size()-1){
currentSprite++;
}// else if
}// loopAnimation
private void playAnimation(){
if(timer.timerEvent(animationSpeed) && currentSprite != sprites.size()-1 && !isDestroyAfterAnim()){
play = false;
currentSprite = 0;
}// if
else if(timer.timerEvent(animationSpeed) && currentSprite == sprites.size()-1 && isDestroyAfterAnim()){
sprites = null;
}// else if
else if(timer.timerEvent(animationSpeed) && currentSprite != sprites.size()-1){
currentSprite++;
}// if
}// playAnimation
public byte getCurrentSprite() {
return currentSprite;
}// getCurrentSprite
public void setCurrentSprite(byte currentSprite) {
this.currentSprite = currentSprite;
}// setCurrentSprite
public boolean isLoop() {
return loop;
}// isLoop
public void setLoop(boolean loop) {
this.loop = loop;
}// setLoop
public boolean isSpriteAnimDestroyed(){
if(sprites == null)
return true;
return false;
}// isSpriteAnimDestroyed
public void addSprite(BufferedImage spriteMap, int xPos, int yPos, int width, int height){
sprites.add(spriteMap.getSubimage(xPos, yPos, width, height));
}// addSprite
public void PlayerAnimation(boolean play, boolean destroyAfterAnim){
this.play = play;
this.setDestroyAfterAnim(destroyAfterAnim);
}// PlayerAnimation
public double getxPos() {
return xPos;
}// getxPos
public void setxPos(double xPos) {
this.xPos = xPos;
}// setxPos
public double getyPos() {
return yPos;
}// getyPos
public void setyPos(double yPos) {
this.yPos = yPos;
}// setyPos
public boolean isDestroyAfterAnim() {
return destroyAfterAnim;
}// isDestroyAfterAnim
public void setDestroyAfterAnim(boolean destroyAfterAnim) {
this.destroyAfterAnim = destroyAfterAnim;
}// setDestroyAfterAnim
} // SpriteAnimation
состояния упаковки;
импорт java.awt.Canvas; импорт java.awt.Graphics2D;
импорт java.util.ArrayList;
import game_screen.GameScreen;
общественного класса StateMachine {
private ArrayList<SuperStateMachine> states = new ArrayList<SuperStateMachine>();
private Canvas canvas;
private byte selectState = 0;
public StateMachine(Canvas canvas){
SuperStateMachine game = new GameScreen();
states.add(game);
this.canvas = canvas;
}// StateMachine
public void draw(Graphics2D g){
states.get(selectState).draw(g);
}// draw
public void update(double delta){
states.get(selectState).update(delta);
}// update
public void setState(byte i){
for(int r = 0; r < canvas.getKeyListeners().length; r++){
canvas.removeKeyListener(canvas.getKeyListeners()[r]);
selectState = i;
states.get(selectState).init(canvas);
}// for
}// setState
public byte getStates(){
return selectState;
}
} // StateMachine Класс
состояния упаковки;
импорт java.awt.Canvas; импорт java.awt.Graphics2D;
общедоступный интерфейс SuperStateMachine {
public void update(double delta);
public void draw(Graphics2D g);
public void init(Canvas canvas);
} Таймер
пакет;
общественного класса Timer {
private long prevTime;
public Timer(){
setPrevTime(System.currentTimeMillis());
}// timer
public long getPrevTime() {
return prevTime;
}// getCurrentTime
public void setPrevTime(long currentTime) {
this.prevTime = currentTime;
}// setCurrentTime
public void resetTimer(){
prevTime = System.currentTimeMillis();
}
public boolean timerEvent(int timer){
if(System.currentTimeMillis() - getPrevTime() > timer){
resetTimer();
return true;
}// timerEvent
return false;
}// timerEvent
public boolean isTimerReady(int timer){
if(System.currentTimeMillis() - getPrevTime() > timer){
return true;
}
return false;
}
} // Класс Timer
Я добавил недостающий код –
@Bashamock: Что такое класс SuperStateMachine, который расширяет ваш класс? Это, как представляется, программа AWT, а не программа Swing. Я вижу, что вы добавляете свой KeyListener в объект Canvas, и я не вижу, чтобы этому объекту была назначена система фокуса. –
Это все мой код –