Java - KeyListener not working - java

This has been making me want to tear out my hair all day. I'm trying to make it so when I press the right-arrow key, my speed will go to 10. I know that my methods that make my sprite move work, because if I set a value beforehand, it moves across the screen. It's the KeyListener that isn't doing anything:
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class Sprite implements KeyListener{
public Image player = Toolkit.getDefaultToolkit().getImage("player.png");
int x, y, dx, dy;
public void doInit(){
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(600, 400);
window.setLocationRelativeTo(null);
window.getContentPane().add(new ImageDraw());
window.setVisible(true);
window.setIgnoreRepaint(true);
window.createBufferStrategy(2);
window.getContentPane().addKeyListener(this);
}
public Image getSpriteHandle(){
return player;
}
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_RIGHT){
dx = 10;
}
} //end keyPressed
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_RIGHT){
dx = 0;
}
}
public void keyTyped(KeyEvent arg0) {
}
public int moveX(){
return dx;
}
} //end class
Please help me!

It's the KeyListener that isn't doing anything
You should NOT be using a KeyListener.
Swing was designed to be used with Key Bindings. You can map an Action to a KeyStroke even when a component doesn't have focus.

Related

Null Pointer Exception on GUI application using three classes

I'm working on a fairly basic game for myself. New to Java Swing but not to Cpp. Onto the problem at hand.
As I start to run the GUI app, I'm shot with a Null Pointer Exception and given four links where they happen(I use Eclipse as my IDE). But when I look at them I see no problem with the lines or anything around them.
So I came to see if any of you can spot what I can't find.
This class grabs the picture from the source folder and sets up the keys for giving movement.
import java.awt.event.KeyEvent;
import java.awt.Image;
import javax.swing.ImageIcon;
public class SPRITES
{
private int x_sped;
private int y_sped;
private int x;
private int y;
private Image sprite;
public SPRITES()
{
ImageIcon pine = new ImageIcon(this.getClass().getResource("Untitled.png")); //exception here, line 17
sprite = pine.getImage();
x = 0;
y = 0;
}
public void move()
{
x += x_sped;
y += y_sped;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public Image getImage()
{
return sprite;
}
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if(key == KeyEvent.VK_A)
{
x_sped = -1;
}
if(key == KeyEvent.VK_D)
{
x_sped = 1;
}
if(key == KeyEvent.VK_W)
{
y_sped = -1;
}
if(key == KeyEvent.VK_S)
{
y_sped = 1;
}
}
public void keyReleased(KeyEvent e)
{
int key = e.getKeyCode();
if(key == KeyEvent.VK_A)
{
x_sped = 0;
}
if(key == KeyEvent.VK_D)
{
x_sped = 0;
}
if(key == KeyEvent.VK_W)
{
y_sped = 0;
}
if(key == KeyEvent.VK_S)
{
y_sped = 0;
}
}
}
The second one is for getting the frame setting up with completely making the frame and finishing up the making of the movement.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Stage extends JPanel implements ActionListener
{
private static final long serialVersionUID = 1L;
private Timer start_stop;
private SPRITES player;
public Stage()
{
addKeyListener(new TAdapter());
setFocusable(true);
setBackground(Color.black);
setDoubleBuffered(true);
player = new SPRITES();
start_stop = new Timer(5, this);
start_stop.start();
}
public void paint(Graphics character)
{
super.paint(character);
Graphics2D G2D = (Graphics2D) character;
G2D.drawImage(player.getImage(), player.getX(), player.getY(), this);
Toolkit.getDefaultToolkit().sync();
character.dispose();
}
public void actionPerformed(ActionEvent arg0)
{
player.move();
repaint();
}
private class TAdapter extends KeyAdapter
{
public void keyReleased(KeyEvent e)
{
player.keyReleased(e);
}
public void keyPressed(KeyEvent e)
{
player.keyPressed(e);
}
}
}
This last one should tie it all together...granted I knew how to fix the exception.
import javax.swing.JFrame;
#SuppressWarnings("serial")
public class Framing extends JFrame
{
public Framing()
{
add(new Stage());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 600);
setLocationRelativeTo(null);
setTitle("FLY");
setResizable(false);
setVisible(true);
}
public static void main(String[] args)
{
new Framing();
}
}
What is causing these exceptions to happen? How can I fix the problem and what should I work on to avoid this happening again?
I think you are misreading the stacktrace from eclipse. All the links except for the first(The one at the top) is the call stack, which show the calls made to the place where you got a nullpointer exception.
Which fit because the code:
new Framing()
Calls
add(new Stage())
which calls
player = new SPRITES();
Which calls
ImageIcon pine = new ImageIcon(this.getClass().getResource("Untitled.png"));
And this is where your nullpointer exception is. My guess is that getResources("Untitled.png"); returns null which then causes the ImageIcon constructor to throw a nullpointer exception.

