I am making a game for my end-of-year school project, and in my game, a need to be able to move the player around the screen with the arrow keys. I would like to move the image (playerUpImageLabel) with my arrow keys, but I do not know how. I have tries looking around online on how to do this, but no luck.
The program works as of now, but I do not know how to move the image (playerUpImageLabel) with my arrow keys.
Any Help Please?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Game {
public void game() {
JFrame gameFrame = new JFrame();
JPanel gamePanel = new JPanel();
JLabel floorLabel = new JLabel();
JLabel copyrightLabel = new JLabel();
ImageIcon floorImage = new ImageIcon();
int playerMovementX;
int playerMovementY;
playerMovementX = 280;
playerMovementY = 280;
ImageIcon playerUpImage = new ImageIcon();
JLabel playerUpImageLabel = new JLabel();
ImageIcon playerLeftImage = new ImageIcon();
JLabel playerLeftImageLabel = new JLabel();
ImageIcon playerRightImage = new ImageIcon();
JLabel playerRightImageLabel = new JLabel();
ImageIcon playerDownImage = new ImageIcon();
JLabel playerDownImageLabel = new JLabel();
ImageIcon playerNormalImage = new ImageIcon();
JLabel playerNormalImageLabel = new JLabel();
gameFrame = new JFrame("Zombehs");
gameFrame.setVisible(true);
gameFrame.setSize(600, 620);
gameFrame.setResizable(false);
gameFrame.setLocationRelativeTo(null);
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gamePanel = new JPanel();
gamePanel.setLayout(null);
gameFrame.add(gamePanel);
floorImage = new ImageIcon(getClass().getResource("/Users/JakeBorg/Desktop/JavaPro/res/floor.png"));
floorLabel = new JLabel(floorImage);
floorLabel.setBounds(0, 0, 600, 600);
copyrightLabel = new JLabel("Copyright @ 2016 Jake_Borg");
copyrightLabel.setFont(new Font("DorFont01", Font.BOLD, 10));
copyrightLabel.setBounds(500, 580, 100, 10);
// Player
playerUpImage = new ImageIcon(getClass().getResource("/Users/JakeBorg/Desktop/JavaPro/res/player/Up_1.png"));
playerUpImageLabel = new JLabel(playerUpImage);
playerUpImageLabel.setBounds(playerMovementX, playerMovementY, 33, 33);
playerNormalImageLabel = playerUpImageLabel;
gamePanel.add(playerNormalImageLabel);
gamePanel.add(floorLabel);
gamePanel.add(copyrightLabel);
}
}