0

Hello Good day. I have an interesting question as it relates to adding a background image to a JPanel (or whichever component is best for what I'm trying to achieve. Hopefully I'll get some help here.)

I have a large picture that I want to use as a background image and I want it to be the background for my JFrame. The only thing is that I don't want the image to resize when the JFrame resizes. I want the image to remain centered in relation to the JFrame's position and size, but I just don't want the view of the image to shrink in size. Any suggestions please?

I'll gladly clarify where needed.
Here's a pictorial illustration.
fafdf4ae4ef08b0459251934ca44ae00

3
Contributors
3
Replies
18
Views
3 Years
Discussion Span
Last Post by CoilFyzx
Featured Replies
  • 1

    either don't allow the JFrame to be resizable, or, every time it is resized, re-calculate the size/position of the image and re-paint it. Read More

  • depends on how you are doing it - do you override paintComponent or is it a JLabel with an ImageIcon? If it's paintComponent then just draw the image at (panel width - image with)/2, (panel height - image height)/2. Your paintComponent will be called after any resize and will always … Read More

1

either don't allow the JFrame to be resizable, or, every time it is resized, re-calculate the size/position of the image and re-paint it.

1

depends on how you are doing it - do you override paintComponent or is it a JLabel with an ImageIcon?
If it's paintComponent then just draw the image at (panel width - image with)/2, (panel height - image height)/2. Your paintComponent will be called after any resize and will always center the image.
If it's a JLabel then it also depends on the layout manager...

0

I used a JPanel.

I found the answer on another forum here: Click Here

Thank you very much to everyone nonetheless

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.