How to get a rectangle to stop when you release a key that moves it?

I've created a JFrame with a rectangle in the center of it that moves when I press certain keys. It's all nice and dandy, but the rectangle keeps going when I release the keys. In fact, if I press a key multiple times, the rectangle accelerates. This is probably (definitely) because I'm using a timer to get around that pesky 0.5-second input delay when holding a key down.
I think I have to put something in the keyReleased() method, but I'm at a loss for what to put there. Any tips? Thanks.
PS: Please don't yell at me for not using key bindings. I know: they're better and stuff. But I'm focusing on key listeners at the moment.
Program:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.Timer;
#SuppressWarnings ("serial")
public class GameFrame extends JComponent implements KeyListener
{
static GameFrame gameFrame = new GameFrame();
public int x = 350;
public int y = 250;
public int keyCode;
public static void main (String[] args)
{
JFrame frame = new JFrame ("Java Game");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setSize (800, 600);
frame.setResizable (false);
frame.getContentPane().setBackground (Color.WHITE);
frame.getContentPane().add (gameFrame);
frame.addKeyListener (gameFrame);
frame.setVisible (true);
}
#Override
public void paintComponent (Graphics graphics)
{
super.paintComponent (graphics);
graphics.setColor (Color.BLACK);
graphics.fillRect (x, y, 100, 100);
}
public void keyPressed (KeyEvent event)
{
keyCode = event.getKeyCode();
new Timer (100, new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
if (keyCode == KeyEvent.VK_LEFT)
{
x--;
repaint();
}
if (keyCode == KeyEvent.VK_RIGHT)
{
x++;
repaint();
}
if (keyCode == KeyEvent.VK_UP)
{
y--;
repaint();
}
if (keyCode == KeyEvent.VK_DOWN)
{
y++;
repaint();
}
}
}).start();
}
public void keyReleased (KeyEvent event) {}
public void keyTyped (KeyEvent event) {}
}
Avoid KeyListener, seriously, they are more trouble than they are worth, use the key bindings API instead. How to Use Key Bindings
There are a number of ways you might achieve this. One of the better way is to use a indirect approach. That is, the user presses a key and you raise a flag to indicate which is pressed, they release the key, you reset the flag, indicating that the key is no longer pressed.
You then use some kind of update loop to change the position of the object based which keys are currently active.
But why go to so much hassle I hear you ask. When a user presses a key, they is a short delay between the first key press and repeated key notification (while the key is down, the OS will send you key events until it is released), this makes the movement look a little "staggered".
Instead, we raise flag and use a constant update loop to make changes to the state of the object based on the state of the flags, which smooths out the key events, for example...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test{
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public static class TestPane extends JPanel {
public enum HorizontalMovement {
NONE,
LEFT,
RIGHT
}
private HorizontalMovement horizontalMovement = HorizontalMovement.NONE;
private int xPos = 0;
public TestPane() {
addKeyPressedBinding("left.pressed", KeyEvent.VK_LEFT, new MoveHorizontialAction(HorizontalMovement.LEFT));
addKeyPressedBinding("right.pressed", KeyEvent.VK_RIGHT, new MoveHorizontialAction(HorizontalMovement.RIGHT));
addKeyReleasedBinding("left.relesed", KeyEvent.VK_LEFT, new MoveHorizontialAction(HorizontalMovement.NONE));
addKeyReleasedBinding("right.relesed", KeyEvent.VK_RIGHT, new MoveHorizontialAction(HorizontalMovement.NONE));
Timer timer = new Timer(40, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
switch (horizontalMovement) {
case LEFT:
xPos--;
break;
case RIGHT:
xPos++;
break;
}
if (xPos < 0) {
xPos = 0;
} else if (xPos + 50 > getWidth()) {
xPos = getWidth() - 50;
}
repaint();
}
});
timer.start();
}
protected void addKeyPressedBinding(String name, int keyCode, Action action) {
KeyStroke ks = KeyStroke.getKeyStroke(keyCode, 0, false);
addKeyBinding(name, ks, action);
}
protected void addKeyReleasedBinding(String name, int keyCode, Action action) {
KeyStroke ks = KeyStroke.getKeyStroke(keyCode, 0, true);
addKeyBinding(name, ks, action);
}
protected void addKeyBinding(String name, KeyStroke ks, Action action) {
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();
im.put(ks, name);
am.put(name, action);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
Rectangle box = new Rectangle(xPos, (getHeight() - 50) / 2, 50, 50);
g2d.setColor(Color.BLUE);
g2d.fill(box);
g2d.dispose();
}
protected void addKeyBinding(String left, int VK_LEFT, MoveHorizontialAction moveHorizontialAction) {
throw new UnsupportedOperationException("Not supported yet.");
}
protected class MoveHorizontialAction extends AbstractAction {
private HorizontalMovement movement;
public MoveHorizontialAction(HorizontalMovement movement) {
this.movement = movement;
}
#Override
public void actionPerformed(ActionEvent e) {
horizontalMovement = movement;
}
}
}
}

