Solved

How to put a background image inside a JFrame

Posted on 2006-10-31
8
3,889 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 …
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 …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

828 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