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.

Recommended Answers

All 3 Replies

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.

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...

I used a JPanel.

I found the answer on another forum here: Click Here

Thank you very much to everyone nonetheless

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.