CounterArea.java
资源名称:J2ME&Game.rar [点击查看]
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:5k
源码类别:
J2ME
开发平台:
Java
- import javax.microedition.lcdui.*;
- // The counter class, which can be used on a canvas
- // or wrapped within a custom item.
- public class CounterArea {
- public static final int DEFAULT_RATE = 500;
- public static final int MIN_RATE = 100;
- // The callback interface by which we notify
- // the counter owner of certain events.
- public interface Callback {
- void invalidateCounter( CounterArea counter );
- void repaintCounter( CounterArea counter );
- void resizeCounter( CounterArea counter );
- }
- public CounterArea(){
- }
- public CounterArea( int width, int height ){
- _width = width;
- _height = height;
- }
- public int getBackColor(){
- return _backColor;
- }
- public Callback getCallback(){
- return _callback;
- }
- public Font getFont(){
- return _font;
- }
- public Font getFontForDrawing(){
- return _font != null ? _font :Font.getDefaultFont();
- }
- public int getHeight(){
- if( _height < 0 ){
- _height = getMinHeight();
- }
- return _height;
- }
- public int getMinHeight(){
- return getFontForDrawing().getHeight();
- }
- public int getMinWidth(){
- Font f = getFontForDrawing();
- return f.stringWidth( Integer.toString( _value ) );
- }
- public int getRate(){
- return _rate;
- }
- public int getTextColor(){
- return _textColor;
- }
- public int getValue(){
- return _value;
- }
- public int getWidth(){
- if( _width < 0 ){
- _width = getMinWidth();
- }
- return _width;
- }
- private void invalidate(){
- if( _callback != null ){
- _callback.invalidateCounter( this );
- }
- }
- public boolean isCounting(){
- return _timer != null;
- }
- public void paint( Graphics g ){
- String s = Integer.toString( _value );
- Font f = getFontForDrawing();
- int w = f.stringWidth( s );
- int h = f.getHeight();
- int aw = getWidth();
- int ah = getHeight();
- g.setColor( _backColor );
- g.fillRect( _left, _top, aw, ah );
- g.setColor( _textColor );
- g.drawString( s, _left + aw - w,
- _top + ( ah - h ) / 2,
- g.TOP | g.LEFT );
- if( w > aw || h > ah ){
- resize();
- }
- }
- private void repaint(){
- if( _callback != null ){
- _callback.repaintCounter( this );
- }
- }
- private void resize(){
- if( _callback != null ){
- _callback.resizeCounter( this );
- }
- }
- private synchronized boolean increment( Runnable source ){
- if( source != _timer ) return false;
- ++_value;
- repaint();
- return true;
- }
- public void setBackColor( int color ){
- _backColor = color;
- invalidate();
- }
- public void setCallback( Callback callback ){
- _callback = callback;
- }
- public void setLeft( int left ){
- _left = left;
- }
- public void setFont( Font f ){
- _font = f;
- invalidate();
- }
- public void setHeight( int h ){
- _height = h;
- }
- public void setRate( int rate ){
- _rate = ( rate < MIN_RATE ? MIN_RATE : rate );
- }
- public void setTextColor( int color ){
- _textColor = color;
- invalidate();
- }
- public void setTop( int top ){
- _top = top;
- }
- public synchronized void setValue( int value ){
- _value = value;
- }
- public void setWidth( int w ){
- _width = w;
- }
- public synchronized void start(){
- _timer = new CounterTimer();
- new Thread( _timer ).start();
- }
- public synchronized void stop(){
- _timer = null;
- }
- private int _backColor = 0x00FFFFFF;
- private Callback _callback;
- private Font _font;
- private int _height = -1;
- private int _index;
- private int _left;
- private int _rate = DEFAULT_RATE;
- private int _textColor = 0x00000000;
- private Runnable _timer;
- private int _top;
- private int _width = -1;
- private int _value;
- //-------------------------------------------------
- // A very simple timer that sleeps and wakes
- // up at regular intervals.
- private class CounterTimer implements Runnable {
- public void run(){
- Thread t = Thread.currentThread();
- while( true ){
- try {
- t.sleep( _rate );
- }
- catch( InterruptedException e ){
- }
- if( !increment( this ) ) break;
- }
- }
- }
- }