Solved

How to put a background image inside a JFrame

Posted on 2006-10-31
8
3,884 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
json example 39 132
GUI builder for Eclipse? 8 27
why cannot we forward request once the response is committed 2 33
eclipse apache tomcat admin console 49 55
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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

920 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

16 Experts available now in Live!

Get 1:1 Help Now