Background image on JFrame?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 5005 Views - Last Post: 28 July 2011 - 01:49 PM Rate Topic: -----

#1 TheCompBoy  Icon User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 314
  • Joined: 21-April 11

Background image on JFrame?

Posted 27 July 2011 - 05:17 PM

If i would like to put a big background image on my JFrame and on top i whant to have all my components(For example buttons)
How would i do this? i don't see any solutions..
Is This A Good Question/Topic? 0
  • +

Replies To: Background image on JFrame?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Background image on JFrame?

Posted 27 July 2011 - 05:22 PM

Asked so many times I wrote a Snippet about it..
Easier if you fill your JFrame with a JPanel but you can modify that one, you will have to getContentPane()

http://www.dreaminco...snippet5599.htm
Was This Post Helpful? 0
  • +
  • -

#3 TheCompBoy  Icon User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 314
  • Joined: 21-April 11

Re: Background image on JFrame?

Posted 27 July 2011 - 05:36 PM

I will take a look on it. Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 TheCompBoy  Icon User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 314
  • Joined: 21-April 11

Re: Background image on JFrame?

Posted 27 July 2011 - 05:47 PM

I tried with your code snippet but now its only showing a little square image.. Not the whole background.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Background image on JFrame?

Posted 27 July 2011 - 05:57 PM

It should fill the JPanel, may be your JPanel does not fill the JFrame
Did you modified it to be used by a JFrame

Post your code if you want real answers, we are always gessing
Was This Post Helpful? 0
  • +
  • -

#6 TheCompBoy  Icon User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 314
  • Joined: 21-April 11

Re: Background image on JFrame?

Posted 27 July 2011 - 06:02 PM

Oh sorry, Here is the MainMenu class that extends the JPanel..
I pretty much copy / pasted it to see if it worked..
package galactic.adventures;

import javax.swing.*;

import java.awt.*;

public class MainMenu extends JPanel{
	
    // the image you want in the background
    private Image img;
   
    // constructor (you may have to overload the one you want to use
    // for example receiving a String as parameter for the a JLabel)
    
    
    MainMenu() {
            // read the background image
            ImageIcon background = new ImageIcon("data/Background/mainMenuBG.png");
            // extract the image out of it
            img = background.getImage();
    }
    
    public void paint(Graphics g) {
        // draw the background image
        // here we use the simplest g.drawImage() method that draw as much of the image it can
        // multiple different drawImage() methods permit to shrink or extend the image
        g.drawImage(img, 0, 0, this);
        // we declare the component as not opaque so
        setOpaque(false);
        // the standard paint() method won't redraw the background
        super.paint(g);
        setOpaque(true);
    }
    
}


And here is the main method where i create the JFrame..
    public static void main(String[] args) throws SlickException {
    	JFrame frame = new JFrame("Galactic Adventures");
    	JButton newGame = new JButton(newGameIcon);
    	MainMenu menu = new MainMenu();
    	menu.setLayout(new GridLayout(4,4));
        for(int i = 0; i < 4; i++) {
                for(int j = 0; j < 4; j++) {
                        JLabel label = new JLabel(" i = " + i + " j = " + j);
                        menu.add(label);
                }
        }
    	frame.setSize(800, 800);
    	frame.add(menu, BorderLayout.CENTER);
    	Container content = frame.getContentPane();
    	content.setLayout(new FlowLayout());
    	content.add(menu);
    	content.add(newGame);
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setVisible(true);
        }


Variables in the class where main method is:
	JFrame frame;
	JPanel panel;
	JButton newGame;
        public static ImageIcon newGameIcon = new ImageIcon("data/Text/newGame.png");
	public static ImageIcon mainMenuBackground = new ImageIcon("data/Background/mainMenuBG.png");

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Background image on JFrame?

Posted 27 July 2011 - 06:08 PM

A JComponent can only be in one container at a time
you add(menu) in two different places

Imagine the time we will have lost speculating if you haven't post your code :)
Was This Post Helpful? 0
  • +
  • -

