There are a few basic problems...
JPanel
by default, uses aFlowLayout
FlowLayout
uses the preferred size of the component to determine how to layout the components it is managing- Your components don't override
getPreferredSize
which means they will return0x0
- A
JPanel
is not focusable by default, so adding aKeyListener
to it and requesting focus will do very little. You should consider using Key Bindings instead JLabel
provides support for drawingIcon
. You can wrap anImage
in aImageIcon
add use this with aJLabel
to paint images more simplyImageIcon("resources/ball.png")
suggests that the images in question are stored as files on the disk, but the path suggests that they may be embedded resources.ImageIcon(String)
expects the reference to be a file on the disk, which would be relative to the execution context of the program
Depending on what you want to achieve, you may wish to consider writing your own layout manager which you can use to control the location of the child components OR write a single "game surface" which you use custom painting to paint the game state