-1

i was trying to move a rectange using KeyBinding but i guess i am unable to implement it properly. i am a beginner in java and i cannt find the error in this program So please help me out.

Thnx in advance!!

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

class Move {

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                abc();
            }
        });
    }

    private static void abc() {
        JFrame frame = new JFrame("moving object");
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1300, 600);
        frame.add(new P1());
    }
}

class P1 extends JPanel {

    int a, b;
    int x, y;
    Timer t = new Timer(10, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {//the coordinates of this rectangle 
            a += x;                                 //are mordified in every 10 millisec 
            b += y;                                 //x and y changes the direction of the moving
            repaint();                              //rect. 
        }
    });

    P1() {
        this.getInputMap().put(KeyStroke.getKeyStroke("VK_UP"), "doNothing1");
        this.getActionMap().put("doNothing1", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                x = 0;
                y = -1;
            }
        });
        this.getInputMap().put(KeyStroke.getKeyStroke("VK_LEFT"), "doNothing2");
        this.getActionMap().put("doNothing2", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                x = -1;
                y = 0;
            }
        });
        this.getInputMap().put(KeyStroke.getKeyStroke("VK_DOWN"), "doNothing3");
        this.getActionMap().put("doNothing3", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                x = 0;
                y = 1;
            }
        });
        this.getInputMap().put(KeyStroke.getKeyStroke("VK_RIGHT"), "doNothing4");
        this.getActionMap().put("doNothing4", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                x = -1;
                y = 0;
            }
        });
        t.start();
        setFocusable(true);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.fillRect(a, b, 10, 10);
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319

1 Answers1

4

You're using the wrong InputMap, and this is one major cause of your problems. The default InputMap, obtained when you don't specify a condtion, uses the WHEN_FOCUSED condition, and since JPanels don't allow focus by default (and really shouldn't be forced to get focus), this won't work. You instead will want to use the one with the WHEN_IN_FOCUSED_WINDOW condition. To do this, you must explicitly specify this. i.e.:

InputMap inputMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); // note condition
inputMap.put(KeyStroke.getKeyStroke("VK_UP"), "doNothing1");
// ....

This will work even if the bound component doesn't have the focus as long as it is held in a top-level Window that does have focus.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • @user2138054: please show the code for your latest attempt, using this suggestion above. Again, please only post well-formatted code, and let's see if we can help you. – Hovercraft Full Of Eels Mar 11 '13 at 17:47