You can't add images directly into a JPanel anyway.
Just create a BufferedImage with type TYPE_INT_ARGB and then draw the images on this BufferedImage.
Then add the image to a JLabel and place it in your panel.
Read the tutorials:
http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html
http://java.sun.com/docs/books/tutorial/2d/images/drawimage.html
http://java.sun.com/docs/books/tutorial/2d/images/drawonimage.html
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class OverlappingImages extends JPanel {
BufferedImage[] images;
Point loc = new Point(100,100);
public OverlappingImages() {
createImages();
setBackground(Color.black);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for(int j = 0; j < images.length; j++)
g.drawImage(images[j], loc.x, loc.y, this);
}
public Dimension getPreferredSize() {
return new Dimension(300,300);
}
private void createImages() {
int w = 100, h = 100;
int type = BufferedImage.TYPE_INT_ARGB_PRE;
Rectangle clip = new Rectangle(w/3, h/3);
images = new BufferedImage[4];
for(int j = 0; j < images.length; j++) {
images[j] = new BufferedImage(w, h, type);
Graphics2D g2 = images[j].createGraphics();
int x = (j%2 == 0) ? 0 : w-clip.width;
int y = (j/2 == 1) ? h-clip.height : 0;
clip.setLocation(x, y);
g2.setClip(clip);
g2.setPaint(getGradient(j, clip, w, h));
g2.fillRect(0,0,w,h);
g2.dispose();
}
}
private GradientPaint getGradient(int index, Rectangle r, int w, int h) {
Color c1 = Color.blue;
Color c2 = Color.orange;
float x1 = (index%2 == 0) ? r.width : w-r.width;
float y1 = (index/2 == 1) ? h-r.height : r.height;
float x2 = (index%2 == 0) ? 0 : w;
float y2 = (index/2 == 1) ? h : 0;
return new GradientPaint(x1, y1, c1, x2, y2, c2);
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new OverlappingImages());
f.pack();
f.setLocation(200,200);
f.setVisible(true);
}
}