2

I was wondering if there is a quick and easy way of trying to position an image at a particular position in GUI. In addition to this is it possible to put Jlables/Buttons on this picture. That picture should only form a section of the JFrame. I know you can use setbounds(width, height, width, height); but this takes alot of accuracy and very hard to get perfectly right.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Marcello
  • 423
  • 1
  • 5
  • 12
  • If picture should form only a section of `JFrame` then why dont you create an `JPanel` inside that frame and set its background image to that picture – exexzian Jan 08 '13 at 22:11
  • *"particular position in GUI."* By what formula or logic is this position determined? – Andrew Thompson Jan 09 '13 at 04:08

1 Answers1

3

Don't call setBounds, which implies that you are using a null layout. Always use a layout manager.

One approach would be to add a custom JPanel that overrides paintComponent and calls drawImage(Image, x, y, width, height, ...). JLabels/JButtons, etc. can still be added to the panel.

Don't forget to call super.paintComponent—this will ensure that your child components get painted.

Also, have a look at Background Panel.

Reimeus
  • 158,255
  • 15
  • 216
  • 276