/*---------------------------------- ex12_5b.java アプレット:アニメーション(改良版) -----------------------------------*/ import java.applet.*; import java.awt.*; /* */ public class ex12_5b extends Applet implements Runnable { Thread thd = null; Color col; int x, y, wid, hei, t, red, grn, blu; double velo, grav, theta; public void init() { // 初期化処理 x = 5; y = 550; // 初期位置(x,y) velo = 0.5; // 初速(速度: velocity) theta = Math.PI*3/8;// 投射角θ grav = 9.8; // 重力加速度: gravitational acceleration t = 1; // 経過時間 wid = 50; hei = 50; red = 0; grn = 0; blu = 255; col = new Color(red, grn, blu); thd = new Thread(this); // 新しいスレッドを作る thd.start(); // スレッド開始 } public void update(Graphics g) { // 再描画で背景クリアをしない paint(g); } public void paint(Graphics g) { // 描画処理 g.setColor(col); g.drawOval(x, y, wid, hei); } public void run() { // スレッド開始したときに呼び出されるメソッド for (t = 1; t < 75; t++) { // 無限ループ x += (int)(velo * t * Math.cos(theta)); y -= (int)(-1/2 * grav * t * t + velo * Math.sin(theta) * t); wid += 20*Math.cos(Math.PI/7*t); hei += 7*Math.sin(Math.PI/11*t); red += 6; blu -= 6; col = new Color(red, grn, blu); t++; repaint(); // アプレット強制再描画メソッド(結果としてpaint()が実行される) try { thd.sleep(20); // スレッドを一時停止(停止時間:20ミリ秒) } catch (InterruptedException e) { } } } }