I want to add icon over icon.
Here is the code that is using Graphics.drawImage()
to draw a image in existing Graphics
of the JLabel
within overridden paintComponent()
method.
For more info read inline comments.
Sample code:
// back ground image
URL url1 = new URL(
"https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQu3FFM1kR-aeYFjJIqebgusVZNM6uG-a0F4Z_0IPopEqfSZwzBBA");
final BufferedImage bg = ImageIO.read(url1);
// foreground image
URL url2 = new URL("https://cdn1.iconfinder.com/data/icons/supermariopack/Mario.png");
final BufferedImage fg = ImageIO.read(url2);
// re size the image
final BufferedImage scaled = new BufferedImage(fg.getWidth() / 2, fg.getHeight() / 2,
BufferedImage.TYPE_INT_RGB);
Graphics g = scaled.getGraphics();
g.drawImage(fg, 0, 0, scaled.getWidth(), scaled.getHeight(), null);
g.dispose();
// create a JLabel with back ground image
final JLabel label = new JLabel(new ImageIcon(bg)) {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// draw the foreground image
g.drawImage(scaled, 50, 50, null);
}
};
JOptionPane.showMessageDialog(null, label);
The above code is modified code of this post How to draw an image over another image?.
Screenshot: