import java.applet.Applet;
import java.awt.*;
public class Study7 extends Applet implements Runnable {
Thread t1;
int x=0;
Image i;
public void init(){
i = getImage(getDocumentBase(),"sample1.gif");
}
public void start() {
if (t1 == null) {
t1 = new Thread(this);
t1.start();
}
}
public void run() {
while(true) {
repaint();
try {
t1.sleep(100);
} catch (InterruptedException e) { }
}
}
public void paint(Graphics g) {
g.drawImage(i,x,30,this);
x = x - 10;
if (x < -300) x = size().width;
}
public void stop() {
if (t1 != null) {
t1.stop();
t1 = null;
}
}
}
ソースファイルの説明
import java.applet.Applet;
import java.awt.*;
public class Study7 extends Applet implements Runnable {
Thread t1;
int x=0;//初期値を代入しておきます。
Image i;
public void init(){
i = getImage(getDocumentBase(),"sample1.gif");
}
public void start() {
if (t1 == null) {
t1 = new Thread(this);
t1.start();
}
}
public void run() {
while(true) {
repaint();
try {
t1.sleep(100);
} catch (InterruptedException e) { }
}
}
public void paint(Graphics g) {
g.drawImage(i,x,30,this);
x = x - 10;
if (x < -300) x = size().width;
}
public void stop() {
if (t1 != null) {
t1.stop();
t1 = null;
}
}
}
コメント文が書いていないところは既に前の時間で説明してあります。
8時間目に行く。
shimizu@eces.numazu-ct.ac.jp