Raz Cohen
Raz Cohen

Reputation: 540

Canvas and InputMap

I'm building a 2d game in Java and I decided to use Canvas on which I would display the images relevant to the current frame. I'm using Canvas because I've heard it is more efficient in terms of time than JPanel. Is it true?

Also, I would like to add some input to the game through key bindings since key listeners could cause focus issues and are lower level construct: keylistener not working after clicking button (see answer). Is there a way to use key bindings with a Canvas? Or I would simply use KeyListener.

Upvotes: 4

Views: 382

Answers (1)

mKorbel
mKorbel

Reputation: 109823

  • you can't add KeyBindings to awt.Canvas, there isn't method implemented in API

  • you can to add only KeyListener

  • change your decision and to use JPanel/JComponent

Upvotes: 1

Related Questions