KeyPress won't work [duplicate]

This question already has answers here:
None of the event occurs
(3 answers)
Closed 8 years ago.
For some reason my program doesn't detect when I press a key even though it should be fine.
This is my code:
import javax.swing.*;
public class Frame {
public static void main(String args[]) {
Second s = new Second();
JFrame f = new JFrame();
f.add(s);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("Bouncing Ball");
f.setSize(600, 400);
}
}
This is the Second class:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import javax.swing.*;
public class Second extends JPanel implements ActionListener, KeyListener {
Timer t = new Timer(5, this);
double x = 0, y = 0, velX =0 , velY = 0;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Rectangle2D circle = new Rectangle2D.Double(x, y, 40, 40);
g2.fill(circle);
t.start();
}
public void actionPerformed(ActionEvent e) {
x += velX;
y += velY;
repaint();
}
public void up() {
velY = -1.5;
velX = 0;
}
public void down() {
velY = 1.5;
velX = 0;
}
public void keyPressed(KeyEvent e) {
int KeyCode = e.getKeyCode();
if (KeyCode == KeyEvent.VK_Z) {
up();
}
if (KeyCode == KeyEvent.VK_S) {
down();
}
}
public void keyTyped(KeyEvent e){
}
public void keyReleased(KeyEvent e){
}
}
How can I solve this problem?
The reason the timer doesn't appear to work is because, velX and velY are equals to 0, so, it doesn't increment anything. If you give them a value, it will animate.
The reason the Keys won't work is
because you haven't registered the KeyListener to the panel.
you need to setfocusable(true).
You need to call repaint() in the either the up() down() methods or in the keyPressed() method.
You need to increment/decrement the y value in the up() and down() methods.
Add the below constructor, add the repaint() to the keyPressed() and increment/decrement correctly and it works
public Second(){
setFocusable(true);
addKeyListener(this);
}
Add the above constructor. And the repaint in the keyPressed
public void keyPressed(KeyEvent e) {
int KeyCode = e.getKeyCode();
if (KeyCode == KeyEvent.VK_Z) {
up();
repaint();
}
if (KeyCode == KeyEvent.VK_S) {
down();
repaint();
}
}
Increment/Decrement
public void up() {
y -= 10;
}
public void down() {
y += 10;
}
Though this may work, it is recommended to use key bindings.
See How to use Key Bindings | The complete Creating a GUI with Swing trail
For testing purposes, add a listener to the JFrame and see if you get any response there. If you do, then it means that the JFrame isn't passing the events down to the Second, probably because the Second doesn't have focus.
You could also try calling requestFocusInWindow(): http://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html
A component generally gains the focus when the user clicks it, or when the user tabs between components, or otherwise interacts with a component. A component can also be given the focus programmatically, such as when its containing frame or dialog-box is made visible. This code snippet shows how to give a particular component the focus every time the window gains the focus:
//Make textField get the focus whenever frame is activated.
> f.addWindowFocusListener(new WindowAdapter() {
> public void windowGainedFocus(WindowEvent e) {
> s.requestFocusInWindow();
> } });
I would also recommend using more descriptive variables than s, f, and a more descriptive class name than Second.

