4

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.

Community
  • 1
  • 1
Raz Cohen
  • 540
  • 4
  • 15

1 Answers1

1
  • 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

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Thanks for the quick response! Is JPanel as efficient as Canvas in terms of time (I know JPanel is lightweight component while Canvas isn't so I'm not asking about memory) – Raz Cohen Oct 06 '13 at 20:45
  • there I can't see difference, in the case that there is/are some (J)Component(s) or Java2D, – mKorbel Oct 07 '13 at 06:01