JAVAアプレットのテキスト表示サンプルです。ソースコード及びサンプルHTMLは
ダウンロード
コーナー入っています。
このアプレットは時計のサンプルです
基本パラメータは url 表示するファイル名 x Xサイズ y Yサイズ
表示サンプル このページ自身を表示してみました。変な文字がひょうじされるのは日本語部分のためで、 これはAWTクラスが日本語に対応していないためです。きちんと表示されていれば特に 問題はありません。
// JAVA applet sample 'Clock1.java' for kitaro C3Lab JAVA lab // copyright (c) by kitaro 1996 // このプログラムは現在時間をテキスト表示するアプレットサンプルです // バックグラウンド処理で表示すればされにきれいになりますが // 動作理解をメインに表画面のみで表示しています // 時計プログラムの基本として使用可能です // テキストはカットアンドペーズトが可能です import java.applet.*; import java.awt.*; import java.util.*; public class Clock1 extends java.applet.Applet implements Runnable { Thread thread; String str,font; Date date; int size,timer,x,y; Color color,bgcolor; Font f; String s[] =new String[24]; // public void init(){ String temp; 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="24"; x = Integer.valueOf( temp ).intValue(); temp = getParameter("y"); if(temp==null)temp="24"; y = Integer.valueOf( temp ).intValue(); f = new Font(font,Font.BOLD,size); setFont( f ); str="----------"; timer = 1000; 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()); 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); int i; for(i=0 ;i<24;i++)s[i]="今晩は"; for(i=5 ;i<10;i++)s[i]="おはよう"; for(i=10;i<20;i++)s[i]="今日は"; } public void paint(Graphics g){ date = new Date(); str=s[date.getHours()] + " 今は"; str += Integer.toString(date.getMonth() +1)+"月"+ Integer.toString(date.getDate()+1 )+"日 " ; if(date.getHours ()<10)str+="0"; str += Integer.toString( date.getHours() ); str+=":"; if(date.getMinutes()<10)str+="0"; str += Integer.toString( date.getMinutes() ); str+=":"; if(date.getSeconds()<10)str+="0"; str += Integer.toString( date.getSeconds() ); Color cc = g.getColor(); g.setColor(color); g.drawString(str,x,y); g.setColor(cc); date = null; } public void start(){ thread = new Thread(this); thread.start(); } public void run (){ while(thread.isAlive()) { try{ thread.sleep(timer); } catch(Exception e){ } repaint(); } } }
サンプルHTML <applet code=Clock1 width=320 height=30> <param name=font value="TimesRoman"> <param name=size value="20"> <param name=color value="ccffcc"> <param name=bgcolor value="80cc40"> </applet>