#8 TheCompBoy  Icon User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 314
  • Joined: 21-April 11

Re: Background image on JFrame?

Posted 27 July 2011 - 06:09 PM

Am not sure i understand.. I see i did the content.add(menu); but where do i add it the second time?
Was This Post Helpful? 0
  • +
  • -

#9 TheCompBoy  Icon User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 314
  • Joined: 21-April 11

Re: Background image on JFrame?

Posted 27 July 2011 - 06:27 PM

Okay sorry i know what you mean now that i put it in the frame two times.. But i tried to remove the panel from the Container that didn't do any difference.. And then i put it back and remove the frame.add(menu, borderlayout); But still its the same thing as when i had them both..
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Background image on JFrame?

Posted 27 July 2011 - 06:34 PM

content.setLayout(new FlowLayout());

A FlowLayout will put as much as possible components using their preferredSize
you JPanel preferredSize, as it does not contain any JComponent, should probably be a little square

You are mixing FlowLayout and BorderLayout. Make up your mind
Was This Post Helpful? 0
  • +
  • -

#11 TheCompBoy  Icon User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 314
  • Joined: 21-April 11

Re: Background image on JFrame?

Posted 27 July 2011 - 06:40 PM

Alright.. But now its new problem.. Now i have it covering the whole JFrame but.. No picture is there and the whole screen is like a big button.. Am kinda new to JPanels so would be realy happy if you could help me out with this one and then i will check the documentation by my self and read a little bit about it..
Was This Post Helpful? -1
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Background image on JFrame?

Posted 27 July 2011 - 06:49 PM

Without code we are the blinds leading the blind (and the past shows that we cannot really trust your descriptions of what you did or not did)... no offense
Was This Post Helpful? 0
  • +
  • -

#13 TheCompBoy  Icon User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 314
  • Joined: 21-April 11

Re: Background image on JFrame?

Posted 27 July 2011 - 06:54 PM

Sorry
    public static void main(String[] args) throws SlickException {
    	JFrame frame = new JFrame("Galactic Adventures");
    	JButton newGame = new JButton(newGameIcon);
    	MainMenu menu = new MainMenu();
    	menu.setLayout(new GridLayout(4,4));
        for(int i = 0; i < 4; i++) {
                for(int j = 0; j < 4; j++) {
                        JLabel label = new JLabel(" i = " + i + " j = " + j);
                        menu.add(label);
                }
        }
    	frame.setSize(800, 800);
    	frame.add(menu, BorderLayout.CENTER);
    	Container content = frame.getContentPane();
    	//content.setLayout(new FlowLayout());
    	//content.add(menu);
    	content.add(newGame);
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setVisible(true);
        }

Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Background image on JFrame?

Posted 27 July 2011 - 07:03 PM

frame.add(menu, BorderLayout.CENTER);
Container content = frame.getContentPane();
content.add(newGame);


Use frame.add(xxx) or frame.getContentPane().add(xxx) but not both

Actually frame.add() is a shortcut for frame.getContentPane().add(xxx); that was added with JRE 1.5 because we (a lot of Java programmers) were fed up of doing getContentPane() to access the JFrame when the clause was not required for JPanel that permitted directly add(). Anyhow...

So your code is equivalent to:
frame.add(menu, BorderLayout.CENTER);
frame.add(newGame);  // which will used BorderLayout.CENTER by default


or
Container content = frame.getContentPane();
content.add(menu, BorderLayout.CENTER);
content.add(newGame);



In both cases, newGame replaces menu in the CENTER region of the BorderLayout
Was This Post Helpful? 0
  • +
  • -

#15 TheCompBoy  Icon User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 314
  • Joined: 21-April 11

Re: Background image on JFrame?

Posted 27 July 2011 - 07:08 PM

Alright, Thanks! But now i am just curious about this for loop i got in the method.. What does it actualy do cus im getting things like i = 2j = 1 and stuff getting printer to the picture..
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3