2

Situation: I have a JFrame and I have managed to import the GIF and display it in the Main Panel however it moves all of my other panels down, causing my GUI to be in disarray.

Question: How should I go about making the GIF the background instead of adding to the Main Panel Like I am doing?

Note: The code is simply making a Jframe ,setting it's details, adding the panels then displaying the GIF as a Jlabel. If you need the code I can display it below.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
user3341249
  • 73
  • 1
  • 11
  • You mean something like this: http://stackoverflow.com/questions/10836832/show-an-animated-bg-in-swing – pL4Gu33 Feb 22 '14 at 17:22
  • possible duplicate of [How to set Jframe Background Image in GroupLayout Java](http://stackoverflow.com/questions/15493219/how-to-set-jframe-background-image-in-grouplayout-java) – Paul Samsotha Feb 22 '14 at 17:25
  • Sweet! Thanks, the posts I found all had faulty code. – user3341249 Feb 22 '14 at 17:28

3 Answers3

2

There are several ways you might achieve this...

You could...

Use a JLabel, setting the label's icon property to the reference of the image. You could then apply a layout manager to the label and set the label as main container for the rest of you components by simply adding them to it as usual...

IconImage image = ...;
JLabel background = new JLabel(image);
background.setLayout(...);
// You could set the frames content pane to the label and keep
// working as per normal, adding components to the frame
setContentPane(background);
// or, simply add components to it directly like any other container
background.add(...);

The great thing about this, is if the GIF is animated, it will contain to play as normal...nice side effect. The draw back is, JLabel won't resize the image for you...

You could...

Paint the image to the background of the component, like JPanel and then add your remaining components to it.

This would allow you to apply special effects and or resize the image if you wanted to do. If it's an animated GIF however, this makes it extremely more complex, as you will need to animate the frames yourself.

Take a look at...

For more details

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

UPDATED ANSWER

Now in accordance with Oracle's Swing UI-guidlines as correctly indicated by @MadProgrammer

Best way to add a background is to simply paint it. Create a BackgroundPanel (extends JPanel) and override paintComponent(...);

public class BgPanel extends JPanel
{ 
    //Overload and paint your background
    public void paintComponent(Graphics g)
    {
        //create image object, or more preferably do that in the BgPanel constructor
        g.drawImage(image, 0, 0, null);
    }
}

In accordance with Oracle's guidelines for custom rendering when using Swing.

Markus Millfjord
  • 707
  • 1
  • 6
  • 26
  • 1
    There are at least 3 things wrong with this suggest. 1- You are overriding `paint`, which is never recommended. 2- You are calling `paint` after performing some custom painting, which is likely to just fill the components surface with the background color, completely painting over everything you just did. 3- Unless you are intending to do some sort of adjustment or effect to the image, it's a lot of work for little gain – MadProgrammer Feb 22 '14 at 20:58
  • As for 1 - I don't agree. Overriding *paint* or paintCompnent (depending on what you want to do), is as far as I know the way to go of you want to make changes that has to do with how a component is painted (?). 2 - True, unless you setOpaque accordingly on the panel, which I forgot in my "concept-code" suggestion. 3 - Yes, I agree. But it works and I've use the approach in multiple projects without issues, at the cost of having to make a background class -- however, if made more generic, it's an easy task that can be reutilized elsewhere in your project(s). – Markus Millfjord Feb 22 '14 at 21:22
  • Overriding paint works in that it achieves painting but it's [improper for Swing](http://www.oracle.com/technetwork/java/painting-140037.html#swing_summary). – Radiodef Feb 22 '14 at 21:47
  • `paintComponent` paints the background, no need to make it transparent, in the context is would be the right choice and is generally the recommended approach. Children can be painted without the parent's paint method being called, causing all sorts of nasty effects. Point 3 focus on the fact there is already a ready made component that can be used to achieve the same result and has lots of inbuilt functionality. Override `paint` is dangerous and leads to more problems than it solves and is generally discouraged for multiple reasons... – MadProgrammer Feb 22 '14 at 22:03
  • Gottcha; "Extensions of Swing components which wish to implement their own paint code should place this code within the scope of the paintComponent() method ( not within paint())." I stand corrected. Thx! ;) – Markus Millfjord Feb 22 '14 at 22:10
  • `g.drawImage(image, 0, 0, null);` should be `g.drawImage(image, 0, 0, this);` since `JPanel` **is an** `ImageObserver`.. – Andrew Thompson Feb 23 '14 at 01:22
0

You can do it in the style.css so this would be for the whole body.

body { background-position: center center; background-image: url('../images/MyGif.gif'); background-repeat: no-repeat; }

or create a class for it

.frame{ background-position: center center; background-image: url('../images/MyGif.gif'); background-repeat: no-repeat; }