5

I saw many Ludum Dare games, which are written in Java (using its standard library). I checked the code of one or two of them and the thing that I saw is in their Game class, they extend Canvas.

So my question is why would they extend Canvas, not JPanel?

DMGregory
  • 134,153
  • 22
  • 242
  • 357
sn0k3
  • 59
  • 1
  • 3
  • I believe, when using a Canvas, you can use a custom BufferedStrategy instead of the JPanels default doubleBufferStrategy – PixelKicker Dec 29 '15 at 10:44

3 Answers3

1

Without seeing the actual code, it's difficult to say. However, I would guess that they are using both Canvas and JPanel somewhere (or at least some form of JPanel, e.g. JFrame or Applet etc..) and simply extending the game class from Canvas to make it more portable, i.e. so it can be added to a JPanel (or whatever) later.

Steve Smith
  • 180
  • 7
1

Very interesting question. I developed a game with JPanel in Linux and at the end discovered that it was not functioning in Windows (getBufferStrategy null). Switching to canvas fixed the problem for me.

Pikalek
  • 12,372
  • 5
  • 43
  • 51
frhack
  • 111
  • 3
1

As mentioned in the API

Canvas extends Component

Component:

A component is an object having a graphical representation that can be displayed on the screen and that can interact with the user.

Canvas

A Canvas component represents a blank rectangular area of the screen onto which the application can draw or from which the application can trap input events from the user.

JPanel extends JComponent

JComponent

The base class for all Swing components except top-level containers.

JLabel

JPanel is a generic lightweight container.

summary

if you do not implement a Swing Application then you forgo JComponents and stick with the basic Components.

Martin Frank
  • 472
  • 3
  • 12