Key Event Listener Not Working - java

So I'm working on implementing key input to move a sprite around, but I'm having trouble because, for some unknown reason, I can't get the keyboard input to register. I've been able to track down my issue to the fact that it's not calling a the TAdapter method, but I don't know why.
Here is my code:
public class Game extends JPanel implements ActionListener
{
private static final long serialVersionUID = 1L;
private static Action upKeyAction;
//Frame Width and Height
static int WIDTH = 600;
static int HEIGHT = 400;
Timer timer;
Magnus magnus;
//Get Background
BufferedImage background = null;{
try {
background = ImageIO.read(new URL("http://i.imgur.com/YXlnj1g.png"));
System.out.println("Gotten Background");
} catch (IOException e) {
System.out.println("Incorrect Background");
}}
public Game()
{
addKeyListener(new TAdapter());
setFocusable(true);
setDoubleBuffered(true);
requestFocusInWindow();
magnus = new Magnus();
timer = new Timer(5, this);
timer.start();
System.out.println("Game Intialized");
}
public void paint(Graphics g)
{
super.paint(g);
//System.out.println("Graphics Painted");
g.drawImage(background, 0, 0, null);
g.drawImage(Magnus.magnus, magnus.getX(), magnus.getY(), this);
}
public void actionPerformed(ActionEvent e)
{
magnus.move();
repaint();
}
private class TAdapter extends KeyAdapter
{
public void keyReleased(KeyEvent e)
{
magnus.keyReleased(e);
}
public void keyPressed(KeyEvent e)
{
magnus.keyPressed(e);
System.out.println("Key Pressed");
}
}
And what should happen when the keys are pressed:
public class Magnus
{
private int dx;
private int dy;
private int x;
private int y;
public static BufferedImage magnus;
public Magnus()
{
try {
magnus = ImageIO.read(new URL("http://i.imgur.com/eoz7j06.png"));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Gotten Magnus");
//Set Position
x = 40;
y = 60;
}
public void move()
{
x += dx;
y += dy;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public BufferedImage getImage()
{
return magnus;
}
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT)
{
dx = -1;
System.out.println("Left button pressed");
}
if (key == KeyEvent.VK_RIGHT)
{
dx = 1;
System.out.println("Right button pressed");
}
if (key == KeyEvent.VK_UP)
{
dy = -1;
System.out.println("Up button pressed");
}
if (key == KeyEvent.VK_DOWN)
{
dy = 1;
System.out.println("Down button pressed");
}
}
public void keyReleased(KeyEvent e)
{
System.out.print("Key event initialized");
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT)
{
dx = 0;
}
if (key == KeyEvent.VK_RIGHT)
{
dx = 0;
}
if (key == KeyEvent.VK_UP)
{
dy = 0;
}
if (key == KeyEvent.VK_DOWN)
{
dy = 0;
}
}
}

Try implements KeyListener instead of implements ActionListener

Related

Not able to see the picture, just shows a black background

public class Board extends JPanel implements ActionListener {
private Player player;
private Timer timer;
public ArrayList<Enemy> enemyList;
public ArrayList<Bullet> bulletList;
private Random randomNumber;
public int WIDTH;
public int HEIGHT;
public Image plane;
public Image enemyimage;
private int x,y;
private final int DELAY = 25;
private final int B_WIDTH = 300;
private final int B_HEIGHT = 300;
private final int ICRAFT_X = 40;
private final int ICRAFT_Y = 60;
public Board(){
initBoard();
}
public void initBoard(){
addKeyListener(new TAdapter());
setBackground(Color.BLACK);
setFocusable(true);
setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
player = new Player();
loadImage();
timer = new Timer(DELAY, this);
timer.start();
}
public void loadImage(){
ImageIcon ii = new ImageIcon("src/resources/planeimage.png");
plane = ii.getImage();
}
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
doDrawing(g);
}
public void doDrawing(Graphics g){
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(player.getImage(),player.getX(),player.getY(),this);
ArrayList<Bullet> bulletList = player.getBullets();
for (Bullet bullet : bulletList){
g2d.drawImage(bullet.getImage(),bullet.getX(),bullet.getY(),this);
}
}
#Override
public void actionPerformed(ActionEvent e){
updateBullets();
updatePlayer();
repaint();
}
public void updateBullets() {
for (int i = 0; i < bulletList.size(); i++) {
Bullet bullet = bulletList.get(i);
if (bullet.isVisible()) {
bullet.move();
} else {
bulletList.remove(i);
}
}
}
public void updatePlayer(){
player.move();
}
private class TAdapter extends KeyAdapter {
#Override
public void keyReleased(KeyEvent e) {
player.keyReleased(e);
}
#Override
public void keyPressed(KeyEvent e) {
player.keyPressed(e);
}
}
This is the player class which extends a vehicle class. Player constructor is being called in the board class and i guess, the problem lies their.
public class Player extends Vehicle{
public ArrayList<Bullet> bulletList;
public Player(){
super();
initPlayer();
}
public void initPlayer(){
bulletList = new ArrayList<>();
loadImage("src/resources/planeimage.png");
getImageDimensions();
}
#Override
public void move(){
x += dx;
y += dy;
}
public ArrayList<Bullet> getBullets(){
return bulletList;
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_SPACE) {
fire();
}
if (key == KeyEvent.VK_LEFT) {
dx = -1;
}
if (key == KeyEvent.VK_RIGHT) {
dx = 1;
}
if (key == KeyEvent.VK_UP) {
dy = -1;
}
if (key == KeyEvent.VK_DOWN) {
dy = 1;
}
}
#Override
public void fire() {
bulletList.add(new Bullet(x + w, y + h / 2));
}
public void keyReleased(KeyEvent e){
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
dx = 0;
}
if (key == KeyEvent.VK_RIGHT) {
dx = 0;
}
if (key == KeyEvent.VK_UP) {
dy = 0;
}
if (key == KeyEvent.VK_DOWN) {
dy = 0;
}
}
}
public class Application extends JFrame {
public Application() {
initUI();
}
private void initUI() {
add(new Board());
setSize(1024, 768);
setTitle("Application");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
Application ex = new Application();
ex.setVisible(true);
});
}
}
Application class is the main class. Board class has all the code. When i run the program, only the black background is being shown, not the images. Why is that so, what am i doing wrong. Thank you.

