| 0.5秒間隔で,ランダム色で変化する文字列 |
import java.applet.*;
import java.awt.*;
public class ja01c extends Applet
{
String s = "KodayanHomePage"; //表示する文字列
char sc[]; //文字列の配列
int xpos[]; //各文字を描画するx座標の配列
FontMetrics fm; //フォントメトリックス
public void init(){
setBackground(Color.white); //Appletの背景を白色に
setFont(new Font("TimesRoman",Font.BOLD,30)); //フォントの設定
fm = getFontMetrics(getFont()); //使用フォントからフォントメトリックスを取得
sc = new char[s.length()]; //文字列の配列を生成
s.getChars(0,s.length(),sc,0); //文字列の配列に,各文字を格納する
xpos = new int[s.length()]; //x座標の配列を生成
for(int i = 0;i < s.length();i++){
xpos[i] = fm.charsWidth(sc,0,i) + 20;
}
}
public void paint(Graphics g){
for(int i = 0;i < s.length();i++){
g.setColor(new Color((float)Math.random(),(float)Math.random(),(float)Math.random()));
//ランダムに色を設定
g.drawChars(sc,i,1,xpos[i],35); //文字列の配列から,i番目の文字を描画
}
repaint(500); //0.5秒間隔で再描画する
}
}
末尾