JAVA研究室へ戻る


JAVAアプレットの始めのサンプルとしてテキスト表示を行ってみましょう。ソースコード及び サンプルHTMLは ダウンロード した中に入っています。

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



// JAVA applet sample 'DrawText.java' for kitaro C3Lab JAVA lab
// copyright (c) by kitaro 1996

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

public class DrawText extends java.applet.Applet {
  String str,font,temp;
  int  x,y,size;
  Color color,bgcolor;
  String temp1,temp2;
  Font f;

  public void init(){
	int vr,vg,vb;
	str  = getParameter("str");   if(str ==null)str ="Hello";
	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(); 

	setFont( new Font(font,Font.BOLD,size) );
	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);

  }
  public void paint(Graphics g){
	g.drawString(str,x,y);
  }
}