2

So I want to move the String towards the direction the user pushes but it's not working. It worked with the mouselistener so I thought this would be adequate. Should I be adding the listener to something else?

public class Snake extends JComponent implements KeyListener{

    private int x;
    private int y;
    private String s;

    public Snake(String s, int x, int y){
        this.s = s;
        this.x = x;
        this.y = y;
        addKeyListener(this);
    }

    @Override
    protected void paintComponent(Graphics g) {
        g.drawString(s, x, y);
    }


    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();
        switch(code) {
            case KeyEvent.VK_UP:
                y-=15;
            case KeyEvent.VK_DOWN:
                y+=15;
            case KeyEvent.VK_RIGHT:
                x+=15;
            case KeyEvent.VK_LEFT:
                x-=15;
        }
        repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}

public class Game {

    public static void main(String[] args){
        JFrame frame = new JFrame("Up Up And Away!");
        JComponent star = new Snake("*", 250, 100);
        frame.add(star);
        frame.setSize(500, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
codebot
  • 2,540
  • 3
  • 38
  • 89
Mark
  • 51
  • 1
  • 7
  • Avoid using `KeyListener` for this purpose, use [Key Bindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) instead – BackSlash Sep 29 '14 at 06:51
  • check this link might be helpful for you. http://stackoverflow.com/questions/286727/java-keylistener-for-jframe-is-being-unresponsive – Rana Pratap Singh Sep 29 '14 at 06:59
  • http://stackoverflow.com/a/26098190/1966247 here I have solved your question you check it :) , I hope you will get it helpful – Muhammad Sep 29 '14 at 11:02

2 Answers2

1

As someone else mentioned. It's probably better to use Key Bindings. However in your case, your focus is somewhere else, so your component just need to grab focus. Just add star.grabFocus(); in the main. Ie :

public static void main(String[] args){
    JFrame frame = new JFrame("Up Up And Away!");
    JComponent star = new Test2("*", 250, 100);
    frame.add(star);
    frame.setSize(500, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    star.grabFocus();
}

Then it should work.

Oliver Watkins
  • 12,575
  • 33
  • 119
  • 225
  • http://stackoverflow.com/questions/17680817/difference-between-requestfocusinwindow-and-grabfocus-in-swing#comment25758510_17680818 - relevant – Gorbles Sep 29 '14 at 10:25
  • I think he should use The keyListener used in his Snake class, like I did here http://stackoverflow.com/a/26098190/1966247 and it is working – Muhammad Sep 29 '14 at 10:57
  • thanks that worked, but for some reason it's only moving down or left but not right or up. I know that's not a problem with the keylistener but I'm not sure why those 2 directions work and the other two don't – Mark Sep 29 '14 at 13:21
  • i think you have forgot your breaks in your switch statement – Oliver Watkins Sep 29 '14 at 13:29
  • each 'case' needs to have a 'break' at the end of it, or you are going to get some funny results. – Oliver Watkins Sep 29 '14 at 13:29
  • @Dad Hey Dad, how about some up votes for the help I gave u last week. – Oliver Watkins Oct 06 '14 at 07:17
1

Your KeyListener is Applied on the JComponent but not on JFrame, when you run the program the JFrame has the focus(only JFrame can listen to the KeyEvents), add the following line to your Game class then it should work :)

frame.addKeyListener((KeyListener)star); 
Muhammad
  • 6,725
  • 5
  • 47
  • 54