JAVA研究室へ戻る


JAVAアプレットのテキスト表示サンプルです。ソースコード及びサンプルHTMLは ダウンロード コーナー入っています。
このアプレットはテキストの上スクロール表示を行います

基本パラメータは
 str     表示する文字列
 font    表示フォントの指定
 size    文字フォントのサイズ
 x       X座標オフセット
 y       Y座標オフセット
 color   文字の表示色
 bgcolor バックの表示色

追加パラメータ
 timer   表示を呼び出すタイミングを指定
 sount   スクロールサイズ




// JAVA applet sample 'DrawTextUpScroll.java' for kitaro C3Lab JAVA lab
// copyright (c) by kitaro 1996
// このプログラムはテキストを表示するアプレットサンプルです
// バックグラウンド処理で表示すればされにきれいになりますが
// 動作理解をメインに表画面のみで表示しています

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

public class DrawTextUpScroll extends java.applet.Applet implements Runnable {
  String str,font,temp;
  int  x,y,size;
  Color color,bgcolor;
  Font f;
  //
  Thread thread;
  int   timer,count;
  //
  FontMetrics fm;
  int count_max;
  int skip_count;
  //

  public void init(){
	str  = getParameter("str");   if(str ==null)str ="HelloWorld";
	font = getParameter("font");  if(font==null)font="Ariel";
	temp = getParameter("size");  if(temp==null)temp="24"; size = Integer.valueOf( temp ).intValue(); 
	temp = getParameter("x");     if(temp==null)temp="15"; x    = Integer.valueOf( temp ).intValue(); 
	temp = getParameter("y");     if(temp==null)y=size; else y    = Integer.valueOf( temp ).intValue(); 
	f = new Font(font,Font.BOLD,size);
	setFont( f );
	
	temp = getParameter("color"); if(temp==null)temp="FFFFFF";
  	
	color = new Color(
		Integer.valueOf(temp.substring(0,2),16).intValue(),
		Integer.valueOf(temp.substring(2,4),16).intValue(),
		Integer.valueOf(temp.substring(4  ),16).intValue());
	setForeground(color);

	temp = getParameter("bgcolor"); if(temp==null)temp="000000";
	bgcolor = new Color(
		Integer.valueOf(temp.substring(0,2),16).intValue(),
		Integer.valueOf(temp.substring(2,4),16).intValue(),
		Integer.valueOf(temp.substring(4  ),16).intValue());
	setBackground(bgcolor);

	//-----------------------------------------------------------
	temp = getParameter("timer");  if(temp==null)temp="10"; timer = Integer.valueOf( temp ).intValue();
	temp = getParameter("scount"); if(temp==null)temp="4";  skip_count = Integer.valueOf( temp ).intValue();
	

	fm = getFontMetrics(f);
	count_max = fm.getHeight();
	count_max*=2;
	count = -(count_max); 
  }
  public void start(){
	thread = new Thread(this);
	thread.start();
  }
  public void run (){
	while(true)
	{
	 try{ 
		thread.sleep(timer);
	}
	catch(Exception e){
	}
	repaint();
	count+=skip_count;
	if( count >= count_max) count=-count_max;
	}  
}
  public void paint(Graphics g){
	String s1,s2,s3;
	g.drawString(str,x,y-count);
  }
}

サンプルHTML
<html>
<head>
<title>DrawTextUpScroll</title>
</head>
<body>
<hr>
<applet code=DrawTextUpScroll width=200 height=60>
<param name=str    value=さんぷる>
<param name=timer  value=100>
<param name=color  value=CCCCFF>
<param name=scount value=2>
</applet>
</body>
</html>
<hr>