So im making a gui, and i have a background image for it. i dont know how to make it set as the background, so any help with that would be good. an explination would also be good. also, after we get that image as a background, how do we get the image resize to the size of the window. such as
image.setSize(frame.getHeight(), frame.getWidth());
but i dont know if that would work. the image name is ABC0001.jpg and the frame name is frame. thanks!
Asked
Active
Viewed 1.2k times
0

davidfrancis
- 3,734
- 2
- 24
- 21

PulsePanda
- 1,806
- 10
- 33
- 56
-
Probable duplicate of: http://stackoverflow.com/questions/1064977/setting-background-images-in-jframe – davidfrancis Apr 11 '12 at 14:32
-
Please search SO first. Both these questions have been asked & answered on SO *many* times before. – Andrew Thompson Apr 11 '12 at 15:22
2 Answers
3
To get the image to resize, you can either use
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, getWidth(), getHeight(), this); // draw the image
}
or you can use a componentlistener, implemented like:
final Image img = ...;
ComponentListener cl = new ComponentAdapter() {
public void componentResized(ComponentEvent ce) {
Component c = ce.getComponent();
img = im1.getScaledInstance(c.getWidth(), c.getHeight(), Image.SCALE_SMOOTH);
}
};
Image quality will degrade over time with the second solution, so it is recommended that you keep the original and the copy separate.

ControlAltDel
- 33,923
- 10
- 53
- 80
-
One tip. Since `paintComponent` implies a `JComponent` or anything that inherits from it, and `JComponent` implements `ImageObserver`, change the `null` in draw to `this`.. Good call on 1) Mentioning other ways to resize images, though I believe `getScaledInstance` is largely abandoned in favor of even better ways to scale an image. 2) Making a note to keep the original. – Andrew Thompson Apr 11 '12 at 15:35
-
`RenderingHints` can be used to suggest a specific interpolation type in `drawImage()`. – trashgod Apr 11 '12 at 15:50
0
Create a class the extends JPanel. Have that class load the image by overriding paintComponent
class BackgroundPanel extends JPanel
{
Image img;
public BackgroundPanel()
{
// Read the image and place it in the variable img so it can be used in paintComponent
img = Toolkit.getDefaultToolkit().createImage("ABC0001.jpg");
}
public void paintComponent(Graphics g)
{
g.drawImage(img, 0, 0, null); // draw the image
}
}
Now that you have this class, simply add this to your JFrame (or whereever you want the background).
//create refrence if you want to add stuff ontop of the panel
private BackgroundPanel backGroundPanel;
//constructor
add(backGroundPanel, BorderLayout.CENTER);
The size of the background will fill the entire frame so no need to scale it unless you want it smaller

John Snow
- 5,214
- 4
- 37
- 44
-
I believe it's better to set your background image panel as the content pane, as then any further GUI components will appear "over" the background image. – davidfrancis Apr 11 '12 at 14:41
-
Suppose so. I just wrote this on the top of my head. Altho if you add the components on the backgroundPanel they will still appear "over" the backgroundimage. – John Snow Apr 11 '12 at 14:46
-
Yes they will, it's pretty good for off the top of your head, I had to search 8=} – davidfrancis Apr 11 '12 at 14:54
-
should be [g.drawImage(img, 0, 0, null);](http://stackoverflow.com/a/7724971/714968) – mKorbel Apr 11 '12 at 14:54
-
1@mKorbel should be `g.drawImage(img,0,0,getWidth(),getHeight(),this);` 1) "image resize" is achieved by the 3rd & 4th args 2) A `JPanel` is an `ImageObserver` - that is especially important if the code is using a non-blocking method (e.g. from `Toolkit`) to load it. – Andrew Thompson Apr 11 '12 at 15:28
-
@Andrew Thompson I'm want to sent little bit encrypted second chance for amend, :-) down_voter was faster :-) – mKorbel Apr 11 '12 at 15:30