import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

    // Display layers of images in a JLayeredPane.
    // The names of the image files should be passed to
    // the constructor.
class SwingLayerFrame2 extends JFrame {
    public SwingLayerFrame2(String[] images) {
        final int width = 275, height = 200;
        setSize(width,height);
        final Container contents = getContentPane();

        // Place a JLayeredPane within the frame to manage the
        // layering.
        contents.add(getPane(),"Center");

        setupImages(images);

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(new QuitJButton());
        contents.add(buttonPanel,"South");
    }

        // Place a component holding each image into the layered pane.
    protected void setupImages(String[] images){
        final Container contents = getContentPane();
        final JLayeredPane pane = getPane();
        // Where to place the first image.
        final int initialX = 2, initialY = 2;
        // By how much to offset each image from the previous one.
        final int initialXOffset = 25;
        // Where to locate each image.
        Point initialLocation = new Point(initialX,initialY);
        // Which layer to place the next image in.
        int nextLayer = JLayeredPane.DEFAULT_LAYER.intValue();

        final int numImages = images.length;
        for(int i = 0; i < numImages; i++){
            final JComponent image = new JLabel(new ImageIcon(images[i]));
            // Without a layout manager, we set the required size
            // explicitly.
            image.setSize(image.getPreferredSize());
            image.setLocation(initialLocation);

            // Add it to its own layer.
            pane.add(image,new Integer(nextLayer));

            // Attach a mouse listener to it that will enable the
            // image to be moved around.
            new SwingMouseResponder(image);

            // Place the next in a different initial location and layer.
            initialLocation.x += initialXOffset;
            nextLayer++;
        }
    }

    protected JLayeredPane getPane(){
        return pane;
    }

    // The (layered) pane in which the labels will be stored.
    private final JLayeredPane pane = new JLayeredPane();
}
