2

Here is what's inside my keyPressed:

public class Movie extends JFrame implements KeyListener {

public static Sprite star1 = new Sprite("Assets/star1.png");
public static Sprite star2 = new Sprite("Assets/star2.png");
public static Sprite star3 = new Sprite("Assets/star3.png");

public void init(){
    this.addKeyListener(this);
}
@Override
public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub

}
@Override
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

}
public void keyPressed(KeyEvent e) {

    System.out.println("KEY PRESSED: " + e.getKeyChar());
    animation window = new animation(500, 450); //length , height

    if (e.getKeyCode() == KeyEvent.VK_DOWN)
    {
        setFocusable(true);
        Movie.star1.setPosition( Movie.star1.getXposition() -100, 0);
        window.frameFinished();
    }
    else if  (e.getKeyCode() == KeyEvent.VK_UP)
    {
        setFocusable(true);
        Movie.star1.setPosition( Movie.star1.getXposition() +100, 0);
        window.repaint();
    }

}

My object does not move when the arrow keys are pressed.

All I want to know is - is this because I need to call the keyPressed(KeyEvent e) method in my main? When I do call it, I get an error that states:

cannot be resolved in a variable

The objects that I want to move are in a giant loop.

Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
bri
  • 21
  • 1
  • 4
  • What did you add the listener to? – user253751 May 03 '15 at 08:07
  • have you implemented KeyListener ? – svarog May 03 '15 at 08:10
  • Not directly related, but you might want to consider using Key Bindings instead. Read [this thread](http://stackoverflow.com/questions/15290035/key-bindings-vs-key-listeners-in-java) for a comparison between those and KeyListeners. – PakkuDon May 03 '15 at 08:10
  • and @immibis what do you mean by what I added listener to? I added it to my class? – bri May 03 '15 at 08:13
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) For Swing, we typically use [key bindings](https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) rather than the lower level `KeyListener`. – Andrew Thompson May 03 '15 at 08:13
  • Consider using [Key Bindings](https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) it solves the issue with focusability that `KeyListener` suffers from – MadProgrammer May 03 '15 at 08:15
  • @bri KeyListeners (and all other listeners) aren't magical and don't do anything by themselves. Typically you call `addKeyListener` on some Swing component, which tells *that component* to call your key listener when a key is pressed. (Note that it's the component that looks for key presses, not the listener). But I see you have `this.addKeyListener(this);` which answers my question (the thing you added the listener to is the window) – user253751 May 03 '15 at 08:15
  • Is your `init` method called? – user253751 May 03 '15 at 08:16
  • what is the "animation" object ? why do you always reassign it with a new keyword ? maybe the star1 changes it's position but you don't refresh the view properly ?! – svarog May 03 '15 at 08:16
  • @bri Note on coding style: you are mixing up **static** fields (star1, ...) with non-static elements. That is bad style; and will hurt you as soon as you try to extend your example to be more complex. Base rule: try to avoid using static. You need a static main method to start with; but anything else should not be using static. – GhostCat May 03 '15 at 08:17
  • @svarog - the animation is from a class in a file called animation.java (yes i know i did not capitalized the 'a'). this creates a window/canvas that pops up and shows the objects. – bri May 03 '15 at 08:21
  • @immibis no my init method is never called – bri May 03 '15 at 08:22
  • See [this](http://stackoverflow.com/questions/22741215/how-to-use-key-bindings-instead-of-key-listeners) on using key bindings instead. – user1803551 May 04 '15 at 13:20

1 Answers1

4

If you never add the key listener to some Swing component, then it will never receive events.

KeyListener itself isn't magical and doesn't listen for keypresses. What you do with a KeyListener is: you tell some other Swing component (like a window or a textbox) to call your KeyListener when a key is pressed. The component is what looks for keypresses, not the listener.

In your case, it looks like you meant to add the key listener to the window, with this.addKeyListener(this); (since in your case this is both a KeyListener and a JFrame).

However, if nothing calls your init method, then the code inside your init method (like any method) never runs, so the key listener doesn't get added to the window, so the window doesn't call it when a key is pressed!

One possible solution would be to make sure to call init after creating a new Movie (you haven't shown the code where that happens).

Another solution would be to use a constructor, instead of a method, like this:

public Movie() {
    this.addKeyListener(this);
}

- constructors run when the object is created, so that way, addKeyListener will be called whenever a Movie object is created, without the creator having to remember to call init.

user253751
  • 57,427
  • 7
  • 48
  • 90