import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;

/**
  メッセージの色とフォントを指定できる ColorMessage クラス
*/
public class ColorMessage extends Applet {

     /**
       メッセージの内容
     */
       public String message=null;

     /**
       メッセージの色
     */
       public Color  textColor=null;

     /**
       背景色
     */
       public Color  bgColor=null;

     /**
       メッセージのフォント
     */
       public Font   textFont=null;

     /**
       メッセージのフォント・メトリック
     */
       public FontMetrics   textFontMetrics=null;

     /**
      フォントのサイズ
     */
       public int fontSize;

     /**
      初期設定の関数（生成時に自動的に呼ばれる）
     */
       public void init() {

          // パラメータの値を得る

             String color = getParameter("color");
             String bgcolor = getParameter("bgcolor");
             String size = getParameter("size");
             String style = getParameter("style");
             String family = getParameter("family");

           // テキストの色を設定

             if ( color != null ) {
                  if ( color.equals("white") 
                       || color.equals("WHITE") || color.equals("White") )
                     textColor = Color.white;
                  else if ( color.equals("red")
                       || color.equals("RED") || color.equals("Red") )
                     textColor = Color.red;
                  else if ( color.equals("yellow")
                       || color.equals("YELLOW") || color.equals("Yellow") )
                     textColor = Color.yellow;
                  else if ( color.equals("green")
                       || color.equals("GREEN") || color.equals("Green") )
                     textColor = Color.green;
                  else if ( color.equals("orange")
                       || color.equals("ORANGE") || color.equals("Orange") )
                     textColor = Color.orange;
                  else if ( color.equals("magenta")
                       || color.equals("MAGENTA") || color.equals("Magenta") )
                     textColor = Color.magenta;
                  else if ( color.equals("pink")
                       || color.equals("PINK") || color.equals("Pink") )
                     textColor = Color.pink;
                  else if ( color.equals("cyan")
                       || color.equals("CYAN") || color.equals("Cyan") )
                     textColor = Color.cyan;
                  else if ( color.equals("blue")
                       || color.equals("BLUE") || color.equals("Blue") )
                     textColor = Color.blue;
                  else
                     textColor = Color.black;
              }
             else 
                textColor = Color.black;

           // 背景色を設定

             if ( bgcolor != null ) {
                  if ( bgcolor.equals("white") 
                       || bgcolor.equals("WHITE") || bgcolor.equals("White") )
                     bgColor = Color.white;
                  else if ( bgcolor.equals("red")
                       || bgcolor.equals("RED") || bgcolor.equals("Red") )
                     bgColor = Color.red;
                  else if ( bgcolor.equals("yellow")
                       || bgcolor.equals("YELLOW") || bgcolor.equals("Yellow") )
                     bgColor = Color.yellow;
                  else if ( bgcolor.equals("green")
                       || bgcolor.equals("GREEN") || bgcolor.equals("Green") )
                     bgColor = Color.green;
                  else if ( bgcolor.equals("orange")
                       || bgcolor.equals("ORANGE") || bgcolor.equals("Orange") )
                     bgColor = Color.orange;
                  else if ( bgcolor.equals("magenta")
                       || bgcolor.equals("MAGENTA") || bgcolor.equals("Magenta") )
                     bgColor = Color.magenta;
                  else if ( bgcolor.equals("pink")
                       || bgcolor.equals("PINK") || bgcolor.equals("Pink") )
                     bgColor = Color.pink;
                  else if ( bgcolor.equals("cyan")
                       || bgcolor.equals("CYAN") || bgcolor.equals("Cyan") )
                     bgColor = Color.cyan;
                  else if ( bgcolor.equals("blue")
                       || bgcolor.equals("BLUE") || bgcolor.equals("Blue") )
                     bgColor = Color.blue;
                  else
                     bgColor = Color.lightGray;
              }
             else 
                bgColor = Color.lightGray;

           // フォントのサイズの設定

             if ( size != null ) {
                  if ( size.equals("large")
                       || size.equals("LARGE") || size.equals("Large") )
                     fontSize=36;
                  else if ( size.equals("small")
                       || size.equals("SMALL") || size.equals("Small") )
                     fontSize=10;
                  else
                     fontSize=14;
              }  
             else
                     fontSize=14;

           // フォントのファミリーの設定

             if ( family == null ) family = "TimesRoman";

           // フォントのスタイルの設定

             if ( style != null ) {
                  if ( style.equals("italic")
                       || style.equals("ITALIC") || style.equals("Italic") )
                     textFont = new Font(family, Font.ITALIC, fontSize);
                  else if ( style.equals("bold")
                       || style.equals("BOLD") || style.equals("Bold") )
                     textFont = new Font(family, Font.BOLD, fontSize);
                  else
                     textFont = new Font(family, Font.PLAIN, fontSize);
              }  
              else
                     textFont = new Font( family, Font.PLAIN, fontSize );

           // フォントメトリックを得る（文字列の長さを調べるため）

             textFontMetrics = getFontMetrics( textFont );

           // メッセージの内容を設定

             message = getParameter("message");
             if ( message == null )
                  message = "Welcome to Wakkanai!";

           // サイズの調整

             resize( textFontMetrics.stringWidth(message) + 8, fontSize*3/2  );
       }

     /**
       初期画面を描く関数（画面登場時に自動的に呼ばれる）
     */
       public void paint( Graphics g ) {

             g.setColor( bgColor );
             g.fill3DRect( 0, 0,
                size().width-2, size().height-2, true ); //立体的な矩形を描く

             g.setColor( textColor );
             g.setFont( textFont );
             g.drawString( message, 4, fontSize );  //文字列を描く
       }
}

