I'm currently trying to make a game for fun. So far I'm trying to move a square using arrow keys. I've tried to use keylistener and it isn't working well so I've decided to use key bindings. I've made it so that the square responds to the keys that I press, but the square keeps moving once I release the key. I know that in keylistener there's a built-in method where this is resolved, but im not sure how you do it with keybindings.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Game extends JPanel {
public static final int WIDTH = 800;
public static final int HEIGHT = 800;
Player p = new Player(WIDTH / 2, 500, Color.blue);
public Game() {
new Window(WIDTH, HEIGHT, "Game", this);
setKeyBindings();
}
public void setKeyBindings() {
ActionMap actionMap = getActionMap();
int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = getInputMap(condition);
String vkLeft = "VK_LEFT";
String vkRight = "VK_RIGHT";
String vkUp = "VK_UP";
String vkDown = "VK_DOWN";
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), vkLeft);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), vkRight);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), vkUp);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), vkDown);
actionMap.put(vkLeft, new KeyAction(vkLeft));
actionMap.put(vkRight, new KeyAction(vkRight));
actionMap.put(vkUp, new KeyAction(vkUp));
actionMap.put(vkDown, new KeyAction(vkDown));
}
public class KeyAction extends AbstractAction {
public KeyAction(String actionCommand) {
putValue(ACTION_COMMAND_KEY, actionCommand);
}
@Override
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
case "VK_LEFT":
p.setVelX(-1);
System.out.println(e.getActionCommand());
break;
case "VK_RIGHT":
p.setVelX(1);
break;
case "VK_UP":
p.setVelY(-1);
break;
case "VK_DOWN":
p.setVelY(1);
break;
}
}
}
public void gameLoop(Graphics2D g) {
g.setColor(Color.black);
g.fillRect(0, 0, WIDTH, HEIGHT);
p.draw(g);
p.update();
repaint();
}
// Paint component
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
gameLoop(g2d);
repaint();
}
}
import java.awt.*;
import java.io.File;
import javax.imageio.*;
import java.io.IOException;
import java.awt.image.*;
//Class for the player
public class Player {
//Variables for the players x, y, x-velocity and y-velocity
private int x, y;
private int velX = 0;
private int velY = 0;
private Color c;
public Player(int x, int y, Color c) {
this.x = x;
this.y = y;
this.c = c;
}
//Gets the current x
public int getX(){
return x;
}
//Gets the current y
public int getY(){
return y;
}
//Sets the x
public void setX(int x){
this.x += x;
}
//Sets the y
public void setY(int y){
this.y += y;
}
//Updates the x and y variables for movement
public void update(){
x += velX;
y += velY;
}
//Sets x-velocity
public void setVelX(int velX){
this.velX = velX;
}
//Sets y-velocity
public void setVelY(int velY){
this.velY = velY;
}
public void draw(Graphics g){
g.setColor(c);
g.fillRect(x, y, 10, 10);
}
}
I've shown two of my classes. The first one is where most of the actual game code is. The second class is for the square or "player"