I'm working on a new Java Desktop Application using the GUI and I want to add a JPanel
with a background image on it. A JLabel
with an image won't work because I'm going to be adding different labels on top of the background panel.
So I came up with this example and I want to implement it.
http://www.coderanch.com/how-to/java/BackgroundImageOnJPanel
class BackgroundPanel extends JPanel
{
Image image;
public BackgroundPanel()
{
try
{
image = javax.imageio.ImageIO.read(getClass().getResource("Test.gif"));
}
catch (Exception e) { e.printStackTrace(); /*handled in paintComponent()*/ }
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (image != null)
g.drawImage(image, 0,0,this.getWidth(),this.getHeight(),this);
}
}
How do I add and draw that panel on my JFrame
? I'm trying to add it to the mainPanel
but I don't even know if it's working. How can I call or where is the paintComponent
method being called?
bgPanel = new BackgroundPanel();
bgPanel.setOpaque(false);
mainPanel.add(bgPanel, new java.awt.GridBagConstraints());
BackgroundPanel
should return the size of the image as the minimum preferred size ( @Override .. getPreferredSize()
). Possibly larger if it has components (the super size is larger).
A
JLabel
with an image won't work because I'm going to be adding different labels on top of the background panel.
Funny you should mention that. It is possible (not necessarily recommended) to set the layout of a JLabel
, then add other JComponent
objects to it. It is important to call setOpaque(false)
on any JPanel
objects or the BG image won't show through.
This demonstrates both approaches.
import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class BackGroundImage {
private JComponent ui = null;
BackGroundImage() {
initUI();
}
public void initUI() {
if (ui != null) {
return;
}
ui = new JPanel(new GridLayout(0, 1));
ui.setBorder(new EmptyBorder(4, 4, 4, 4));
try {
BufferedImage bi1 = ImageIO.read(
new URL ("http://i.stack.imgur.com/OVOg3.jpg"));
BackgroundPanel bp = new BackgroundPanel(bi1);
ui.add(bp);
bp.setLayout(new GridBagLayout());
JLabel l1 = new JLabel("Using BackgroundPanel");
Font f = l1.getFont();
l1.setFont(f.deriveFont(32f));
l1.setForeground(Color.RED);
bp.add(l1);
BufferedImage bi2 = ImageIO.read(
new URL ("http://i.stack.imgur.com/lxthA.jpg"));
JLabel l = new JLabel(new ImageIcon(bi2));
ui.add(l);
l.setLayout(new GridBagLayout());
JLabel l2 = new JLabel("Using JLabel");
l2.setFont(f.deriveFont(32f));
l2.setForeground(Color.RED);
l.add(l2);
} catch (Exception e) {
e.printStackTrace();
}
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
BackGroundImage o = new BackGroundImage();
JFrame f = new JFrame("BackgroundPanel");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
class BackgroundPanel extends JPanel {
BufferedImage image;
public BackgroundPanel(BufferedImage image) {
this.image = image;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);
}
@Override
public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
int w = d.width > image.getWidth() ? d.width : image.getWidth();
int h = d.height > image.getHeight() ? d.height : image.getHeight();
return new Dimension(w, h);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.