📄 imageanimation.java
字号:
// ImageAnimation.java: Display a sequence of images
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class ImageAnimation extends JApplet implements ActionListener
{
private Image imageToDisplay;
protected Image imageArray[]; // Hold images
protected int numOfImages = 52, // Total number of images
currentImageIndex = 0, // Current image subscript
sleepTime = 100; // Milliseconds to sleep
protected int direction = 1; // Image rotating direction
// Text field for receiving speed
protected JTextField jtfSpeed = new JTextField(5);
// Button for reversing direction
JButton jbtReverse = new JButton("Reverse");
// Initialize the applet
public void init()
{
// Load the image, the image files are named
// L1 - L52 in Images directory
imageArray = new Image[numOfImages];
for (int i=0; i<imageArray.length; i++ )
{
imageArray[i] = getImage(getDocumentBase(),
"Images/L" + (i+1) + ".gif" );
}
// Panel p to hold animation control
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(new JLabel("Animation speed in millisecond"),
BorderLayout.WEST);
p.add(jtfSpeed, BorderLayout.CENTER);
p.add(jbtReverse, BorderLayout.EAST);
// Add the image panel and p to the applet
getContentPane().add(new PlayImage(), BorderLayout.CENTER);
getContentPane().add(p, BorderLayout.SOUTH);
// Register listener
jtfSpeed.addActionListener(this);
jbtReverse.addActionListener(this);
}
// Handle ActionEvent
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == jtfSpeed)
{
sleepTime = Integer.parseInt(jtfSpeed.getText());
}
else if (e.getSource() == jbtReverse)
{
direction = -direction;
}
}
class PlayImage extends JPanel implements Runnable
{
private Thread thread = null;
// Determine the thread status
protected boolean suspended = false;
// Constructor
public PlayImage()
{
// Start with the first image
currentImageIndex = 0;
// Start the thread
thread = new Thread(this);
thread.start();
// Set line border on the panel
setBorder(new LineBorder(Color.red, 1));
}
public void start()
{
resume();
}
public void stop()
{
suspend();
}
public synchronized void resume()
{
if (suspended)
{
suspended = false;
notify();
}
}
public synchronized void suspend()
{
suspended = true;
}
public void destroy()
{
thread = null;
}
public void run()
{
while (true)
{
imageToDisplay =
imageArray[currentImageIndex%numOfImages];
// Make sure currentImageIndex is nonnegative
if (currentImageIndex == 0) currentImageIndex = numOfImages;
currentImageIndex = currentImageIndex + direction;
repaint();
try
{
thread.sleep(sleepTime);
waitForNotificationToResume();
}
catch (InterruptedException ex)
{
}
}
}
// Wait for notification to resume
private synchronized void waitForNotificationToResume()
throws InterruptedException
{
while (suspended)
wait();
}
// Display an image
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (imageToDisplay != null)
{
g.drawImage(imageToDisplay, 0, 0, getSize().width,
getSize().height, this);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -