Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Background image in a pane

Status
Not open for further replies.

jeremytaffy

Technical User
Sep 6, 2001
75
US
How can I set the background image of a JPanel using a gif or jpg file?
 
Yes but you have to take care of drawing it.
You need to subclass JPanel and override the
paint method. Also if it's a tile you need to take
care of drawing it multiple times to cover the whole
panel. If this is the case this should be done using
double buffering. (ie Drawing in memory and then putting
the result on the panel)

Simplest.....

public class myPanel extend JPanel
{
public Image back;
public myPanel ()
{
super();
MediaTracker tracker;
tracker=new MediaTracker(this);
back = Toolkit.getDefaultToolkit().getImage("somefile.jpg");
tracker.addImage(back, 1);
try
{
tracker.waitForAll();
if (tracker.isErrorAny())
System.out.println("Error loading Image");
}
repaint();
}
public static void paint(Graphics g)
{
g.drawImage(0,0,this);
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top