アットウィキロゴ

あppぇ0401

import java.applet.*;
import java.awt.*;
public class apple0401 extends Applet
              implements Runnable{ //(a)
    Thread th=null;          //(b) スレッドオブジェクトの変数
    int x,y;                 //    描画位置
    public void init() {     //    ロード時の初期化メソッド
       x=20; y=40;           //(c) x,yの初期値
    } //end init
    public void start() {
       if (th == null) {        //(d)
          th=new Thread(this);  //(d) スレッドの作成
          th.start();           //(d) スレッドの開始
       }
    } //end start
    public void run() {
       while( true ) {               //(e) ループ
          try {                      //(f) 例外処理
             x+=20;                  //(g) x位置を増やす
             if (x >= 200) { x=20; } //(g) x位置を戻す
             repaint();              //(h) 作画
             th.sleep(1000);    //(i) 停止する時間(ミリ秒)
          } catch(InterruptedException e) { } // 例外処理
       } //end while
    } //end run
    public void stop() {
       if (th != null) {
          th.stop();  // スレッドの停止
          th=null;
       }
    } //end stop
    //=================== paintメソッド ====================
    public void paint(Graphics g) {
       g.setColor(Color.blue);
       g.fillOval(x,y,50,50);      // 円を描く
    } //end paint
} //end Thread1
最終更新:2010年09月16日 14:28