1

I'm trying to write a game applet that responds to keys typed by the player. I'm trying to use key bindings to accomplish this. But I can't get it to work. The applet (what little of it there is, at the moment) seems to display correctly in Appletviewer, but nothing happens when I press keys. I haven't been able to test it in a browser, as it doesn't always display correctly in a browser.

I'm running Sun Java 6 on Ubuntu. I managed to find mention of a Ubuntu bug where iBus would block keyboard input to applets. However, I don't have iBus running, and I've been able to use keyboard input with other applets (not written by me).

Here is the code so far

public class AlxTestVersion extends JApplet {
   private GridBagLayout layout;
   private GridBagConstraints layoutConstraints;
   private JPanel mainPanel;

   public void init() {
      this.setLayout ( new FlowLayout(FlowLayout.LEFT) );
      //Main frame.
      mainPanel = new JPanel();
      layout = new GridBagLayout();
      layoutConstraints = new GridBagConstraints();
      layoutConstraints.anchor = GridBagConstraints.NORTHWEST;
      layoutConstraints.fill = GridBagConstraints.NONE;
      mainPanel.setLayout(layout);
      mainPanel.setBackground(Color.pink);
      getContentPane().add(mainPanel);
      //Map display
      JPanel leftPanel = new JPanel();
      GlobalData.mainMap = new MapCanvas(9);
      addComponent(GlobalData.mainMap, 0, 0, 1, 1);
      /*
         Define other components...
      */
   }

public class MapCanvas extends JPanel {
   private int tileSize;
   private int mapTileWidth;
   private int mapOffset;
   private int mapDim;

   private MapSquare screenTiles[];

   public void paintComponent(Graphics g) {
      super.paintComponent(g);
      ImageIcon testImage = new ImageIcon("tiles/test_land.gif");
      int x,y;
      for (x=0;x<mapTileWidth;x++) {
         for (y=0;y<mapTileWidth;y++) {
            g.drawImage(testImage.getImage(), x*tileSize + mapOffset, y*tileSize + mapOffset, this);
         }
      }
   }

   public MapCanvas(int numTiles) {
      //Set up window
      tileSize = 48;
      mapTileWidth = numTiles;
      mapOffset = 4;
      mapDim = (tileSize * mapTileWidth) + (2 * mapOffset);
      Dimension dim = new Dimension(mapDim, mapDim);
      this.setPreferredSize(dim);
      this.setMinimumSize(dim);
      this.setMaximumSize(dim);
      this.setLayout( new GridLayout(numTiles, numTiles, 0, 0) );
      this.setBackground(Color.black);
      screenTiles = new MapSquare[numTiles^2];

      //Map-related actions
      InputMap im = this.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
      ActionMap am = this.getActionMap();

      AbstractAction north = new AbstractAction() {
         public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, "Just for testing", "testing",
            JOptionPane.PLAIN_MESSAGE);
         }
      };
      am.put("North", north);

      im.put(KeyStroke.getKeyStroke('2'), "North");
      im.put(KeyStroke.getKeyStroke('i'), "North");
   }
}

About the only difference I can find between what I've used and working examples found in various places is they add the keystroke to the inputmap before mapping the keystroke to the action. I tried switching the order, but it didn't seems to have any effect.

Can anyone see what I'm doing wrong here? I just know I'm missing something obvious.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Leonide
  • 235
  • 3
  • 11

1 Answers1

1

Your code doesn't work for me either (on a mac), until I click inside the window. Adding the following as the last thing in init() seems to help (but is not totally reliable):

GlobalData.mainMap.requestFocus();

Your applet window probably does not have the focus when you press the keys.

Try adding this to your init():

GlobalData.mainMap.addFocusListener(new FocusDebugger("canvas"));
this.addFocusListener(new FocusDebugger("applet"));

Here's FocusDebugger:

public static class FocusDebugger implements FocusListener {
    private final String name;

    public FocusDebugger(String name) {
        this.name = name;
    }

    public void focusGained(FocusEvent focusEvent) {
        System.out.println(name + ".focusGained");
    }

    public void focusLost(FocusEvent focusEvent) {
        System.out.println(name+".focusLost");
    }
}
jackrabbit
  • 5,525
  • 1
  • 27
  • 38
  • 1
    Thank you very much. After playing with it a bit, I discovered getContentPane().requestFocus() also seems to work. – Leonide Dec 22 '11 at 02:17