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

    // Display a layer of images in a JLayeredPane.
    // The names of the image files should be passed to
    // the constructor.
class SwingLayerFrame1 extends JFrame {
    public SwingLayerFrame1(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);

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

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

        // Locate the image in a JLabel.
        // The component should listen for mouse clicks and move
        // itself as a result.
        // This is used to illustrate some of the within-layer
        // methods of JLayeredPane.
    protected JComponent createImage(String imageFile){
        final JLabel label = new JLabel(new ImageIcon(imageFile));
        label.addMouseListener(new MouseAdapter(){
            public void mousePressed(MouseEvent e){
                final JLayeredPane pane = getPane();
                // Where is this component within its layer?
                int position = pane.getPosition(label);
                // Where is this position within the container.
                Point where = label.getLocation();
                if(position == 0){
                    // Shift it right and down slightly.
                    final int shiftOffset = 2;
                    where.x += shiftOffset;
                    where.y += shiftOffset;
                }
                else{
                    // Move it up one layer.
                    pane.setPosition(label,position-1);
                }
                label.setLocation(where);
            }
        });
        return label;
    }


    protected JLayeredPane getPane(){
        return pane;
    }

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