Solved

How to put a background image inside a JFrame

Posted on 2006-10-31
8
3,890 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

740 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