 // 図形の回転 [Thread4.java]
 import java.applet.*;
 import java.awt.*;
 public class Thread4 extends Applet implements Runnable{
    Thread th=null;          //    スレッドオブジェクトの変数
    int ang2;
    int ang=90;              //(a) 角度の初期値
    int x0=100;              //(a) 中心座標
    int y0=100;              //(a) 中心座標
    public void init() {     //    ロード時の初期化メソッド
       setForeground(Color.blue);  // 描画カラー
    } //end init
    public void start() {
       if (th == null) {
          th=new Thread(this);  // スレッドの作成
          th.start();           // スレッドの開始
       }
    } //end start
    public void run() {
       while( true ) {
          try {
             ang-=20;           // 20度回転
             ang2=ang-90;       //(b) 90度回転した位置
             repaint();         // 描画
             th.sleep(1000);    // sleepする時間(ミリ秒)
          } catch(InterruptedException e) { } // 例外処理
       } //end while
    } //end run
    public void stop() {
       if (th != null) {
          th.stop();  // スレッドの停止
          th=null;
       }
    } //end stop
    //================ グラフィックスの表示 ================
    public void paint(Graphics g) {
       int [] x=new int[3];     // 三角形のx座標
       int [] y=new int[3];     // 三角形のy座標
       final double RAD=Math.PI/180.0; // ラジアンに変換
       x[0]=x0;                 // 中心座標
       y[0]=y0;                 // 中心座標
       x[1]=    (int)(70*Math.cos(RAD*ang)+x0);  //(c)
       y[1]=200-(int)(70*Math.sin(RAD*ang)+y0);  //(c)
       x[2]=    (int)(30*Math.cos(RAD*ang2)+x0); //(c)
       y[2]=200-(int)(30*Math.sin(RAD*ang2)+y0); //(c)
       g.fillPolygon(x,y,3);    // 三角形を描く
    } //end paint
 } //end Thread4
