I'm working on a java project and i'm having a hard time to make the Keyboard input handler work. I have two separate classes, one called KeyInput and one called Player. When i'm starting the Player class and I press a key nothing will be printed, but if i use println in the KeyInput class it will work. So it does register when you press a button only it won't work when I want to make use of it within the Player class.
Player class:
public class Player extends JFrame {
private static final int IMAGE_TYPE = BufferedImage.TYPE_INT_ARGB;
private BufferedImage img;
KeyInput input
public Player() {
super();
this.add(new JPanel() {
#Override
protected void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}
});
img = new BufferedImage(660, 500, IMAGE_TYPE );
this.setSize(img.getWidth(), img.getHeight());
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
input = new KeyInput(this);
if(input.up.isPressed()){
System.out.println("Up");
}
this.setVisible( true );
}
}
KeyInput class:
public class KeyInput implements KeyListener {
BufferedImage img = null;
public KeyInput(Player player) {
player.requestFocus(); // click window to move bug fix he didn't add this
player.addKeyListener(this);
}
public class Key {
private int numTimesPressed = 0;
private boolean pressed = false;
public int getNumTimesPressed() {
return numTimesPressed;
}
public boolean isPressed() {
return pressed;
}
public void toggle(boolean isPressed) {
pressed = isPressed;
if (isPressed) {
numTimesPressed++;
}
}
}
public List<Key> keys = new ArrayList<Key>();
public Key up = new Key();
public Key down = new Key();
public Key left = new Key();
public Key right = new Key();
public Key esc = new Key();
public void keyPressed(KeyEvent e) {
toggleKey(e.getKeyCode(), true);
}
public void keyReleased(KeyEvent e) {
toggleKey(e.getKeyCode(), false);
}
public void keyTyped(KeyEvent e) {
}
public void toggleKey(int KeyCode, boolean isPressed) {
if (KeyCode == KeyEvent.VK_W || KeyCode == KeyEvent.VK_UP
|| KeyCode == KeyEvent.VK_NUMPAD8) {
up.toggle(isPressed);
}
if (KeyCode == KeyEvent.VK_S || KeyCode == KeyEvent.VK_DOWN
|| KeyCode == KeyEvent.VK_NUMPAD2) {
down.toggle(isPressed);
}
if (KeyCode == KeyEvent.VK_A || KeyCode == KeyEvent.VK_LEFT
|| KeyCode == KeyEvent.VK_NUMPAD4) {
left.toggle(isPressed);
}
if (KeyCode == KeyEvent.VK_D || KeyCode == KeyEvent.VK_RIGHT
|| KeyCode == KeyEvent.VK_NUMPAD6) {
right.toggle(isPressed);
}
if(KeyCode == KeyEvent.VK_ESCAPE){
System.exit(0);
}
}
}
You should use Key Bindings to assign key stroke to action on specific component.
A qoute from docs
component.getInputMap().put(KeyStroke.getKeyStroke("F2"),
"doSomething");
component.getActionMap().put("doSomething",
anAction);
//where anAction is a javax.swing.Action
https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html#howto
Related
When I hold up and left the program registers them fine. However, it won't register when I hit space. If you switch VK_LEFT to VK_RIGHT then proceed to hold right and up while hitting space it works fine.
import javax.swing.JFrame;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
public class Testing implements KeyListener {
public boolean isUp;
public boolean isLeft;
public Testing() {
JFrame application = new JFrame();
application.addKeyListener(this);
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.setSize(500, 500);
application.setVisible(true);
}
public static void main(String[] args) {
Testing test = new Testing();
}
#Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_UP)
isUp = true;
else if(e.getKeyCode() == KeyEvent.VK_LEFT)
isLeft = true;
else if(isUp && isLeft && e.getKeyCode() == KeyEvent.VK_SPACE)
System.out.println("pew pew");
}
#Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_UP)
isUp = false;
else if(e.getKeyCode() == KeyEvent.VK_LEFT)
isLeft = false;
}
#Override
public void keyTyped(KeyEvent e) {
}
}
It should register space while holding up and left like it does with up and right but it doesn't.
Hello i am trying to make the game pong. Now i am trying to move the paddle with my keyboard but it is not responding at all. Can anyone tell me what i am missing here? I can't figure it out what it is i am doing wrong.
The paddle is moving when i set the condition in the check to false so there is not a problem in moving it. Just getting the keyEvent to work.
What am i missing in the keyEvent?
Thanks in advance!
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class Paneel extends JPanel
{
private int height, width;
private boolean moveLeft, moveRight, moveUp, moveDown, playerMoveLeft,
playerMoveRight, computerMoveLeft, computerMoveRight = false;
private Timer timer;
private Ball ball;
private Paddle player, computer;
public Paneel()
{
ball = new Ball(994, 772);
player = new Paddle(1, 994, 722);
computer = new Paddle(2, 944, 722);
TimerHandler timerHandler = new TimerHandler();
timer = new Timer(20, timerHandler);
timer.start();
}
public void paintComponent(Graphics pen)
{
super.paintComponent(pen);
ball.drawBall(pen);
player.drawPaddle(pen);
computer.drawPaddle(pen);
pen.drawString("" + getHeight() + " " + getWidth(), 50, 50);
}
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_LEFT) playerMoveLeft = true;
else if (e.getKeyCode() == KeyEvent.VK_RIGHT) playerMoveRight =
true;
}
public void keyReleased(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_LEFT) playerMoveLeft = false;
else if (e.getKeyCode() == KeyEvent.VK_RIGHT) playerMoveRight =
false;
}
public void movePlayer()
{
if (playerMoveRight == true) player.moveDown();
else if (playerMoveLeft == true) player.moveUp();
}
public void moveComputer()
{
}
public void resetBall()
{
}
public void resetPlayer()
{
}
public void resetComputer()
{
}
class TimerHandler implements ActionListener
{
public void actionPerformed(ActionEvent e) {
movePlayer();
repaint();
}
}
}
This is the code for my game. The entire image flickers every 30 ms (aprox)
public class MainGame extends Canvas implements Runnable, KeyListener, MouseListener, MouseMotionListener {
boolean isRunning = false;
public boolean isClicked, isReleased, lClick, rClick, down, up, upPressed;
public boolean drawArrow;
public boolean lastLeft, lastRight, goingLeft, goingRight, left, right;
public int x = 0, y = 0, aX = 250, aY = 250;
double modifier = 4;
public Graphics g;
ImageIcon background = new ImageIcon("F:/workspace/RPGGame/src/healy.jpg");
Image picture = background.getImage();
Image offscreen;
ControlBase base = new ControlBase();
Graphics buffer;`
private boolean fireLeft;
private boolean fireRight;
private int arrowCounter = 0;
public MainGame () {
base.frame.setVisible(true);
base.frame.setResizable(false);
base.frame.setMinimumSize(new Dimension(1024, 768));
base.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
base.frame.setLocationRelativeTo(null);
base.frame.addMouseListener(this);
base.frame.addKeyListener(this);
JPanel panel = new JPanel();
Container pane = base.frame.getContentPane();
pane.add(panel);
pane.paint(g);
base.frame.setVisible(true);
Graphics g = base.frame.getGraphics();
g.drawImage(picture, x, y, this);
}
public void run() {
while(isRunning) {
paint(base.frame.getGraphics());
//moves left
if (left == true){
x+=1;
lastLeft = true;
lastRight = false;
}
//moves right
if (right == true){
x-=1;
lastLeft = false;
lastRight = true;
}
//moves down
if (down == true){
y-=1;
}
if (goingLeft == true) {
aX--;
}
if (goingRight == true) {
aX++;
}
if(attackStyle == 1) {
melee();
}
else if (attackStyle == 2) {
range();
}
else {
magic();
}
System.out.println(arrowCounter);
base.arrowMech();
fireLeft = base.fireLeft;
fireRight = base.fireRight;
base.frame.repaint();
}
}
#Override
public void paint(Graphics g) {
super.paint(g);
g.clearRect(0, 0, 1024, 768);
g.setColor(Color.red);
g.fillOval(250, 250, 20, 20);
g.drawImage(picture, x, y, this);
if (drawArrow == true) {
if (fireLeft) {
arrowCounter++;
goingLeft = true;
goingRight = false;
base.drawArrow(g);
}
if (fireRight) {
arrowCounter++;
goingLeft = false;
goingRight = true;
base.drawArrow(g);
}
}
if ((arrowCounter >=450) || (!drawArrow)){
arrowCounter = 0;
aX = 250;
aY = 250;
}
}
public void update(Graphics g) {
repaint();
}
public void start() {
isRunning = true;
new Thread(this).start();
}
public void stop() {
isRunning = false;
}
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
public void mouseClicked(MouseEvent click) {
if(click.getButton() == 1) {
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
/** This method handles mouse clicks
*/
public void mousePressed(MouseEvent click) {
if(SwingUtilities.isLeftMouseButton(click)) {
lClick = true;
}
else if (SwingUtilities.isRightMouseButton(click)) {
rClick = true;
}
}
/** This method handles the release of mouse clicks
*/
public void mouseReleased(MouseEvent release) {
if(SwingUtilities.isLeftMouseButton(release)) {
lClick = false;
}
else if (SwingUtilities.isRightMouseButton(release)) {
rClick = false;
}
}
/**
* This method handle the movement for the character and all other key binds
*/
public void keyPressed(KeyEvent e) {
//left arrow
if(e.getKeyCode() == 37 || e.getKeyCode() == 65){
left = true;
}
//up arrow
if(e.getKeyCode() == 38 || e.getKeyCode() == 87){
up = true;
upPressed = true;
down = false;
}
//right arrow
if(e.getKeyCode() == 39 || e.getKeyCode() == 68){
right = true;
}
//down arrow
if(e.getKeyCode() == 40 || e.getKeyCode() == 83){
down = true;
}
}
/**
* This method handles the stopping of movement for the character and stoping all other key binds
*/
public void keyReleased(KeyEvent e) {
//left arrow
if(e.getKeyCode() == 37 || e.getKeyCode() == 65){
left = false;
}
//up arrow
if(e.getKeyCode() == 38 || e.getKeyCode() == 87){
up = false;
upPressed = false;
}
//right arrow
if(e.getKeyCode() == 39 || e.getKeyCode() == 68){
right = false;
}
//down arrow
if(e.getKeyCode() == 40 || e.getKeyCode() == 83){
down = false;
}
}
public void keyTyped(KeyEvent e) {
}
Any help would be appreciated. Have tried using a bufferstrategy to no avail. Is there any other way to do it without using a bufferstrategy?
I don't think you need to call the paint(Graphics g) method. It should be called automatically by the parent when needed. Especially since you are calling repaint at the end of the code
So, I just added the InputHandler so I can move my screen with wasd (up,down,left,right). but when I try it my spritesheet gets torn apart and the screen does not move up or down, just diagonally (left and right works fine) and I dont know why it does this. I dont have any errors...
package ca.linus.game;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
public class InputHandler implements KeyListener {
public InputHandler(Game game) {
game.addKeyListener(this);
}
public class Key {
private int numTimesPressed = 0;
private boolean pressed = false;
public int getNumTimesPressed() {
return numTimesPressed;
}
public boolean isPressed() {
return pressed;
}
public void toggle(boolean isPressed) {
pressed = isPressed;
if (isPressed) numTimesPressed++;
}
}
public List<Key> keys = new ArrayList<Key>();
public Key up = new Key();
public Key down = new Key();
public Key left = new Key();
public Key right = new Key();
public void keyPressed(KeyEvent e) {
toggleKey(e.getKeyCode(), true);
}
public void keyReleased(KeyEvent e) {
toggleKey(e.getKeyCode(), false);
}
public void keyTyped(KeyEvent e) {
}
public void toggleKey(int keyCode, boolean isPressed) {
if (keyCode == KeyEvent.VK_W) {up.toggle(isPressed);}
if (keyCode == KeyEvent.VK_S) {down.toggle(isPressed);}
if (keyCode == KeyEvent.VK_D) {right.toggle(isPressed);}
if (keyCode == KeyEvent.VK_A) {left.toggle(isPressed);}
}
}
Here's my InputHandler file.
All help appreciated!
I have a key listener in my program and for most keys I want the user to be able to hold down a key. However the key for screenshots I want the user to be able to hold down the key yet it only takes one screenshot any ideas?
package me.bevilacqua.game;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class InputHandler implements KeyListener{
private boolean[] keys = new boolean[120];
public boolean up , down , left , right , screen_shot;
public void tick() {
up = keys[KeyEvent.VK_UP] || keys[KeyEvent.VK_W]; //Will be Jump
down = keys[KeyEvent.VK_DOWN] || keys[KeyEvent.VK_S]; //Will not be down but maybe slide or better yet action key or maybe shoot
left = keys[KeyEvent.VK_LEFT] || keys[KeyEvent.VK_A];
right = keys[KeyEvent.VK_RIGHT] || keys[KeyEvent.VK_D];
screen_shot = keys[KeyEvent.VK_F5] || keys[KeyEvent.VK_BACK_SLASH];
}
public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()] = true;
}
public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()] = false;
}
public void keyTyped(KeyEvent e) {
}
}
EDIT:
Why doesn't this work:
package me.bevilacqua.game;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Timer;
public class InputHandler implements KeyListener{
private boolean[] keys = new boolean[120];
public long current , last = -1;
public boolean up , down , left , right , screen_shot;
public boolean shotFlag = true; //True if allowed
public void tick() {
up = keys[KeyEvent.VK_UP] || keys[KeyEvent.VK_W]; //Will be Jump
down = keys[KeyEvent.VK_DOWN] || keys[KeyEvent.VK_S]; //Will not be down but maybe slide or better yet action key or maybe shoot
left = keys[KeyEvent.VK_LEFT] || keys[KeyEvent.VK_A];
right = keys[KeyEvent.VK_RIGHT] || keys[KeyEvent.VK_D];
screen_shot = keys[KeyEvent.VK_F5];
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() != KeyEvent.VK_F5) keys[e.getKeyCode()] = true;
else if (e.getKeyCode() == KeyEvent.VK_F5 && shotFlag) {
keys[e.getKeyCode()] = true;
shotFlag = false;
}
}
public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()] = false;
if(e.getKeyCode() == KeyEvent.VK_F5) shotFlag = true;
}
public void keyTyped(KeyEvent e) {
}
}
From what I understand, the action associated with every key repeats when the appropriate key is held down and you want to stop one of those actions from repeating even when the corresponding key is held down. I think you can solve this with an additional flag that is set when keyPressed() is called. Then screen_shot is only set in tick() when this flag is set and the flag is unset every time tick() is called (after reading its value of course). Alternatively, you can unset the flag only when it is set and you are setting the screen_shot flag. I don't think it makes any difference either way.
You could try it like this:
public boolean up = false;
public boolean down = false;
public boolean left = false;
public boolean right = false;
public boolean screen_shot = false;
//...
f.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_D)
{
right = true;
}
if (e.getKeyCode() == KeyEvent.VK_A)
{
left = true;
}
if (e.getKeyCode() == KeyEvent.VK_S)
{
down = true;
}
if (e.getKeyCode() == KeyEvent.VK_W)
{
up = true;
}
if (e.getKeyCode() == KeyEvent.VK_F5)
{
screen_shot = true;
}
}
public void keyReleased(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_D)
{
right = false;
}
if (e.getKeyCode() == KeyEvent.VK_A)
{
left = false;
}
if (e.getKeyCode() == KeyEvent.VK_S)
{
down = false;
}
if (e.getKeyCode() == KeyEvent.VK_W)
{
up = false;
}
if (e.getKeyCode() == KeyEvent.VK_F5)
{
screen_shot = false;
}
}
//...
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
this.setBackground(Color.white);
g.drawImage(character, characterLocation.x, characterLocation.y, this);
if (right)
{
characterLocation.x += 1;
}
if (left)
{
characterLocation.x -= 1;
}
if (down)
{
characterLocation.y += 1;
}
if (up)
{
characterLocation.y -= 1;
}
if (screen_shot)
{
BufferedImage shot = character.createScreenCapture(new Rectangle(100,100));
}
repaint();
}
}
Might not work up to par as you might have to change the "if" statement for screen shot above.