タイトルアニメ(歓迎メッセージ)


ランダム位置に8回描画後,歓迎メッセージを7秒間表示。これを繰り返す。

translate()を使い描画原点の位置を変更する。

JavaApplet

ソースコード

import java.applet.*;
import java.awt.*;

public class ja30c extends Applet implements Runnable
{
  int w,h;        		//Appletの幅と高さ
  Thread myThread = null;
  Image pic;      		//背景画像
  Image of;       		//オフスクリーンイメージ
  Graphics g,ofg;   		//ofgはオフスクリーンイメージのGraphicsオブジェクト

  public void init(){
    w = size().width;   	//Appletの幅と高さを取得
    h = size().height;
    g = getGraphics();  	//AppletのGraphicsオブジェクトを取得
    pic = getImage(getCodeBase(),"ban_138.gif");   //画像の読み込み
    Font f = new Font("TimesRoman",Font.BOLD+Font.ITALIC,20);
    g.setFont(f);	
  }

  public void paint(Graphics g){
    of = createImage(w,h);		//オフスクリーンのImageオブジェクトを生成
    ofg = of.getGraphics();		//上のGraphicsオブジェクトの獲得
    ofg.drawImage(pic,0,0,w,h,this);  	//オフスクリーン上に描画

    g.drawImage(of,0,0,w,h,this);   	//オフスクリーンImageを画面に描画する
  }

  public void start(){
    if(myThread == null){		//スレッドの開始
	myThread = new Thread(this);
	myThread.start();
    }
  }

  public void stop(){			//スレッドを停止
    if(myThread != null){
	myThread.stop();
	myThread = null;
    }
  }

  public void run(){
    int n = 0;      //カウント変数
    int i = 1500;   //sleep間隔(ミリ秒)
    while(myThread != null){
      ranDraw();
      if(++n == 8){ //(前置きインクリメント)nを1増加していく。n=8になれば以下の処理
        n = 0;
        i = 7000;   //スレッドを7秒間停止し,msgDraw()を呼び出す
        msgDraw();
      }else{i = 1500;}

      try{
	Thread.sleep(i);		//iミリ秒間隔でスレッドを実行
      }catch(InterruptedException e){};
    }
  }

  void ranDraw(){        		//ランダム位置に楕円と文字列を描画
    g.drawImage(of,0,0,w,h,this);   	//背景画像を描画
    Graphics g2 = g.create();      //Graphicsオブジェクトをコピー (Graphicsオブジェクトの原点を変更するため)
    g2.translate((int)(Math.random() * (w -100)),(int)(Math.random() * (h -20)));
          //描画座標の原点(左上のxy座標)をランダムに変更する
          //描画楕円が右,下の画面からはみ出ないようにdarwOval()の幅と高さを減じて調整
    g2.setColor(Color.red);
    g2.drawOval(0,0,100,20);    	//楕円を描画
    g2.setColor(Color.blue);
    g2.drawString("Kodayan",15,15);   	//楕円内に文字列を描画
    g2.dispose();         	//コピーしたGraphicsオブジェクトを破棄し,システムリソースを解放する
  }

  void msgDraw(){       		//一定間隔で表示する歓迎メッセージ
    g.drawImage(of,0,0,w,h,this);
    g.setColor(Color.pink);
    g.fillOval(150,20,200,60);    	//塗り潰し楕円を描画

    g.setColor(Color.blue);
    g.drawString("歓迎!",170,50);  	//楕円内に文字列を描画
    g.drawString("おこしやす!",200,70);
  }
}

末尾