1

I'm tring to make a simple Card Game in Java using Swing.

So far the problem i've ran into is: I'm using a JLayeredPane with a null layout to draw cards in layers.

But it seems i can't do getHeight or getWidth of the JLayeredPanel to draw on the layeredPane Bounds.

I tried also to print out the layeredPane.getWidth or layeredPane.getHeight and it prints out zero.

frost
  • 66
  • 7
  • 2
    You may want to override [`getPreferredSize()`](http://stackoverflow.com/q/7229226/230513) or try a different [approach](https://stackoverflow.com/a/20256794/230513). – trashgod Jun 04 '17 at 12:22
  • 1
    You're likely calling those methods before the layered pane has been rendered, when the sizes *are* in fact 0. The solution is as @trashgod mentions: either try using preferred sizes, or make sure you only get the sizes after rendering, such as within a component listener. – Hovercraft Full Of Eels Jun 04 '17 at 12:26
  • So i made a little timer that prints out each second. It indeed is the problem that @Hovercraft Full Of Eels stated, When i paint my components, the size of the layeredpane is 0. How should i approach this problem? How can i be sure to draw the rectangles/images after the window has been rendered? – frost Jun 04 '17 at 13:33
  • I initialized the shapes after the construction of the object, now it shows the size properly. Need to implement further and ill send feedback, thanks – frost Jun 04 '17 at 13:41

0 Answers0