MovingLabelspublic class MovingLabels extends JApplet implements ActionListener
Fields Summary |
---|
int | frameNumber | Timer | timer | boolean | frozen | JLayeredPane | layeredPane | JLabel | bgLabel | JLabel | fgLabel | int | fgHeight | int | fgWidth | int | bgHeight | int | bgWidth | static String | fgFile | static String | bgFile |
Methods Summary |
---|
public void | actionPerformed(java.awt.event.ActionEvent e)
//Advance animation frame.
frameNumber++;
//Display it.
fgLabel.setLocation(
((frameNumber*5)
% (fgWidth + bgWidth))
- fgWidth,
(bgHeight - fgHeight)/2);
| void | buildUI(java.awt.Container container, java.awt.Image bgImage, java.awt.Image fgImage)
final ImageIcon bgIcon = new ImageIcon(bgImage);
final ImageIcon fgIcon = new ImageIcon(fgImage);
bgWidth = bgIcon.getIconWidth();
bgHeight = bgIcon.getIconHeight();
fgWidth = fgIcon.getIconWidth();
fgHeight = fgIcon.getIconHeight();
//Set up a timer that calls this object's action handler
timer = new Timer(100, this); //delay = 100 ms
timer.setInitialDelay(0);
timer.setCoalesce(true);
//Create a label to display the background image.
bgLabel = new JLabel(bgIcon);
bgLabel.setOpaque(true);
bgLabel.setBounds(0, 0, bgWidth, bgHeight);
//Create a label to display the foreground image.
fgLabel = new JLabel(fgIcon);
fgLabel.setBounds(-fgWidth, -fgHeight, fgWidth, fgHeight);
//Create the layered pane to hold the labels.
layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(
new Dimension(bgWidth, bgHeight));
layeredPane.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (frozen) {
frozen = false;
startAnimation();
} else {
frozen = true;
stopAnimation();
}
}
});
layeredPane.add(bgLabel, new Integer(0)); //low layer
layeredPane.add(fgLabel, new Integer(1)); //high layer
container.add(layeredPane, BorderLayout.CENTER);
| public void | init()
//Invoked only when run as an applet.
Image bgImage = getImage(getCodeBase(), bgFile);
Image fgImage = getImage(getCodeBase(), fgFile);
buildUI(getContentPane(), bgImage, fgImage);
| public static void | main(java.lang.String[] args)
Image bgImage = Toolkit.getDefaultToolkit().getImage(
MovingLabels.bgFile);
Image fgImage = Toolkit.getDefaultToolkit().getImage(
MovingLabels.fgFile);
final MovingLabels movingLabels = new MovingLabels();
JFrame f = new JFrame("MovingLabels");
f.addWindowListener(new WindowAdapter() {
public void windowIconified(WindowEvent e) {
movingLabels.stopAnimation();
}
public void windowDeiconified(WindowEvent e) {
movingLabels.startAnimation();
}
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
movingLabels.buildUI(f.getContentPane(), bgImage, fgImage);
f.setSize(500, 125);
f.setVisible(true);
movingLabels.startAnimation();
| public void | start()
startAnimation();
| public synchronized void | startAnimation()
if (frozen) {
//Do nothing. The user has requested that we
//stop changing the image.
} else {
//Start animating!
if (!timer.isRunning()) {
timer.start();
}
}
| public void | stop()
stopAnimation();
| public synchronized void | stopAnimation()
//Stop the animating thread.
if (timer.isRunning()) {
timer.stop();
}
|
|