📄 movingpenguin.java
字号:
import java.awt.*;
import java.applet.*;
public class MovingPenguin extends Applet implements Runnable {
int frameNumber = 0;
int delay;
int allFrame = 10;
Thread animatorThread;
Image offImage = null;
Graphics offGraphics = null;
Image images[];
MediaTracker tracker;
AudioClip sound;
public void init()
{
String str;
int fps = 10;
//How many milliseconds between frames?
str = getParameter("fps");
try {
if (str != null)
{
fps = Integer.parseInt(str);
}
} catch (Exception e) {}
delay = (fps > 0) ? (1000 / fps) : 100;
//Load Images
images = new Image[allFrame];
tracker = new MediaTracker(this);
for( int i = 1; i <= allFrame; i++ )
{
images[i-1] = getImage(getCodeBase(), "T" + i + ".gif");
tracker.addImage(images[i-1], i-1);
}
try{
tracker.waitForAll();
}catch(Exception e){}
try{
sound = getAudioClip(getDocumentBase(),"bark.au");
}catch(Exception e){}
}
public void start()
{
//Start animating!
if (animatorThread == null) {
animatorThread = new Thread(this);
}
animatorThread.start();
sound.loop();
}
public void stop()
{
//Stop the animating thread.
animatorThread = null;
offImage = null;
offGraphics = null;
sound.stop();
}
public void run()
{
//Just to be nice, lower this thread's priority
//so it can't interfere with other processing going on.
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
//Remember the starting time.
long startTime = System.currentTimeMillis();
//This is the animation loop.
while (Thread.currentThread() == animatorThread)
{
//Advance the animation frame.
frameNumber++;
//Display it.
repaint();
//Delay depending on how far we are behind.
try {
startTime += delay;
Thread.sleep(Math.max(0,
startTime-System.currentTimeMillis()));
} catch (InterruptedException e) {
break;
}
}
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
Dimension d = size();
if( offImage == null || offGraphics == null )
{
offImage = createImage(d.width, d.height);
offGraphics = offImage.getGraphics();
}
offGraphics.setColor( getBackground() );
offGraphics.fillRect(0, 0, d.width, d.height);
offGraphics.setColor( Color.black );
//Paint the image onto the buffer image
int no;
no = frameNumber % allFrame;
offGraphics.drawImage(images[no], 0, 0, this);
//Paint the Image onto the screen.
g.drawImage(offImage, 0, 0, this);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -