www.pudn.com > Bluegammon蓝牙的应用编程.rar > WinnerAnim.java


// Copyright (c) 2005 Sony Ericsson Mobile Communications AB 
// 
// This software is provided "AS IS," without a warranty of any kind.  
// ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,  
// INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A  
// PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.  
// 
// THIS SOFTWARE IS COMPLEMENTARY OF JAYWAY AB (www.jayway.se) 
 
package bluegammon.gui.animation; 
 
import javax.microedition.lcdui.Font; 
import javax.microedition.lcdui.Graphics; 
 
import bluegammon.Resources; 
import bluegammon.gui.BoardCanvas; 
import bluegammon.logic.BoardMediator; 
 
/** 
 * Animation for indicating a winner. Spawns 
 * subanimations of flying pieces. 
 *  
 * @see bluegammon.gui.animation.WinnerPieceMoveAnim 
 * @author Peter Andersson 
 */ 
public class WinnerAnim extends Animation 
{ 
  protected static final int WHITE_COL = 0xffffff; 
  protected static final int BLACK_COL = 0x000000; 
  protected static final char[] TXT_WHITE_WINS = 
    Resources.getChars(Resources.TXT_A_WHITE_WINS); 
  protected static final char[] TXT_BLACK_WINS =  
    Resources.getChars(Resources.TXT_A_BLACK_WINS); 
  protected static final Font BIG_FONT = 
    Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE); 
  protected static final Font SMALL_FONT = 
    Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_MEDIUM); 
   
  protected boolean m_whiteWinner; 
  protected int m_pieces = 0; 
  protected int m_piecesLeft; 
  protected int m_pts; 
  protected int m_d, m_x, m_y, m_fg, m_bg, m_step, m_tw, m_th; 
  protected char[] m_colWinnerTxt; 
  protected char[] m_piecesTxt; 
  protected char[] m_ptsTxt; 
  protected char[] m_totalTxt; 
 
  /** 
   * Creates a new winner animation. 
   * @param white		True for white winner, false for black winner. 
   * @param piecesLeft	Number of loser pieces left. 
   * @param pts			Number of points per piece. 
   */ 
  public WinnerAnim(boolean white, int piecesLeft, int pts) 
  { 
    m_whiteWinner = white; 
    m_ptsTxt = ("x" + pts + " " + 
        Resources.getString(pts > 1 ? Resources.TXT_A_POINTS : Resources.TXT_A_POINT) 
      ).toCharArray(); 
    m_piecesLeft = piecesLeft; 
    m_pts = pts; 
    m_d = Math.min(BoardCanvas.getInstance().getWidth(), BoardCanvas 
        .getInstance().getHeight()) / 2; 
    m_x = BoardCanvas.getInstance().getWidth() / 2; 
    m_y = (BoardCanvas.getInstance().getHeight() - m_d) / 2; 
    m_fg = WHITE_COL; 
    m_bg = BLACK_COL; 
    m_colWinnerTxt = TXT_BLACK_WINS; 
    if (white) 
    { 
      m_fg = BLACK_COL; 
      m_bg = WHITE_COL; 
      m_colWinnerTxt = TXT_WHITE_WINS; 
    } 
    m_tw = BIG_FONT.charsWidth(m_colWinnerTxt, 0, m_colWinnerTxt.length); 
    m_th = BIG_FONT.getHeight(); 
  } 
 
  public void paint(Graphics g) 
  { 
    if (BoardMediator.isGameFinished()) 
    { 
      int y = m_y; 
      drawText(m_colWinnerTxt, g, BIG_FONT, y); 
      y += (1.5f * (float) m_th); 
      if (m_pieces > 0) 
      { 
        drawText(m_piecesTxt, g, SMALL_FONT, y); 
        y += m_th; 
        drawText(m_ptsTxt, g, SMALL_FONT, y); 
        y += (1.5f * (float) m_th); 
        drawText(m_totalTxt, g, BIG_FONT, y); 
      } 
    } 
  } 
 
  public void next() 
  { 
    m_step++; 
    BoardCanvas.getInstance().requestRepaint(); 
    if (m_pieces < m_piecesLeft) 
    { 
      BoardCanvas.getInstance().addWinningPieceAnimation(!m_whiteWinner, this); 
    } 
  } 
 
  public boolean isFinished() 
  { 
    return !BoardMediator.isGameFinished(); 
  } 
 
  public long getInterval() 
  { 
    return 500; 
  } 
 
  /** 
   * Called from WinnerPieceMoveAnim when the 
   * piece has flyed beyond the screen. Updates the score count. 
   */ 
  public void piecePlus() 
  { 
    m_pieces++; 
    int score = m_pieces * m_pts; 
    m_piecesTxt = (m_pieces + " " + 
        Resources.getString(m_pieces > 1 ? Resources.TXT_A_PIECES : Resources.TXT_A_PIECE) 
      ).toCharArray(); 
    m_totalTxt = (score + " " + 
        Resources.getString(score > 1 ? Resources.TXT_A_POINTS : Resources.TXT_A_POINT) 
      ).toCharArray(); 
  } 
 
  /** 
   * Draws centered text in winning animation manner. 
   * @param txt	The text to draw. 
   * @param g	The graphic context to draw to. 
   * @param f	The font of the text. 
   * @param y	The y coordinate of the text.  
   */ 
  protected void drawText(char[] txt, Graphics g, Font f, int y) 
  { 
    g.setFont(f); 
    g.setColor(m_fg); 
    g.drawChars(txt, 0, txt.length, m_x + 1, y, 
        Graphics.BASELINE | Graphics.HCENTER); 
    g.drawChars(txt, 0, txt.length, m_x - 1, y, 
        Graphics.BASELINE | Graphics.HCENTER); 
    g.drawChars(txt, 0, txt.length, m_x, y + 1, 
        Graphics.BASELINE | Graphics.HCENTER); 
    g.drawChars(txt, 0, txt.length, m_x, y - 1, 
        Graphics.BASELINE | Graphics.HCENTER); 
    g.setColor(m_bg); 
    g.drawChars(txt, 0, txt.length, m_x, y, 
        Graphics.BASELINE | Graphics.HCENTER); 
  } 
 
}