📄 logoanimator.java
字号:
// Animation of a series of images.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LogoAnimator extends JPanel implements ActionListener {
private final static String IMAGE_NAME = "deitel"; // base image name
protected ImageIcon images[]; // array of images
private int totalImages = 30; // number of images
private int currentImage = 0; // current image index
private int animationDelay = 50; // millisecond delay
private int width; // image width
private int height; // image height
private Timer animationTimer; // Timer drives animation
// initialize LogoAnimator by loading images
public LogoAnimator()
{
images = new ImageIcon[ totalImages ];
// load images
for ( int count = 0; count < images.length; ++count )
images[ count ] = new ImageIcon( getClass().getResource(
"images/" + IMAGE_NAME + count + ".gif" ) );
// this example assumes all images have the same width and height
width = images[ 0 ].getIconWidth(); // get icon width
height = images[ 0 ].getIconHeight(); // get icon height
}
// display current image
public void paintComponent( Graphics g )
{
super.paintComponent( g );
images[ currentImage ].paintIcon( this, g, 0, 0 );
// move to next image only if timer is running
if ( animationTimer.isRunning() )
currentImage = ( currentImage + 1 ) % totalImages;
}
// respond to Timer's event
public void actionPerformed( ActionEvent actionEvent )
{
repaint(); // repaint animator
}
// start or restart animation
public void startAnimation()
{
if ( animationTimer == null ) {
currentImage = 0;
animationTimer = new Timer( animationDelay, this );
animationTimer.start();
}
else // continue from last image displayed
if ( ! animationTimer.isRunning() )
animationTimer.restart();
}
// stop animation timer
public void stopAnimation()
{
animationTimer.stop();
}
// return minimum size of animation
public Dimension getMinimumSize()
{
return getPreferredSize();
}
// return preferred size of animation
public Dimension getPreferredSize()
{
return new Dimension( width, height );
}
// execute animation in a JFrame
public static void main( String args[] )
{
LogoAnimator animation = new LogoAnimator(); // create LogoAnimator
JFrame window = new JFrame( "Animator test" ); // set up window
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
Container container = window.getContentPane();
container.add( animation );
window.pack(); // make window just large enough for its GUI
window.setVisible( true ); // display window
animation.startAnimation(); // begin animation
} // end method main
} // end class LogoAnimator
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -