⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bqgauge.java

📁 J2me唆哈的代码
💻 JAVA
字号:
/*
 * Created on 2005-9-23 by pcy
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package a.a.a.midp.lcdui;

import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.Sprite;

public class BQGauge extends BQItem {

    public static final long serialVersionUID=1;
    
    private Sprite spriteInUse;

    /**
     * Sprite used for the incremental indefinite gauge animation.
     */
    private Sprite INCREMENTAL_SPRITE;
    
    /**
     * Sprite used for the continuous indefinite gauge animation.
     */
    private Sprite CONTINUOUS_SPRITE;
    
    
    /**
     * Frame sequence for an idle indefinite gauge.
     */
    private static final int[] IDLE_SEQUENCE = {4};

    /**
     * Frame sequence for a non idle indefinite gauge.
     */
    private static final int[] ACTIVE_SEQUENCE = {0, 1, 2, 3};

    /** The current value of this gauge */
    private int value;

    /** The maximum possible value of this gauge */
    private int maxValue = 0;

    /** Wether this gauge is interactive or not */
    private boolean interactive;

    /** The number of blocks making up this gauge */
    private int blockCount = -1;

    /** A flag indicating a prior call to callTraverse() */
    //private boolean traversedIn;

    /** By default, a Gauge is 78 pixels wide */
    private static final int DEFAULT_WIDTH = 78;

    /**
     * The space occupied by one block in the gauge.
     */
    private static final int BLOCK_SPACE = 8;

    /**
     * Greatest height, in pixels, this gauge will
     * occupy on screen
     */
    private static final int GAUGE_HEIGHT = 25;


    private int valueOfEachBlock;

    /**
     * Whether we draw the left arrow or not.
     * 
     */
    private boolean drawLeftArrow;

    /**
     * Whether we draw the right arrow or not.
     * 
     */
    private boolean drawRightArrow;

    /** Arrow width - space available for the arrows. */
    private int arrowWidth;

    /**
     * The buffer of horizontal space to split on
     * either side of this gauge (creating left/right margins)
     */
    private int blockMargin;

    /** 
     * A Timer which will handle scheduling repaints of an 
     * indefinite range gauge when value == CONTINUOUS_RUNNING
     */
    private static Timer gaugeUpdateTimer = new Timer(); 

    /** 
     * A TimerTask which will schedule repaints of an indefinite 
     * range gauge when value == CONTINUOUS_RUNNING
     */
    private GaugeUpdateTask updateHelper;

    public BQGauge(String label, boolean interactive,
            int maxValue, int initialValue) {
        super(label);
        
        if (maxValue == Gauge.INDEFINITE && (initialValue < Gauge.CONTINUOUS_IDLE || 
                initialValue > Gauge.INCREMENTAL_UPDATING)) {
            throw new IllegalArgumentException();
        }

        this.interactive = interactive;
 
         if (this.interactive) {
             //arrowWidth = LEFTARROW_IMG.getWidth() + 6;
             // this spacing is what is aesthitically suitable
         } else {
             arrowWidth = 0;
         }
         blockMargin = 2 + arrowWidth;       
         /*
          * IllegalArgumentException may be thrown by
          * setMaxValueImpl and setValue
          */
         setMaxValueImpl(maxValue);
         setValue(initialValue);
    }
    
    public void setLabel(String label){
        if (this.owner instanceof BQAlert) {
            throw new IllegalStateException("Gauge contained within an Alert");
        }
        super.setLabel(label);
    }
    
    public void setLayout(int layout){
        if (this.owner instanceof BQAlert) {
            throw new IllegalStateException("Gauge contained within an Alert");
        }
        super.setLayout(layout);
    }
    
    public void addCommand(BQCommand cmd){
        if (this.owner instanceof BQAlert) {
            throw new IllegalStateException("Gauge contained within an Alert");
        }
        super.addCommand(cmd);
    }
    
    public void setItemCommandListener(BQItemCommandListener l){
        if (this.owner instanceof BQAlert) {
            throw new IllegalStateException("Gauge contained within an Alert");
        }
        super.setItemCommandListener(l);
    }
    
    public void setPreferredSize(int width, int height){
        if (this.owner instanceof BQAlert) {
            throw new IllegalStateException("Gauge contained within an Alert");
        }
        super.setPreferredSize(width, height);
    }
    
    public void setDefaultCommand(BQCommand cmd){
        if (this.owner instanceof BQAlert) {
            throw new IllegalStateException("Gauge contained within an Alert");
        }
        super.setDefaultCommand(cmd);
    }
    
    public void setValue(int value){
        if (!interactive && maxValue == Gauge.INDEFINITE) {
            if (value != Gauge.CONTINUOUS_RUNNING && 
                this.value == Gauge.CONTINUOUS_RUNNING) {
                cancelGaugeUpdateTask();
            }
            switch (value) {
            case Gauge.CONTINUOUS_IDLE:
                spriteInUse = CONTINUOUS_SPRITE;
                spriteInUse.setFrameSequence(IDLE_SEQUENCE);
            break;
            case Gauge.INCREMENTAL_IDLE:
                spriteInUse = INCREMENTAL_SPRITE;
                spriteInUse.setFrameSequence(IDLE_SEQUENCE);
                break;
            case Gauge.INCREMENTAL_UPDATING:
                if (spriteInUse != INCREMENTAL_SPRITE ||
                    spriteInUse.getFrameSequenceLength() == 1) {
                    spriteInUse = INCREMENTAL_SPRITE;
                    spriteInUse.setFrameSequence(ACTIVE_SEQUENCE);
                } else {
                    spriteInUse.nextFrame();
                }
                break;
            case Gauge.CONTINUOUS_RUNNING:
                if (spriteInUse != CONTINUOUS_SPRITE ||
                    spriteInUse.getFrameSequenceLength() == 1) {
                    spriteInUse = CONTINUOUS_SPRITE;
                    spriteInUse.setFrameSequence(ACTIVE_SEQUENCE);
                }
                if (updateHelper == null) {
                    startGaugeUpdateTask(spriteInUse);
                }
            break;
            default:
                throw new IllegalArgumentException();
            }
        }
        this.value = value;
        checkValue();

    }
    
    public int getValue(){
        return value;
    }
    
    public void setMaxValue(int maxValue){
        setMaxValueImpl(maxValue);
    }
    
    public int getMaxValue(){
        return maxValue;
    }
    
    public boolean isInteractive(){
        return interactive;
    }

    int callMinimumWidth() {
        return DEFAULT_WIDTH;
    }

    int callPreferredWidth(int h) {
        return DEFAULT_WIDTH;
    }

    int callMinimumHeight() {
        return callPreferredHeight(-1);
    }

    int callPreferredHeight(int w) {
        if (maxValue == Gauge.INDEFINITE) {
            return INCREMENTAL_SPRITE.getHeight() /*+ getLabelHeight(w)*/;
        } else {
            // NTS: Don't know why the '+2' is needed
            return GAUGE_HEIGHT + /*getLabelHeight(w)*/ + 2;
        }
    }
    
    private void setMaxValueImpl(int maxValue) {
        if (maxValue <= 0) {
            if (!(interactive == false && maxValue == Gauge.INDEFINITE)) {
                throw new IllegalArgumentException();
            }
        }
        int oldMaxValue = this.maxValue;
        this.maxValue = maxValue;
        if (oldMaxValue == Gauge.INDEFINITE) {
            if (maxValue > Gauge.INDEFINITE) {
                value = 0;
                blockCount = -1;  // signal to recalculate blockCount
            }
        } else {
            if (maxValue == Gauge.INDEFINITE) {
                value = Gauge.CONTINUOUS_IDLE;
                if (spriteInUse == null) {
                      initSprites();
                }
                spriteInUse = CONTINUOUS_SPRITE;
                spriteInUse.setFrameSequence(IDLE_SEQUENCE);
            }
        }
        checkValue();
 
        //
        // changing the max value will change the scale and how many
        // solid blocks are drawn. setting blockCount to -1 will force
        // callPaint to recalculate how much each block is worth.
        //
        blockCount = -1;
    }
    
    private void checkValue() {
        if (maxValue == Gauge.INDEFINITE) {
            if (value < Gauge.CONTINUOUS_IDLE || value > Gauge.INCREMENTAL_UPDATING) {
                value = Gauge.CONTINUOUS_IDLE;
            }
        } else {
            if (value < 0) {
                value = 0;
            } else if (value > maxValue) {
                value = maxValue;
            }
        }
    }
    
    private void initSprites() {
        //INCREMENTAL_SPRITE = new Sprite(INCREMENTAL_IMG, 44, 45);   
        //CONTINUOUS_SPRITE = new Sprite(CONTINUOUS_IMG, 29, 33);
    }
    
    private void startGaugeUpdateTask(Sprite sprite) {
        updateHelper = new GaugeUpdateTask(sprite, this);
        gaugeUpdateTimer.schedule(updateHelper, 250, 250);
    }
        
        /**
         * Stop the GaugeUpdateTask from running.
         */
    private void cancelGaugeUpdateTask() {
        if (updateHelper != null) {
            updateHelper.cancel();
            updateHelper = null;
        }
    }

    private class GaugeUpdateTask extends TimerTask {
        /**
         * the sprite to send updated events to
         */
        private Sprite mySprite;

        /**
         * the gauge to repaint
         */
        private BQGauge myGauge;

        /**
         * Construct a new GaugeUpdateTask.
         *
         * @param sprite the sprite to send update events to
         * @param gauge the gauge to repaint
         */
        GaugeUpdateTask(Sprite sprite, BQGauge gauge) {
            super();
            mySprite = sprite;
            myGauge = gauge;
        }

        /**
         * required method in TimerTask derivatives
         */
            public final void run() {
                mySprite.nextFrame();
                //myGauge.repaint();
            }
        }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -