JAVA研究室へ戻る


JAVAアプレットのテキスト表示サンプルです。ソースコード及びサンプルHTMLは ダウンロード コーナー入っています。
 このアプレットはバックグラウンドイメージ処理で画面のずれに対応しています。 もとのDrawTextTickerクラスとの差がバックグラウンド処理のために追加した部分です
さらに影付き表示と壁紙を張ることができます。

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

追加パラメータ
 timer   表示を呼び出すタイミングを指定
 color2  テキスト変化表示色
 wx      表示エリアX座標サイズ
 wy      表示エリアY座標サイズ
 scolor  影テキスト表示色
 wall    壁紙グラフィクスファイル


// JAVA applet sample 'DrawTextTickerShadowWall.java' for kitaro C3Lab JAVA lab // copyright (c) by kitaro 1996 // このプログラムはテキストを表示するアプレットサンプルです // バックグラウンド処理対応にしました // バックグラウンド処理のためパラメータが3つ増えました // テキストに指定色の影をつけます // 壁紙対応にしました // wx : エリアサイズX座標 // wy : エリアサイズY座標 // scolor : 影の色 // wall : 壁紙ファイル名 import java.applet.*; import java.awt.*; import java.net.*; public class DrawTextTickerShadowWall extends java.applet.Applet implements Runnable { String str,font,temp; int x,y,size; Color color,bgcolor,scolor; Font f; // Thread thread; int timer,count; // Color color2; int textLength; FontMetrics fm; // background job Graphics og; Image oimage; int wx,wy; // Wall paper String wall; Image wallImage; // public void init(){ str = getParameter("str"); if(str ==null)str ="HelloWorld"; font = getParameter("font"); if(font==null)font="Ariel"; temp = getParameter("wx"); if(temp==null)temp="200"; wx = Integer.valueOf( temp ).intValue(); temp = getParameter("wy"); if(temp==null)temp="200"; wy = Integer.valueOf( temp ).intValue(); 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("scolor"); if(temp==null) { temp = getParameter("color"); if(temp==null)temp="808080"; scolor = new Color( Integer.valueOf(temp.substring(0,2),16).intValue()/2, Integer.valueOf(temp.substring(2,4),16).intValue()/2, Integer.valueOf(temp.substring(4 ),16).intValue()/2); } else scolor = 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("timer"); if(temp==null)temp="100"; 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(); // wall paper setting wall = getParameter("wall"); wall = "http://www.pionet.net/~kitaro/gif/.wall/w_glass1.gif"; if(wall!=null) try{wallImage = getImage(new URL(wall)); } catch(Exception e){ wall=null; showStatus(e.toString());} else wallImage=null; // offscreen oimage = createImage(wx,wy); og = oimage.getGraphics(); og.setColor(getBackground()); og.fillRect(0,0,wx,wy ); og.setColor(getForeground()); og.setFont(f); } public void start(){ thread = new Thread(this); thread.start(); } public void run (){ while(true) { try{ thread.sleep(timer);} catch(Exception e){ } off_update(); repaint(); count++; if( count >= textLength) count=0; } } public void off_clear(){ og.setColor(bgcolor); if(wall==null) og.fillRect(0,0,wx,wy ); else{ int x,y,sx,sy; try{ sx = wallImage.getWidth(this); sy = wallImage.getHeight(this); if((sx>0)&&(sy>0)){ for(x = 0; x < wx; x += sx) for(y = 0; y < wy; y += sy) og.drawImage(wallImage,x,y,bgcolor,this); } }catch(Exception e){} } og.setColor(color); } public void off_update(){ 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); off_clear(); og.setColor(scolor); og.drawString(str,x+2,y+2); og.setColor(color); og.drawString(s1,x,y); og.setColor(color2); og.drawString(s2,x+t1,y); og.setColor(color); og.drawString(s3,x+t1+t2,y); } public void paint(Graphics g){ g.drawImage(oimage,0,0,bgcolor,this); } public void update(Graphics g){paint(g); } }
サンプルHTML <html> <head> <title>DrawTextTickerShadowWall</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> <param name=wall value=/~kitaro/gif/.wall/w_glass1.gif> </applet> </body> </html> <hr>