| ・HTMLのパラメータを5個まで設定でき,最大5個のスクロール文字列を同時にスクロールできる。
|
|
import java.applet.*;
import java.awt.*;
import java.util.*;
public class ja205 extends Applet implements Runnable
{
Font f = new Font("TimesRoman",Font.BOLD,26);
Thread td; //スレッドオブジェクト
ja205a scrText[] = new ja205a[5]; //ja205aクラス(スクロール文字列)のインスタンス配列
int width,height; //Appletの幅と高さ
int n = 0;
int j = 0;
public void init(){
Dimension d = size(); //Appletのサイズを取得
width = d.width;
height = d.height;
String scrT[] = new String[5]; //パラメーターから得たスクロール文字列を格納する配列
int scrX[] = new int[5]; //X軸方向の移動量の配列
int scrY[] = new int[5]; //Y軸方向の移動量の配列
int scrXPOS[] = new int[5]; //スクロール開始位置のx座標の配列
int scrYPOS[] = new int[5]; //スクロール開始位置のy座標の配列
int scrR[] = new int[5]; //R値(赤色)の配列
int scrG[] = new int[5]; //G値(緑色)の配列
int scrB[] = new int[5]; //B値(青色)の配列
Color scrColor[] = new Color[5]; //色を格納する配列
int i;
for(i=1;i<=5;i+=1){
String param = getParameter("set" + i); //HTMLのパラメーターから取得
if(param == null)break; //パラメーターが設定されていない場合,ループから抜ける
StringTokenizer sToken = new StringTokenizer(param,","); //取得したパラメータ文字列をカンマ(,)で区切る
//StringTokenizerクラスは,区切り文字を設定し,区切られた文字列を取り出すのに利用
String token = sToken.nextToken(); //最初の文字列を取得
scrT[n] = token;
token = sToken.nextToken(); //カンマ(,)で区切られた次の文字列を取得(この場合,スクロール方向を指定する数字)
try{
j = Integer.valueOf(token).intValue(); //文字列型の数字を数値型に変換
}catch(NumberFormatException e){
}
switch(j){
case 0: //左スクロールの場合
scrX[n] = -5;
scrY[n] = 0;
scrXPOS[n] = width;
scrYPOS[n] = height / 3;
break;
case 1: //右移動の場合
scrX[n] = 5;
scrY[n] = 0;
scrXPOS[n] = 0;
scrYPOS[n] = height / 3 * 2;
break;
case 2: //上移動の場合
scrX[n] = 0;
scrY[n] = -5;
scrXPOS[n] = width / 3;
scrYPOS[n] = height;
break;
case 3: //下移動の場合
scrX[n] = 0;
scrY[n] = 5;
scrXPOS[n] = width /3 * 2;
scrYPOS[n] = 0;
break;
case 4:
scrX[n] = -7;
scrY[n] = 3;
scrXPOS[n] = width;
scrYPOS[n] = 0;
break;
}
token = sToken.nextToken();//カンマ(,)で区切られた次の文字列を取得(この場合,赤色を指定する数字)
try{
scrR[n] = Integer.valueOf(token).intValue(); //数値型に変換
}catch(NumberFormatException e){
}
token = sToken.nextToken();
try{
scrG[n] = Integer.valueOf(token).intValue();
}catch(NumberFormatException e){
}
token = sToken.nextToken();
try{
scrB[n] = Integer.valueOf(token).intValue();
}catch(NumberFormatException e){
}
scrColor[n] = new Color(scrR[n],scrG[n],scrB[n]);//上で得たRGB値を使い色を作成
n += 1;
}
for(i=0;i<n;i+=1){ //コンストラクタで,ja205aクラス(スクロール文字列)のインスタンスを生成する
scrText[i] = new ja205a(scrXPOS[i],scrYPOS[i],scrX[i],scrY[i],width,height,scrT[i],scrColor[i]);
}
}
public void start(){
td = new Thread(this);
td.start(); //スレッドの開始
}
public void run(){
while(true){
try{
Thread.sleep(100); //スレッドを0.1秒間隔で実行
}catch(InterruptedException e){}
repaint();
}
}
public void stop(){
td.stop();
}
public void paint(Graphics g){
int i;
g.setFont(f);
for(i = 0;i < n;i += 1){
scrText[i].paint(g); //ja205aクラス(スクロール文字列)のpaintメソッドを呼び出す
}
}
}
class ja205a //スクロール文字列を定義するクラス
{
int xstart,ystart; //スクロール開始位置のx,y座標
int xpos,ypos; //スクロール位置のx,y座標
int width,height; //Appletの幅と高さ
String st; //スクロール文字列
int stx,sty; //1回で移動する幅
Color sc; //文字列の色
public ja205a(int x,int y,int sx,int sy,int w,int h,String s,Color c){ //コンストラクタ
xstart = x; //スクロール開始位置
ystart = y;
stx = sx; //移動幅
sty = sy;
width = w; //Appletの幅と高さ
height = h;
st = s; //スクロール文字列
sc = c; //文字色
xpos = xstart; //スクロール開始位置を設定
ypos = ystart;
}
void paint(Graphics g){
g.setColor(sc); //描画色を設定
g.drawString(st,xpos,ypos); //文字列を描画する
xpos += stx; //移動幅分だけ位置を変化さす
ypos += sty;
FontMetrics fm = g.getFontMetrics(); //現在設定されているフォントの大きさを得る
int stw = fm.stringWidth(st); //スクロール文字列全体の幅と高さを取得
int sth = fm.getHeight();
if(stx < 0 && xpos < -stw)xpos = xstart;//左移動で,文字列が左端に消えたら開始位置に戻す
if(stx > 0 && xpos > width)xpos = xstart;//右移動で,文字列が右端に消えたら開始位置に戻す
if(sty < 0 && ypos < 0)ypos = ystart; //上移動で,文字列が上端に消えたら開始位置に戻す
if(sty > 0 && ypos >height + sth)ypos = ystart;//下移動で,文字列が下端に消えたら開始位置に戻す
}
}
末尾