0

I am currently creating a GUI for a game. I am currently loading an image using JLabel onto my JPanel, I was wondering whether it would be possible to add buttons on various parts of the image e.g. (5,7) and (12,12).

mKorbel
  • 109,525
  • 20
  • 134
  • 319
nsc010
  • 395
  • 3
  • 5
  • 12

1 Answers1

3

If you wish to have JButtons appear over your image, a better approach would be to override the paintComponent of the JPanel and use drawImage to draw the image similar to this example . This will allow you to add components to the container.

Absolute positioning(null layout) is generally frowned upon for setting component locations, however. This DragLayout should be a better substitute taking care of component sizing.

DragLayout was designed to replace a null layout. It will respect the location of a component. By default it will use the preferred size of the component to determines its size. Finally, it will automatically calculate the preferred size of the Container.

Community
  • 1
  • 1
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • @Reimeus `JLayeredPane` not useful in overlapping? – joey rohan Feb 02 '13 at 16:59
  • @joeyrohan This could be achieved using `JLayeredPane`, however IMHO using a background image thru `paintComponent` is a simpler solution (in terms of implementation & possible scaling). – Reimeus Feb 02 '13 at 17:02