CounterArea.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:5k
源码类别:

J2ME

开发平台:

Java

  1.  import javax.microedition.lcdui.*;
  2. // The counter class, which can be used on a canvas
  3. // or wrapped within a custom item.
  4. public class CounterArea {
  5.     public static final int DEFAULT_RATE = 500;
  6.     public static final int MIN_RATE = 100;
  7.     // The callback interface by which we notify
  8.     // the counter owner of certain events.
  9.     public interface Callback {
  10.         void invalidateCounter( CounterArea counter );
  11.         void repaintCounter( CounterArea counter );
  12.         void resizeCounter( CounterArea counter );
  13.     }
  14.     public CounterArea(){
  15.     }
  16.     public CounterArea( int width, int height ){
  17.         _width = width;
  18.         _height = height;
  19.     }
  20.     public int getBackColor(){
  21.         return _backColor;
  22.     }
  23.     public Callback getCallback(){
  24.         return _callback;
  25.     }
  26.     public Font getFont(){
  27.         return _font;
  28.     }
  29.     public Font getFontForDrawing(){
  30.         return _font != null ? _font :Font.getDefaultFont();
  31.     }
  32.     public int getHeight(){
  33.         if( _height < 0 ){
  34.             _height = getMinHeight();
  35.         }
  36.         return _height;
  37.     }
  38.     public int getMinHeight(){
  39.         return getFontForDrawing().getHeight();
  40.     }
  41.     public int getMinWidth(){
  42.         Font f = getFontForDrawing();
  43.         return f.stringWidth( Integer.toString( _value ) );
  44.     }
  45.     public int getRate(){
  46.         return _rate;
  47.     }
  48.     public int getTextColor(){
  49.         return _textColor;
  50.     }
  51.     public int getValue(){
  52.         return _value;
  53.     }
  54.     public int getWidth(){
  55.         if( _width < 0 ){
  56.             _width = getMinWidth();
  57.         }
  58.         return _width;
  59.     }
  60.     private void invalidate(){
  61.         if( _callback != null ){
  62.             _callback.invalidateCounter( this );
  63.         }
  64.     }
  65.     public boolean isCounting(){
  66.         return _timer != null;
  67.     }
  68.     public void paint( Graphics g ){
  69.         String s = Integer.toString( _value );
  70.         Font   f = getFontForDrawing();
  71.         int    w = f.stringWidth( s );
  72.         int    h = f.getHeight();
  73.         int   aw = getWidth();
  74.         int   ah = getHeight();
  75.         g.setColor( _backColor );
  76.         g.fillRect( _left, _top, aw, ah );
  77.         g.setColor( _textColor );
  78.         g.drawString( s, _left + aw - w,
  79.                       _top + ( ah - h ) / 2,
  80.                       g.TOP | g.LEFT );
  81.         if( w > aw || h > ah ){
  82.             resize();
  83.         }
  84.     }
  85.     private void repaint(){
  86.         if( _callback != null ){
  87.             _callback.repaintCounter( this );
  88.         }
  89.     }
  90.     private void resize(){
  91.         if( _callback != null ){
  92.             _callback.resizeCounter( this );
  93.         }
  94.     }
  95.     private synchronized boolean increment( Runnable source ){
  96.         if( source != _timer ) return false;
  97.         ++_value;
  98.         repaint();
  99.         return true;
  100.     }
  101.     public void setBackColor( int color ){
  102.         _backColor = color;
  103.         invalidate();
  104.     }
  105.     public void setCallback( Callback callback ){
  106.         _callback = callback;
  107.     }
  108.     public void setLeft( int left ){
  109.         _left = left;
  110.     }
  111.     public void setFont( Font f ){
  112.         _font = f;
  113.         invalidate();
  114.     }
  115.     public void setHeight( int h ){
  116.         _height = h;
  117.     }
  118.     public void setRate( int rate ){
  119.         _rate = ( rate < MIN_RATE ? MIN_RATE : rate );
  120.     }
  121.     public void setTextColor( int color ){
  122.         _textColor = color;
  123.         invalidate();
  124.     }
  125.     public void setTop( int top ){
  126.         _top = top;
  127.     }
  128.     public synchronized void setValue( int value ){
  129.         _value = value;
  130.     }
  131.     public void setWidth( int w ){
  132.         _width = w;
  133.     }
  134.     public synchronized void start(){
  135.         _timer = new CounterTimer();
  136.         new Thread( _timer ).start();
  137.     }
  138.     public synchronized void stop(){
  139.         _timer = null;
  140.     }
  141.     private int       _backColor = 0x00FFFFFF;
  142.     private Callback  _callback;
  143.     private Font      _font;
  144.     private int       _height = -1;
  145.     private int       _index;
  146.     private int       _left;
  147.     private int       _rate = DEFAULT_RATE;
  148.     private int       _textColor = 0x00000000;
  149.     private Runnable  _timer;
  150.     private int       _top;
  151.     private int       _width = -1;
  152.     private int       _value;
  153.     //-------------------------------------------------
  154.     // A very simple timer that sleeps and wakes
  155.     // up at regular intervals.
  156.     private class CounterTimer implements Runnable {
  157.         public void run(){
  158.             Thread t = Thread.currentThread();
  159.             while( true ){
  160.                 try {
  161.                     t.sleep( _rate );
  162.                 }
  163.                 catch( InterruptedException e ){
  164.                 }
  165.                 if( !increment( this ) ) break;
  166.             }
  167.         }
  168.     }
  169. }