How to make the sprite move once when arrow key is pressed?

I am trying to make the sprite stop continuously moving. Right now, the code makes the sprite move in the direction indicated by the user through arrow keys without stopping. I want to press an arrow key and make the sprite move once (ex. 10 steps only) in that direction.
public class Contents extends JPanel implements ActionListener, KeyListener {
Timer t = new Timer(100, this);
private Image man;
int x=0, y=0;
public Contents() {
super.setDoubleBuffered(true);
t.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}
#Override
public void paintComponent (Graphics g) {
super.paintComponent(g);
super.setBackground(Color.white);
ImageIcon ii = new ImageIcon("guy.png");
man = ii.getImage();
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(man, x, y, this);
}
double xv = 0;
double yv = 0;
public void move() {
x += xv;
y += yv;
}
public void up() {
yv = -1;
xv = 0;
}
public void down() {
yv = 1;
xv = 0;
}
public void left () {
xv = -1;
yv = 0;
}
public void right () {
xv = 1;
yv = 0;
}
#Override
public void actionPerformed(ActionEvent e) {
repaint();
//Collision for left and right walls
if (x==0) {
xv = 1;
}
else if (x==900-240) {
xv = -1;
}
// Collision for top and bottom walls
if (y==0) {
yv = 1;
}
else if (y==600-320) {
yv = -1;
}
move();
}
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_UP) {
up();
}
if (code == KeyEvent.VK_DOWN) {
down();
}
if (code == KeyEvent.VK_LEFT) {
left();
}
if (code == KeyEvent.VK_RIGHT) {
right();
}
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
}
It works when I delete the boundaries under ActionEvent. The boundaries prevent the sprite from going off screen. But I want to keep the boundaries and control the sprite too.
I figured it out. Under keyReleased, set xv=0. Then when you release the arrow key, the sprite will stop moving.

Java KeyListener dissappears when adding new object

