/*
 * FontCanvas.java
 *
 * Created on 17 กันยายน 2546, 16:07 น.
 */

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 *
 * @author  suppakit
 * @version
 */
public class FontCanvas extends Canvas  implements CommandListener {
  private Font mSystemFont, mMonospaceFont, mProportionalFont;
  private Command mBoldCommand, mItalicCommand, mUnderlineCommand;

public FontCanvas(){
  this(Font.STYLE_PLAIN);
}

public FontCanvas(int style){
  setStyle(style);
  mBoldCommand = new Command("Bold", Command.SCREEN, 0);
  mItalicCommand = new Command("Italic", Command.SCREEN, 0);
  mUnderlineCommand = new Command("Underline", Command.SCREEN, 0);
  addCommand(mBoldCommand);
  addCommand(mItalicCommand);
  addCommand(mUnderlineCommand);
  setCommandListener(this);
}

  public void setStyle(int style){
    mSystemFont = Font.getFont(Font.FACE_SYSTEM, style, Font.SIZE_MEDIUM);
    mMonospaceFont = Font.getFont(Font.FACE_MONOSPACE, style, Font.SIZE_MEDIUM);
    mProportionalFont = Font.getFont(Font.FACE_PROPORTIONAL, style, Font.SIZE_MEDIUM);   
  }

    /**
     * paint
     */
    public void paint(Graphics g) {
        int w = getWidth();
   int h = getHeight();

   //Clear the Canvas
   g.setGrayScale(255);
   g.fillRect(0, 0, w-1, h-1);
   g.setGrayScale(0);
   g.drawRect(0, 0, w-1, h-1);

   int x = w/2;
   int y=20;

   y += showFont(g, "System", x, y, mSystemFont);
   y += showFont(g, "Monospace", x, y, mMonospaceFont);
   y += showFont(g, "Proportional", x, y, mProportionalFont);

    }   
  
  private int showFont(Graphics g, String s, int x, int y, Font f){
    g.setFont(f);
    g.drawString(s, x, y, Graphics.TOP | Graphics.HCENTER);
    return f.getHeight();

  }
  
  public void commandAction(Command c, Displayable s){
    boolean isBold = mSystemFont.isBold()^(c == mBoldCommand);
    boolean isItalic = mSystemFont.isItalic()^(c == mItalicCommand);
    boolean isUnderline = mSystemFont.isUnderlined()^(c == mUnderlineCommand);

    int style =
		(isBold ? Font.STYLE_BOLD : 0)|
		(isItalic ? Font.STYLE_ITALIC : 0)|
		(isUnderline ? Font.STYLE_UNDERLINED : 0);
    setStyle(style);
    repaint();

  }
   
}
