📄 gauge.java
字号:
* to each block. * * @param width the width given to this gauge on the screen */ private void setBlockCountAndValue(int width) { blockCount = (width - (2 * arrowWidth)) / (BLOCK_SPACE); if (maxValue != INDEFINITE) { if (blockCount > maxValue) { blockCount = maxValue; } valueOfEachBlock = 1; if (maxValue <= blockCount) { return; } try { valueOfEachBlock = maxValue / blockCount; if (maxValue % blockCount != 0) { valueOfEachBlock++; } // There's a case when our max is close to // a multiple of our value per block (ie 50 // as a max value, 4 or 5 as a value per block). // In this case, we decrease the number of blocks // and make each block an even value // (5, using the above example). blockCount = maxValue / valueOfEachBlock; if (maxValue % valueOfEachBlock != 0) { blockCount++; } // If the max was a value like 52, then there // would be the full number of blocks (11 with // the RI screen width) and the last block // would have 2 units of value. The last block // in the gauge always as as many or less units // than the other blocks in the gauge. } catch (ArithmeticException e) { // width could be set to 0. // in which case an aritmetic exception // might be thrown } } } /** * Set the horizontal arrows for this gauge. * we are turning on the left/right indicator arrows * on the display to indicate to the user that * the Gauge can be incremented/decremented. */ private void setGaugeArrows() { // If we have focus and are interactive, we check // to see if we need to add the right or left arrow // arrow should only // appear when the gauge has focus if ((hasFocus) && (interactive)) { int scrollPosition = value * 100 / maxValue; // Fix the rounding error if (value > 0 && scrollPosition == 0) { scrollPosition = 1; } if (0 < value && value < maxValue) { drawLeftArrow = drawRightArrow = true; } else { if (value == 0) { drawLeftArrow = false; drawRightArrow = true; } else { drawLeftArrow = true; drawRightArrow = false; } } } else { drawLeftArrow = drawRightArrow = false; } } /** * Utility method to ensure the value of the Gauge is always * in a range of 0 to maxValue, or if maxValue is INDEFINITE * that value is CONTINUOUS_IDLE, INCREMENTAL_IDLE, * INCREMENTAL_UPDATING, or CONTINUOUS_RUNNING. In the case * where maxValue is INDEFINITE and value is not one of the * three defined here it will be set to CONTINUOUS_IDLE. (0) * * private instanve variable value will be within parameter * after this call */ private void checkValue() { if (maxValue == INDEFINITE) { if (value < CONTINUOUS_IDLE || value > INCREMENTAL_UPDATING) { value = CONTINUOUS_IDLE; } } else { if (value < 0) { value = 0; } else if (value > maxValue) { value = maxValue; } } } /** * Set the max value of this Gauge. * * @param maxValue The maximum value to set for this Gauge * * @throws IllegalArgumentException if maxValue is not positive for * interactive gauges * @throws IllegalArgumentException if maxValue is neither positive nor * INDEFINITE for non-interactive gauges */ private void setMaxValueImpl(int maxValue) { if (maxValue <= 0) { if (!(interactive == false && maxValue == INDEFINITE)) { throw new IllegalArgumentException(); } } int oldMaxValue = this.maxValue; this.maxValue = maxValue; if (oldMaxValue == INDEFINITE) { if (maxValue > INDEFINITE) { value = 0; blockCount = -1; // signal to recalculate blockCount } } else { if (maxValue == INDEFINITE) { value = 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; invalidate(); } /** * A helper class to update an indefinite range gauge * when value == CONTINUOUS_RUNNING */ private class GaugeUpdateTask extends TimerTask { /** * the sprite to send updated events to */ private Sprite mySprite; /** * the gauge to repaint */ private Gauge myGauge; /** * Construct a new GaugeUpdateTask. * * @param sprite the sprite to send update events to * @param gauge the gauge to repaint */ GaugeUpdateTask(Sprite sprite, Gauge gauge) { super(); mySprite = sprite; myGauge = gauge; } /** * required method in TimerTask derivatives */ public final void run() { mySprite.nextFrame(); myGauge.repaint(); } } /** * Start the GaugeUpdateTask running * * @param sprite the sprite to pass to the GaugeUpdateTask * constructor */ 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; } } /** * Current sprite in use...continuous or incremental * If null, the initSprites() has not been called */ 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; /** * Image for the left arrow. */ private static final Image LEFTARROW_IMG; /** * Image for the right arrow. */ private static final Image RIGHTARROW_IMG; /** * Sprite image for frames of an incremental indefinite gauge. */ private static final Image INCREMENTAL_IMG; /** * Sprite image for frames of an continuous indefinite gauge. */ private static final Image CONTINUOUS_IMG; /* * Initialize animation sequences for sprites. Assumes that * CONTINUOUS_SPRITE and INCREMENTAL_SPRITE have the same frame * count (5), and that the idle state image is the last frame * in the sprite's image. */ /** * 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; /** * The numerical value assigned to each block. * The number of blocks (blockCount) times the * value of each block (valueOfEachBlock) shall * equal the maximum value of this gauge (maxValue) * * In the case of an indefinite range gauge, the * number of blocks (blockCount) will equal the * "maximum value" of the gauge (frameCount) and * valueOfEachBlock will be 1. */ 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; /** * A TimerTask which will schedule repaints of an indefinite * range gauge when value == CONTINUOUS_RUNNING */ private GaugeUpdateTask updateHelper; static { /* * The Timer to schedule update/repaint events with * in CONTINUOUS_RUNNING mode. */ gaugeUpdateTimer = new Timer(); /* * Initialize the images and sprites necessary for the gauge. */ LEFTARROW_IMG = ImmutableImage.createIcon("gauge_leftarrow.png"); RIGHTARROW_IMG = ImmutableImage.createIcon("gauge_rightarrow.png"); CONTINUOUS_IMG = ImmutableImage.createIcon("continuous_strip.png"); INCREMENTAL_IMG = ImmutableImage.createIcon("incremental_strip.png"); } // static }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -