Next: 4 スレッドを用いたアニメーション
Up: ソフトウェア第三 講義資料 配列,スレッド応用,アニメーション,シミュレーション
Previous: 2.4 CurveTest.java
waitとnotifyはスレッドの中断と再開の処理を
実現することができる.
図4のような画面の画像が連続的に表示
されているとして,Suspendボタンで表示を止めて,
Resumeで再開するようなプログラムを下に示す.
(参考文献,Steven Holzner著,武藤健志監修,トップスタジオ訳,Java プログ
ラミング 2nd Edition Black Book, インプレス,2001.
の第13章マルチスレッドより.)
// whirl3.java
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
/*
<APPLET
CODE = whirl3.class
WIDTH = 300
HEIGHT = 300 >
</APPLET>
*/
public class whirl3
extends Applet
implements ActionListener, Runnable
{
Image whirlImages[] = new Image[4];
Image nowImage;
int whirlIndex = 0;
Thread whirlThread;
Button suspendButton, resumeButton;
boolean animateFlag = true;
boolean goFlag = true;
public void init()
{
whirlImages[0] =
getImage(getCodeBase(), "whirl1.gif");
whirlImages[1] =
getImage(getCodeBase(), "whirl2.gif");
whirlImages[2] =
getImage(getCodeBase(), "whirl3.gif");
whirlImages[3] =
getImage(getCodeBase(), "whirl4.gif");
suspendButton = new Button("Suspend");
add(suspendButton);
suspendButton.addActionListener(this);
resumeButton = new Button("Resume");
add(resumeButton);
resumeButton.addActionListener(this);
}
public synchronized
void actionPerformed(ActionEvent e)
{
if(e.getSource() == suspendButton){
goFlag = false;
}
if(e.getSource() == resumeButton){
goFlag = true;
notify();
}
}
public void start()
{
whirlThread = new Thread(this);
whirlThread.start();
}
public void stop()
{
animateFlag = false;
}
public void run()
{
while(animateFlag){
nowImage = whirlImages[whirlIndex++];
if(whirlIndex > 3) whirlIndex = 0;
repaint();
try {
Thread.sleep(200);
synchronized(this){
while(!goFlag)
wait();
}
}
catch(InterruptedException e) { }
}
}
public void paint (Graphics g)
{
if(nowImage != null)
g.drawImage(nowImage, 10, 10, this);
}
public void update(Graphics g)
{
g.clipRect(10, 10, 280, 280);
paint(g);
}
}
generated through LaTeX2HTML. M.Inaba 平成18年5月7日