Beginner Java Program-- cannot get key pressed to work

I am starting out with Java and I have a problem. When people click "a" on the java applet, I want it to draw a yellow rectangle and if they press anything else it draws a black rectangle but nothing happens.
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
public class guitarGame extends Applet implements ActionListener, KeyListener{
Timer timer = new Timer (1000, this);
String s = "";
char a;
int selection;
public void keyReleased(KeyEvent ae){}
public void keyPressed(KeyEvent ae){}
public void keyTyped(KeyEvent ae){
a = ae.getKeyChar();
}
public void init(){
addKeyListener(this);
}
public void actionPerformed (ActionEvent ae)
{
if (a == a)
{
selection = 1;
}
else{
selection = 2;
}
}
public void paint (Graphics g)
{
if (selection == 1){
g.setColor(Color.YELLOW);
g.fillRect(100,100,100,100);
}
if (selection == 2){
g.setColor(Color.YELLOW);
g.fillRect(100,100,100,100);
}
repaint();
}
}
Any help?
You are not doing anything with your KeyListener methods. Your ActionListener will never be called as it hasnt been registered with any component.
public void keyPressed(KeyEvent ae){
char keyChar = ae.getKeyChar();
if (keyChar == 'a' ) {
selection = 1;
} else {
selection = 2;
}
repaint();
}
Some suggestions:
Don't call repaint inside paint - This will cause the latter to loop indefinitely
Use enums rather than magic numbers for paint selection.
Call super.paint(g)
Consider using Swing which has much better performance over the old heavyweight AWT.

Sprite not moving on JPanel

I'm trying to get a simple sprite moving around my screen. I can't get my head around what is wrong with this code as i've followed instructions from a different source code but removed some complexity added from other features in other code.
Right now I'm just trying to get it to move around freely on the screen.
Later I intend for the animation to change while its moving.
Code for both classes is below.
package game;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
import javax.swing.Timer:
public class Game extends JPanel implements ActionListener {
int x, y, b_width, b_height;
Player player;
Timer timer;
public Game() {
addKeyListener(new KeyRecorder());
player = new Player();
timer = new Timer(5, this);
timer.start();
}
public void paint(Graphics g){
g.drawImage(player.image, player.getX(), player.getY(), this);
}
#Override
public void actionPerformed(ActionEvent e) {
player.move();
repaint();
}
public class KeyRecorder extends KeyAdapter{
public void keyPressed(KeyEvent e){
player.keyPressed(e);
repaint();
}
public void keyReleased(KeyEvent e){
player.keyReleased(e);
}
}
}
And the sprite:
package game;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
public class Player {
private String sprite = "sprite.png";
int x, y, dx, dy;
int width, height;
Image image;
public Player() {
ImageIcon ii = new ImageIcon(this.getClass().getResource(sprite));
image = ii.getImage();
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public void move() {
x = x + dx;
y = y + dy;
}
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
if(key == KeyEvent.VK_UP){
dy -= 3;
}
if(key == KeyEvent.VK_DOWN){
dy += 3;
}
if(key == KeyEvent.VK_LEFT){
dx -= 3;
}
if(key == KeyEvent.VK_RIGHT){
dx += 3;
}
}
public void keyReleased(KeyEvent e){
int key = e.getKeyCode();
if(key == KeyEvent.VK_UP){
dy -= 0;
}
if(key == KeyEvent.VK_DOWN){
dy += 0;
}
if(key == KeyEvent.VK_LEFT){
dx -= 0;
}
if(key == KeyEvent.VK_RIGHT){
dx += 0;
}
}
}
Custom painting is done by overriding the paintComponent() method, not the paint() method.
KeyEvents are only received by comonents with focus. It doesn't look like you panel has focus. In the constructor you need to add:
setFocusable(true);
I don't know a huge amount about swing, but are you calling "repaint()" for every frame you want drawn? For example
thePanel.repaint();
If it flickers, you may also want to set the panel to double buffered
setDoubleBuffered(true);
I hope those helps you anyway. If that doesn't answer your question, perhaps this will http://docs.oracle.com/javase/tutorial/uiswing/painting/ I don't have time to read through it but I did skim it and it looks good.

Categories

Resources