using the java swing how can i put a background image to my application in a way that the image will cover the frame .
ps : I'm new to the java so maybe my question is a silly one but i will appreciate any help .

jackbauer24 commented: crawl before you walk... the question is not silly :) +3

Recommended Answers

All 14 Replies

That is pretty easy. An example is here:-

import java.awt.*;
import javax.swing.*;

class <your className> {
Image im = new ImageIcon("Yourimage.jpeg/png").getImage();
JFrame frame;
public static void main(String[] args)  {
<your className> y = new <your className>();
y.go();
}
public void go() {
/*normal GUI code would appear here
* with one important difference
* making your own panel :)
*/
frame = new JFrame();
<yourPanel> p = new <yourPanel>();
frame.getContentPane().add(p);
}
/* now for the panel...
make a class that extends JPanel
and override one method - paintComponent();
never call this method yourself,
however you can call repaint();
which will ultimately lead to paintComponent(); 
called
*/
class <yourPanel> extends JPanel {
public void paintComponent(Graphics g) {
g.drawImage(frame, 0, 0, this);
/* parameter explained--
1. parent component
2. x distance
3. y distance
4. image display unit
*/
}
}
}

thanks for the help thepythonguy
but i had a problem with your code in the section that talk about the frame
this is the code where a i is the error

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package imagebackground;

import java.awt.*;
import javax.swing.*;


public class ImageBackGround {

    public ImageBackGround(){
        Image im = new ImageIcon("lebanon ball.jpg").getImage();
        JFrame frame = new JFrame();
    
    }   
    
    
    
    public static void main(String[] args) {
        // TODO code application logic here
     ImageBackGround y = new ImageBackGround();
     y.go();
    }
    public void go(){
   
        JPanelImg p = new JPanelImg();
      // here i had an error 
        frame.getContentPane().add(p);
        
        
        
    }
    class JPanelImg extends JPanel{
        @Override
        public void paintComponent(Graphics g){
            g.drawImage(frame, 0, 0, this);
            
        }
    }
}

well my question where is file with name lebanon ball.jpg placed, because I miss there

- rellative path to the resources in the Java package,
- absolute path to the HDD,
- URL from to the Http

Please post the full text of the error message.

You declare im and frame inside the constructor ImageBackGround(), so they can't be accessed from anywhere else. If you want them available to all the methods of that class then declare them in the class but outside any one method.

commented: You almost solved the whole issue! +3

a) Sorry jouj, my code is wrong. The first parameter should be the image(in this case im). First parameter is always the image.
1. JamesCherrill is right. Just because you have marked your constructor public doesn't mean that the variables will be public. Declare them(im and frame) as instance variables.


So your new code:-

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package imagebackground;

import java.awt.*;
import javax.swing.*;


public class ImageBackGround {
[U]JFrame frame;
Image im;[/U]
    public ImageBackGround(){
[U]im = new ImageIcon("lebanon ball.jpg").getImage();
frame = new JFrame();[/U]
    
    }   
    
    
    
    public static void main(String[] args) {
        // TODO code application logic here
     ImageBackGround y = new ImageBackGround();
     y.go();
    }
    public void go(){
   
        JPanelImg p = new JPanelImg();
      // here i had an error 
        frame.getContentPane().add(p);
        
        
        
    }
    class JPanelImg extends JPanel{
        @Override
        public void paintComponent(Graphics g){
            g.drawImage([U]im[/U], 0, 0, this);
            
        }
    }
}

If my code still has problems, contact me.

Two small suggestions... new ImageIcon("file name") is certainly convenient, but it throws no Exceptions if it fails, thus leading to confusing errors later on when you try to use the image. It's a good idea to check the resulting Image, eg im.getWidth(null)>0 and issue a diagnostic if necessary

If the frame is resizable you may prefer to scale the image to fit the frame by using im.getScaledInstance using the panel's current width and height.

Hey friend it's very easy just use drag and drop tool of Netbean and look in the properties you get the properties as setbackground image set your image and do whatever you want.......

Actually being able to code it without the help of an IDE is a better way to nourish your knowledge in java programming :)

I agree with Zeroliken. What's the fun if you let somebody else do your work?

first i 'm sorry for the late reply
second your new code is right but i still have a little problem when it's executed nothing appear in the frame.
(sorry if i 'm bothering you with my questions but I'm new as i already said )
and thank you .

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package imagebackground;

import java.awt.*;
import javax.swing.*;

/**
 *
 * @author GGE
 */
public class ImageBackGround {

    JFrame frame;
    Image im;
            
    public ImageBackGround(){
         im = new ImageIcon("lebanon ball.jpg").getImage();
         frame = new JFrame();
    
    }   
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
     ImageBackGround y = new ImageBackGround();
     y.go();
    }
    
    public void go(){
   
        JPanelImg p = new JPanelImg();
        frame.getContentPane().add(p);
        
        
        frame.add(p);
        frame.setSize(800, 400);
         frame.setLocationRelativeTo(null);
         frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
         frame.setVisible(true);
    }
    class JPanelImg extends JPanel{
        @Override
        public void paintComponent(Graphics g){
            g.drawImage(im, 0, 0, this);
                   
        }
        
    }
    
}

Post the code with the problem.

Are you sure you have the correct path to the image?

yes my friend the image is within the folder of the application.
the problem is that when i debug the source code every thing is good but the result is nothing (nothing is display on the screen )

The code you posted works for me. It shows the image in the upper left corner.
The most frequent problem with no image being shown is that the path to the image or the name of the image is wrong.

Be a part of the DaniWeb community

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