Sunil Shahi
Sunil Shahi

Reputation: 653

transparent JButtons in JFrame with a Background Image

I am making a Class that extends JFrame and have my custom background image on it..

I have two problems..

1) I want my background image to stay fixed sized that covers whole screen when maximized. How can I do that?

2) I want to add a transparent button and panels on the frame that does not disturb my background. Is there any easy way to do that?

help will be greatly appreciated... thank you

Upvotes: 1

Views: 1165

Answers (1)

Devon_C_Miller
Devon_C_Miller

Reputation: 16518

Load the image into a BufferedImage.

Add a ComponentListener to determine when the frame is resized. Use the frame size to calculate scaling and call BufferedImage.getScaledImage(xScale, yScale) to obtain a scaled image.

In your class you should be overriding paintBackground() to do the painting. Just call g.drawImage(scaledImage, getWidth(), getHeight(), this) to paint the image.

Any components you add to the frame need to call setOpaque(false) so the background gets painted underneath them.

Upvotes: 1

Related Questions