Solved

How to put a background image inside a JFrame

Posted on 2006-10-31
8
3,881 Views
Last Modified: 2008-01-09
In a small application, I have a non-resizable JFrame containing a JPanel, containing other things, ecc. ecc.

I would like to know how I can do, if it's possible, to put a background image in my application.
0
Comment
Question by:Emanuele_Ciriachi
8 Comments
 
LVL 9

Accepted Solution

by:
matthew016 earned 86 total points
ID: 17841026
public class MyPanel extends JPanel
{
  // all the rest of your code...

  public void paintComponent( Graphics g )
  {
    g.drawImage(backgroundImg.getImage(), 0, 0, this);
    super.paintComponent( g ) ;
  }
}
0
 
LVL 1

Author Comment

by:Emanuele_Ciriachi
ID: 17841223
How to create a Graphics object from a locale file with this image? Graphics is abstract, perhaps I should use Graphics2D?
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 85 total points
ID: 17841495
>>How to create a Graphics object

You don't create Graphics objects - the system does. You just *use* them in the appropriate override, as matthew016 showed
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:Emanuele_Ciriachi
ID: 17841543
I don't understand the comment (probably due to other vital informations you are assuming I know, but I don't).

I have the background image in a file, but in the code snippet posted I don't see where the .jpg or .gif file is used.
0
 
LVL 14

Assisted Solution

by:StillUnAware
StillUnAware earned 85 total points
ID: 17841846
Use this then:

import javax.swing.ImageIcon;

public class MyPanel extends JPanel
{
  // all the rest of your code...

  public void paintComponent( Graphics g )
  {
    ImageIcon ii = new ImageIcon("image.jpg");
    g.drawImage(ii.getImage(), 0, 0, this);
    super.paintComponent( g ) ;
  }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17842185
Not good to (or try to) create the image each time in paintComponent. You can do


public class MyPanel extends JPanel
{
  ImageIcon ii = new ImageIcon("image.jpg");

  public void paintComponent( Graphics g )
  {
    g.drawImage(ii.getImage(), 0, 0, this);
    super.paintComponent( g ) ;
  }
}
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now