Solved

How to put a background image inside a JFrame

Posted on 2006-10-31
8
3,891 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

696 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