super("LayeredPaneDemo2");
//Create and load the duke icon
final ImageIcon icon = new ImageIcon("images/dukeWaveRed.gif");
//Create and set up the layered pane
layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(new Dimension(300, 310));
layeredPane.setBorder(BorderFactory.createTitledBorder(
"Move the Mouse to Move Duke"));
layeredPane.addMouseMotionListener(new MouseMotionAdapter() {
final int XFUDGE = 40;
final int YFUDGE = 57;
public void mouseEntered(MouseEvent e) {
dukeLabel.setLocation(e.getX()-XFUDGE, e.getY()-YFUDGE);
}
public void mouseMoved(MouseEvent e) {
dukeLabel.setLocation(e.getX()-XFUDGE, e.getY()-YFUDGE);
}
});
layeredPane.setLayout(new GridLayout(2,3));
//Add several overlapping, colored labels to the layered pane
//using absolute positioning/sizing.
for (int i = 0; i < layerStrings.length; i++) {
JLabel label = createColoredLabel(layerStrings[i],
layerColors[i]);
layeredPane.add(label, new Integer(i));
}
//Create and add the Duke label to the layered pane
dukeLabel = new JLabel(icon);
dukeLabel.setBounds(15, 225,
icon.getIconWidth(),
icon.getIconHeight());
layeredPane.add(dukeLabel, new Integer(2), 0);
//Add control pane and layered pane to frame
Container contentPane = getContentPane();
contentPane.setLayout(new BoxLayout(contentPane,
BoxLayout.Y_AXIS));
contentPane.add(Box.createRigidArea(new Dimension(0, 10)));
contentPane.add(createControlPanel());
contentPane.add(Box.createRigidArea(new Dimension(0, 10)));
contentPane.add(layeredPane);