hello i am using JPanel as my container of my frame then i really want to used a background picture in my Panel i really need help this is my code so far . this is the updates please check here is my code now
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class imagebut extends JFrame
{
public static void main(String args [])
{
imagebut w = new imagebut();
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
w.setSize(300,300);
w.setVisible(true);
}
public imagebut()
{
setLayout(null); // :-)
PicPanel mainPanel = new PicPanel("picturename.jpg");
mainPanel.setBounds(0,0,500,500);
add(mainPanel);
}
class PicPanel extends JPanel{
private BufferedImage image;
private int w,h;
public PicPanel(String fname){
//reads the image
try {
image = ImageIO.read(new File(fname));
w = image.getWidth();
h = image.getHeight();
} catch (IOException ioe) {
System.out.println("Could not read in the pic");
//System.exit(0);
}
}
public Dimension getPreferredSize() {
return new Dimension(w,h);
}
//this will draw the image
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(image,0,0,this);
}
}
}
There are any number of ways this might be achieved.
You Could...
Disclaimer
Cavet, using a
JLabel
for this purpose may result in the contents over spilling the continer, see below for more detailsCreate a
JLabel
, apply the image to it'sicon
property and set this as the frames content pane. You would then need to set the layout manager appropriately, asJLabel
doesn't have a default layout managerUpdate with full example
The problem with this is the
JLabel
won't resize the image when the frame is resizedWARNING - Using a
JLabel
could cause issues if the required space of the child components exceeds the size of the background image, asJLabel
does not calculate it's preferred size based on it's contents, but based on itsicon
andtext
propertiesYou Could...
Create a custom component, extending from something like
JPanel
and override it'spaintComponent
method, painting the background as you see fit.Take a look at Performing Custom Painting for more details.
This provides you with the ability to decide how best the image should be scaled when it's available space changes. While there are a number of ways this might be achived, you should read through The Perils of Image.getScaledInstance() to understand the pros and cons of them.
This raises a bunch of new questions, to you want to scale them and preserve the aspect ratio? If so, do you want to fit the image to available area or fill it (so it will always cover the available space)?
Take a look at Java: maintaining aspect ratio of JPanel background image for more details.
Other considerations
Images are generally best loaded through the
ImageIO
API, as it's capable of loading a wide range of images, but will also throw anIOException
when something goes wrong.See Reading/Loading an Image for more details.
The location of the image is also important. If the image is external to the application (somewhere on the file system), you can use
ImageIO.read(new File("/path/to/image"))
. However, if the the image is embedded within your application (stored within the Jar for example), you will need to use something more likeImageIO.read(getClass().getResource("/path/to/image"))
instead...For example...
Example
This example demonstrates the use of a custom component which acts as the background component. When the components size exceeds the size of the background image, the image is scaled up to fill the available content area.
It would be a simple matter to also have the image scaled down when the space decreases, but I deliberately decided to maintain the image at it's smallest size.
The example also makes use of a custom divide and conquer scaling algrotithm in order to generate a high quality scaled result.