Brand new to this forum. Tried to learn Java by tutorials for a while, which have worked fine until now when I'm playing around with Swing making a Pong game..
My problem is that when I try to change level in my game my player's keylistener stops working.
I have tried to remove all unnecessary code, hopefully I haven't missed any that causes confusion.
This is what happens in my Main-class, where Data is basically a JPanel working as my background and Player is Another JPanel, I'll get to the Player-class after main.
public class Main {
Data data = new Data(Color.RED);
Player player = new Player(700, 500, 20, 100);
public Main() throws InterruptedException{
run(data, player);
ballRecursive();
}
public void run(Data data) throws InterruptedException {
frame.add(player, BorderLayout.WEST);
frame.add(data, BorderLayout.CENTER);
}
public void ballRecursive() throws InterruptedException {
if(playerScore.returnScore()==1){
Data data = new Data(Color.GREEN);
run(data);
}
This is my Player, tried to remove anything unnecessary here aswell:
public class Player extends JPanel implements ActionListener, KeyListener{
Timer timer = new Timer(5, this);
double yPos = 0, movementY = 0;
int squareSize = 60;
int yBound;
public Player(int w, int h, int width, int height) {
timer.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
yBound = (windowHeight - squareSize);
}
public void actionPerformed(ActionEvent e) {
yPos += movementY;
if (yPos < 0) {
yPos = 0;
} else if (yPos + squareSize > yBound) {
yPos = yBound - squareSize;
}
repaint();
}
public void moveUp()
{
if (yPos == 0)
{
movementY = 0;
}
movementY = -1;
}
public void moveDown()
{
if (yPos == yBound)
{
movementY = 0;
}
movementY = 1;
}
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_UP) {
moveUp();
}
if (keyCode == KeyEvent.VK_DOWN) {
moveDown();
}
}
public void keyReleased(KeyEvent e)
{
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_UP)
{
movementY = 0;
}
if (keyCode == KeyEvent.VK_DOWN)
{
movementY = 0;
}

Problems with KeyBindings

I've switched from KeyListeners to KeyBindings as instructed, however they still seem to do nothing. My keybinds are set up as to allow the left and right arrow keys to call a setDx() method in paddle.java which instructs the move() method to move the paddle.
gamePanel.java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class gamePanel extends JPanel implements ActionListener {
paddle Paddle;
boolean ingame = true;
int delay = 1000;
Timer timer;
JLabel text = new JLabel("stuff here");
InputMap im = this.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
ActionMap am = this.getActionMap();
public gamePanel() {
setBackground(Color.WHITE);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "RightArrow");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "LeftArrow");
add(text);
text.setBounds(100, 100, 200, 300);
timer = new Timer(delay, this);
Paddle = new paddle();
timer.start();
}
#Override
public void paint(Graphics g) {
super.paint(g);
if (ingame) {
g.drawImage(Paddle.getImage(), Paddle.getX(), Paddle.getY(),
Paddle.getWidth(), Paddle.getHeight(), this);
}
}
#Override
public void actionPerformed(ActionEvent ae) {
Object obj = ae.getSource();
if (obj == timer) {
Paddle.move();
validate();
repaint();
}
}
public class ArrowAction extends AbstractAction {
private String cmd;
public ArrowAction(String cmd) {
this.cmd = cmd;
}
#Override
public void actionPerformed(ActionEvent e) {
if (cmd.equalsIgnoreCase("LeftArrow")) {
Paddle.setDx(-20);
} else if (cmd.equalsIgnoreCase("RightArrow")) {
Paddle.setDx(20);
}
}
}
/*
#Override
public void keyPressed(KeyEvent ke) {
int KeyCode = ke.getKeyCode();
if (KeyCode == KeyEvent.VK_LEFT) {
text.setText("key pressed");
Paddle.setDx(-20);
}
if (KeyCode == KeyEvent.VK_RIGHT) {
Paddle.setDx(20);
}
}
#Override
public void keyReleased(KeyEvent ke) {
int KeyCode = ke.getKeyCode();
if (KeyCode == KeyEvent.VK_LEFT) {
Paddle.setDx(0);
}
if (KeyCode == KeyEvent.VK_RIGHT) {
Paddle.setDx(0);
}
}
#Override
public void keyTyped(KeyEvent ke) {
}
*/
}
Paddle.java:
import java.awt.Image;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
public class paddle {
int dx = 0;
int x, y;
int height, width;
Image image;
public paddle() {
ImageIcon ii = new ImageIcon("src/Paddle.png");
image = ii.getImage();
width = image.getWidth(null);
height = image.getHeight(null);
//dx = 20;
resetState();
}
public void setDx(int z) {
dx = z;
}
public void move() {
x += dx;
if (x <= 2) {
x = 2;
}
if (x >= (640 - getWidth())) {
x = (640 - getWidth());
}
}
public void resetState() {
x = 250;
y = 375;
}
public void setX(int x) {
this.x = x;
}
public int getX() {
return x;
}
public void setY(int y) {
this.y = y;
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
Image getImage() {
return image;
}
Rectangle getRect() {
return new Rectangle(x, y, image.getWidth(null), image.getHeight(null));
}
}
If your KeyListener methods are not being called I suspect it's because the correct component does not have focus. It's difficult sometimes to manage what component has focus especially in a game, so I would suggest switching over to using key bindings which doesn't require components to have focus.
You never add the corresponding actions you the ActionMap
am.put("LeftArrow", new ArrowAction("LeftArrow"));
am.put("RightArrow", new ArrowAction("RightArrow"));
Also, if you don't repaint() in the actioPerformed of the ArrowAction, you won't see it update immediately, until repaint() is called by the Timer, which isn't very long, but still a miniscule delay.

Java Game Sprite Stuck

I am having a problem with a stuck sprite as soon as I switch to another JPanel via CardLayout.
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Game extends JFrame implements Runnable {
private viewA v1;
private viewB v2;
private viewC v3;
private viewD v4;
private Player player;
private CardLayout c1;
private JPanel contPanel;
// ############## VIEW A BUTTONLISTENER
public class ViewAButtonList implements ActionListener{
public void actionPerformed(ActionEvent ev){
try{
c1.show(contPanel, "1");
} catch (Exception ex){
ex.printStackTrace();
}
}
}
// ############## VIEW B BUTTON LISTENER
public class ViewBButtonList implements ActionListener{
public void actionPerformed(ActionEvent ev){
try{
c1.show(contPanel, "2");
} catch (Exception ex){
ex.printStackTrace();
}
}
}
// ############## VIEWC BUTTON ACTIONEVENT
public class ViewCButtonList implements ActionListener{
public void actionPerformed(ActionEvent ev){
try{
c1.show(contPanel, "3");
} catch (Exception ex){
ex.printStackTrace();
}
}
}
// ############## VIEW D BUTTON ACTION EVENT
public class ViewDButtonList implements ActionListener{
public void actionPerformed(ActionEvent ev){
try{
c1.show(contPanel, "4");
} catch (Exception ex){
ex.printStackTrace();
}
}
}
public Game() {
player = new Player();
// load player settings from server
// ..
// contPanel = new JPanel();
//load views v1 = new ViewA(Player); v2=new ViewB(Player); v3 = new ViewC(Player); v4 =new ViewD(Player);
c1 = new CardLayout();
contPanel.setLayout(c1);
contPanel.add(v1,"1");
contPanel.add(v2,"2");
contPanel.add(v3"3");
contPanel.add(v4,"4");
c1.show(contPanel, "2");
currPos =2;
this.add(contPanel);
setSize(652, 480);
setLocationRelativeTo(null);
setTitle("GAME");
setResizable(false);
setVisible(true);//go to end of view B (x=0), change to View A, close view B
// Create 4 of each button to place in each seperate view (to swtich back and forth)
// ................
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Game();
}
});
}
#Override public void run() { // TODO Auto-generated method stub
} }
Now.. everything works from the standpoint of switching between screens.. but my Player sprite gets stuck on screen after switch, but on initial load I can move the sprite around with the arrow keys. I am not sure if passing the Player on the new ViewA(Player) might be the culprit.. but I have a feeling that it is.. can't figure out what I am doing wrong..
Here's theplayer:
import java.awt.Image; import java.awt.Rectangle; import
java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.ImageIcon;
public class Player {
private String playersprite = "playersprite.png";
private int dx;
private int dy;
private int x;
private int y;
private int width;
private int height;
private boolean isFired;
private boolean visible;
private Image image;
private ArrayList missiles;
public Player() {
ImageIcon ii = new ImageIcon(this.getClass().getResource(playersprite));
image = ii.getImage();
width = image.getWidth(null);
height = image.getHeight(null);
missiles = new ArrayList();
visible = true;
isFired=false;
//default spawn location
x = 600;
y = 400;
}
public void move() {
x += dx;
y += dy;
if(x<=0){x=0;} if(x>=640-20){x=640-20;} if(y<=0){y=0;}
if(y>=400){y=400;}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setX(int x){
this.x=x;
}
public void setY(int y){
this.y=y;
}
public Image getImage() {
return image;
}
public ArrayList getMissiles() {
return missiles;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
public boolean isVisible() {
return visible;
}
public Rectangle getBounds() {
return new Rectangle(x, y, 32, 32);
}
public void keyPressed(KeyEvent e) {
System.out.println("X: "+x+",Y: "+y);
int key = e.getKeyCode();
if (key == KeyEvent.VK_SPACE) {
if(isFired==false){
isFired=true;
fire();
} else return;
}
if (key == KeyEvent.VK_LEFT) {
dx = -2;
}
if (key == KeyEvent.VK_RIGHT) {
dx = 2;
}
if (key == KeyEvent.VK_UP) {
dy = -2;
}
if (key == KeyEvent.VK_DOWN) {
dy = 2;
}
}
public void fire() {
System.out.println("Player used weapon");
// missiles.add(new Missile(x, y));
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
dx = 0;
}
if (key == KeyEvent.VK_RIGHT) {
dx = 0;
}
if (key == KeyEvent.VK_UP) {
dy = 0;
}
if (key == KeyEvent.VK_DOWN) {
dy = 0;
}
if (key == KeyEvent.VK_SPACE) {
isFired=false;
}
} }
KeyListener require that the component they are registered are focusable AND have focus, this means that when you click something like a JButton focus moves to the button and the component with a KeyListener no longer has keyboard focus and therefore will no longer receive key events.
Instead, it is recommended that you use Key Bindings as this API has the capacity to overcome these limitations

Categories