JAVAアプレットのテキスト表示サンプルです。ソースコード及びサンプルHTMLは
ダウンロード
コーナー入っています。
基本パラメータは str 表示する文字列 font 表示フォントの指定 size 文字フォントのサイズ x X座標オフセット y Y座標オフセット color 文字の表示色 bgcolor バックの表示色 追加パラメータ timer 表示を呼び出すタイミングを指定 color2 テキスト変化表示色
// JAVA applet sample 'DrawTextTicker.java' for kitaro C3Lab JAVA lab // copyright (c) by kitaro 1996 // このプログラムはテキストを表示するアプレットサンプルです // バックグラウンド処理で表示すればされにきれいになりますが // 動作理解をメインに表画面のみで表示しています import java.applet.*; import java.awt.*; public class DrawTextTicker extends java.applet.Applet implements Runnable { String str,font,temp; int x,y,size; Color color,bgcolor; Font f; // Thread thread; int timer,count; // Color color2; int textLength; FontMetrics fm; // 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="1000"; timer = Integer.valueOf( temp ).intValue(); temp = getParameter("color2"); if(temp==null)temp="7f7f7f"; color2 = 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()); fm = getFontMetrics(f); count = 0; textLength = str.length(); } public void start(){ thread = new Thread(this); thread.start(); } public void run (){ while(true) { try{ thread.sleep(timer); } catch(Exception e){ } repaint(); count++; if( count >= textLength) count=0; } } public void paint(Graphics g){ int t1,t2; String s1,s2,s3; s1 = str.substring(0,count); s2 = str.substring(count,count+1); s3 = str.substring(count+1); t1 = fm.stringWidth(s1); t2 = fm.stringWidth(s2); g.setColor(color); g.drawString(s1,x,y); g.setColor(color2); g.drawString(s2,x+t1,y); g.setColor(color); g.drawString(s3,x+t1+t2,y); } public void update(Graphics g){ paint(g); } }
サンプルHTML <html> <head> <title>DrawTextTicker</title> </head> <body> <hr> <applet code=DrawTextTicker width=200 height=200> <param name=str value=さんぷる> <param name=timer value=100> <param name=color value=808080> <param name=color2 value=ffff80> </applet> </body> </html> <hr>