package javaapplication7;
import java.awt.*;
import java.applet.Applet;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class Main extends Applet implements Runnable {
JFrame f = new JFrame("Load Image Sample");
Image frame[ ];
Thread thd;
int num;
Thread runner = null;
Image image;
Graphics graphics;
public Main() throws IOException {
frame = new Image[20];
thd = null;
num = 0;
for (int i = 0; i < frame.length; i++)
frame[i] = new ImageIcon("C:/Documents and Settings/lupohsun/My Documents/My Pictures/測試/"+(i+1)+".jpg").getImage();
start();
}
public void start() {
if ( runner == null ) {
runner = new Thread( this );
runner.start();
}
run();
}
public void stop() {
if ( runner != null && runner.isAlive() )
runner.stop();
runner = null;
}
public void run() {
while ( runner != null ) {
image = frame[num];
try {
Thread.sleep(10);
} catch ( InterruptedException e ) {
}
repaint();
if(num < 19)
num = num + 1;
else
stop();
}
}
public void paint(Graphics g) {
update(g);
}
public void update( Graphics g ) {
g.drawImage(image, 0, 0, this);
}
public Dimension getPreferredSize(){
if (frame[num] == null)
return new Dimension(100,100);
else
return new Dimension(frame[num].getWidth(null), frame[num].getHeight(null));
}
public static void main(String[] args) throws IOException {
Main main = new Main();
main.f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
main.f.add(main);
main.f.pack();
main.f.setVisible(true);
}
}
留言列表