Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6969875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:38:14+00:00 2026-05-27T16:38:14+00:00

I’m trying to write a game applet that responds to keys typed by the

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:38:15+00:00Added an answer on May 27, 2026 at 4:38 pm

    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");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.