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

📄 gauge.java

📁 j2me is based on j2mepolish, client & server for mobile application. menu sample
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	 */
	public Gauge( String label, boolean interactive, int maxValue, int initialValue, Style style )
	{
		super( label, Item.LAYOUT_DEFAULT, Item.PLAIN, style );
		// check values:
		//#ifndef polish.skipArgumentCheck
			if (interactive) {
				if (maxValue < 0 ) {
					//#ifdef polish.debugVerbose
						//# throw new IllegalArgumentException("Invalid maxValue for interactive Gauge: " + maxValue );
					//#else
						throw new IllegalArgumentException();
					//#endif
				}
				if (initialValue < 0 || initialValue > maxValue) {
					//#ifdef polish.debugVerbose
						//# throw new IllegalArgumentException("Invalid initialValue for interactive Gauge: " + initialValue );
					//#else
						throw new IllegalArgumentException();
					//#endif
				}
			} else {
				if (maxValue == INDEFINITE) {
					this.isIndefinite = true;
					this.maxValue = 20;
					if ( !( initialValue == CONTINUOUS_IDLE 
							|| initialValue == CONTINUOUS_RUNNING
							|| initialValue == INCREMENTAL_IDLE
							|| initialValue == INCREMENTAL_UPDATING ) ) {
						//#ifdef polish.debugVerbose
							//# throw new IllegalArgumentException("Invalid initialValue for indefinite Gauge: " + initialValue );
						//#else
							throw new IllegalArgumentException();
						//#endif
					}
				} else if (maxValue < 0 ) {
					//#ifdef polish.debugVerbose
						//# throw new IllegalArgumentException("Invalid maxValue for Gauge: " + maxValue );
					//#else
						throw new IllegalArgumentException();
					//#endif
				} else if (initialValue < 0 || initialValue > maxValue) {
					//#ifdef polish.debugVerbose
						//# throw new IllegalArgumentException("Invalid initialValue for Gauge: " + initialValue );
					//#else
						throw new IllegalArgumentException();
					//#endif
				}			
			}
		//#endif
		// set values
		this.isInteractive = interactive;
		if (interactive) {
			this.appearanceMode = Item.INTERACTIVE;
		}
		this.maxValue = maxValue;
		this.isIndefinite = (maxValue == INDEFINITE);
		setValue( initialValue );
	}


	/**
	 * Sets the current value of this <code>Gauge</code> object.
	 * 
	 * <p>If the gauge is interactive, or if it is non-interactive with
	 * definite range, the following rules apply.  If the value is less than
	 * zero, zero is used. If the current value is greater than the maximum
	 * value, the current value is set to be equal to the maximum value. </p>
	 * 
	 * <p> If this <code>Gauge</code> object is a non-interactive
	 * gauge with indefinite
	 * range, then value must be one of <code>CONTINUOUS_IDLE</code>,
	 * <code>INCREMENTAL_IDLE</code>, <code>CONTINUOUS_RUNNING</code>, or
	 * <code>INCREMENTAL_UPDATING</code>.
	 * Other values will cause an exception to be thrown.</p>
	 * 
	 * @param value the new value
	 * @throws IllegalArgumentException if value is not one of CONTINUOUS_IDLE,  INCREMENTAL_IDLE, CONTINUOUS_RUNNING, or INCREMENTAL_UPDATING for non-interactive gauges with indefinite range
	 * @see #INDEFINITE
	 * @see #CONTINUOUS_IDLE
	 * @see #INCREMENTAL_IDLE
	 * @see #CONTINUOUS_RUNNING
	 * @see #INCREMENTAL_UPDATING
	 */
	public void setValue(int value)
	{
		if (this.isIndefinite) {
			if (this.value == CONTINUOUS_RUNNING  && value != CONTINUOUS_RUNNING) {
				// when the value WAS continuous-running, remove this gauge from 
				// the animations:
				AnimationThread.removeAnimationItem( this );
			}
			if (value == CONTINUOUS_IDLE) {
				this.indefinitePos = 0;
			} else if (value == CONTINUOUS_RUNNING) {
				if (this.isShown && this.value != CONTINUOUS_RUNNING) {
					AnimationThread.addAnimationItem( this );
				}
			} else if ( value == INCREMENTAL_IDLE ) {
				this.indefinitePos = 0;
			} else if ( value == INCREMENTAL_UPDATING ) {
				this.indefinitePos++;
				if (this.indefinitePos > this.maxValue ) {
					this.indefinitePos = 0;				
				}
			} else {
				//#ifdef polish.debugVerbose
					//# throw new IllegalArgumentException("Invalid value for indefinite Gauge: " + value );
				//#else
					throw new IllegalArgumentException();
				//#endif
			}
		} else if (value < 0  ) {
			value = 0;
		} else  if (value > this.maxValue) {
			value = this.maxValue;
		}
		this.value = value;
		this.valueString = "" + value;
		//#if polish.css.view-type
		if (this.view == null) {
		//#endif		
			if (this.isInitialized) {
				if (this.isIndefinite) {
					updateIndefiniteIndicatorImage();
				} else if (this.image == null){
					createIndicatorImage();
				}
			}
		//#if polish.css.view-type
		}
		//#endif
		if (this.isInitialized) {
			repaint();
		}
	}

	/**
	 * Calculates the position of the indicator and creator the appropriate image.
	 * This method must not be called when the gauge has not yet been initialised.
	 */
	private void createIndicatorImage() {
		int percentage = (this.value * 100) / this.maxValue;
		int position = (percentage * (this.contentWidth - this.valueWidth)) / 100;
		if (position == 0) {
			position = 1;
		}
		this.indicatorImage = Image.createImage( position, this.contentHeight );
		Graphics g = this.indicatorImage.getGraphics();
		if (this.image != null) {
			int imageWidth = this.image.getWidth();
			int x = 0;
			while ( x < position ) {
				g.drawImage(this.image, x, 0, Graphics.TOP | Graphics.LEFT );
				x += imageWidth;
			}
		} else if (this.mode == MODE_CHUNKED) {
			/*
			if (this.chunkWidth == 0) {
				int spacePerChunk = this.contentWidth / this.maxValue;
				if (spacePerChunk > 5) {
					this.gapWidth = 3;
					this.chunkWidth = spacePerChunk - this.gapWidth;
				} else {
					
				}
			} */
			// paint the filling:
			g.setColor( this.color );
			g.fillRect( 0, 0, position, this.contentHeight );
			// paint the gaps:
			g.setColor( this.gapColor );
			int x = this.chunkWidth;
			while (x < position) {
				g.fillRect( x, 0, this.gapWidth, this.contentHeight );
				x += this.gapWidth + this.chunkWidth;
			}
		} else {
			// mode == CONTINUOUS
			g.setColor( this.color );
			g.fillRect( 0, 0, position, this.contentHeight );
		}
		
	}

	/**
	 * Updates the indicator image for an indefinite gauge.
	 */
	private void updateIndefiniteIndicatorImage() {
		Graphics g = this.indicatorImage.getGraphics();
		if (this.value == CONTINUOUS_IDLE || this.value == INCREMENTAL_IDLE) {
			g.setColor( this.gapColor );
			g.fillRect( 0, 0, this.contentWidth, this.contentHeight );
		} else if (this.value == CONTINUOUS_RUNNING ) {
			g.setColor( this.color );
			g.fillRect( 0, 0, this.contentWidth, this.contentHeight );
			g.setColor( this.gapColor );
			int cWidth = this.chunkWidth + this.gapWidth;
			int x =  this.indefinitePos - cWidth;
			while (x < this.contentWidth) {
				g.fillRect( x, 0, this.gapWidth, this.contentHeight );
				x += cWidth;
			}
		} else { // value == INCREMENTAL_UPDATE
			int percentage = (this.indefinitePos * 100) / this.maxValue;
			int position = (percentage * this.contentWidth) / 100;
			g.setColor( this.gapColor );
			g.fillRect( 0, 0, this.contentWidth, this.contentHeight );
			g.setColor( this.color );
			int cWidth = this.chunkWidth + this.gapWidth;
			int x = 0;
			while (x < position) {
				g.fillRect( x, 0, this.chunkWidth, this.contentHeight );
				x += cWidth;
			}
		}
	}

	/**
	 * Gets the current value of this <code>Gauge</code> object.
	 * 
	 * <p> If this <code>Gauge</code> object is a non-interactive
	 * gauge with indefinite
	 * range, the value returned will be one of <code>CONTINUOUS_IDLE</code>,
	 * <code>INCREMENTAL_IDLE</code>, <code>CONTINUOUS_RUNNING</code>, or
	 * <code>INCREMENTAL_UPDATING</code>.  Otherwise, it will be an integer
	 * between zero and the gauge's maximum value, inclusive.</p>
	 * 
	 * @return current value of the Gauge
	 * @see #INDEFINITE
	 * @see #CONTINUOUS_IDLE
	 * @see #INCREMENTAL_IDLE
	 * @see #CONTINUOUS_RUNNING
	 * @see #INCREMENTAL_UPDATING
	 */
	public int getValue()
	{
		return this.value;
	}

	/**
	 * Sets the maximum value of this <code>Gauge</code> object.
	 * 
	 * <p>For interactive gauges, the new maximum value must be greater than
	 * zero, otherwise an exception is thrown.  For non-interactive gauges,
	 * the new maximum value must be greater than zero or equal to the special
	 * value <code>INDEFINITE</code>, otherwise an exception is thrown.  </p>
	 * 
	 * <p>If the new maximum value is greater than zero, this provides the
	 * gauge with a definite range.  If the gauge previously had a definite
	 * range, and if the current value is greater than new maximum value, the
	 * current value is set to be equal to the new maximum value.  If the
	 * gauge previously had a definite range, and if the current value is less
	 * than or equal to the new maximum value, the current value is left
	 * unchanged. </p>
	 * 
	 * <p>If the new maximum value is greater than zero, and if the gauge had
	 * previously had indefinite range, this new maximum value provides it
	 * with a definite range.  Its graphical representation must change
	 * accordingly, the previous state of <code>CONTINUOUS_IDLE</code>,
	 * <code>INCREMENTAL_IDLE</code>, <code>CONTINUOUS_RUNNING</code>, or
	 * <code>INCREMENTAL_UPDATING</code> is ignored, and the current value
	 * is set to zero. </p>
	 * 
	 * <p>If this gauge is non-interactive and the new maximum value is
	 * <code>INDEFINITE</code>, this gives the gauge indefinite range.
	 * If the gauge
	 * previously had a definite range, its graphical representation must
	 * change accordingly, the previous value is ignored, and the current
	 * state is set to <code>CONTINUOUS_IDLE</code>.  If the gauge previously
	 * had an indefinite range, setting the maximum value to
	 * <code>INDEFINITE</code> will have no effect. </p>
	 * 
	 * @param maxValue the new maximum value
	 * @throws IllegalArgumentException if maxValue is invalid
	 * @see #INDEFINITE
	 * @see #getMaxValue()
	 */
	public void setMaxValue(int maxValue)
	{
		this.isInitialized = false;
		if (maxValue == INDEFINITE) {
			if (this.maxValue != maxValue) {
				this.value = CONTINUOUS_IDLE;
			}
			this.isIndefinite = true;
			//#ifndef polish.skipArgumentCheck
				} else if (maxValue < 0) {
					//#ifdef polish.verboseDebug
						//# throw new IllegalArgumentException("Invalid maxValue for Gauge: " + maxValue );
					//#else
						throw new IllegalArgumentException();
					//#endif
			//#endif
		} else if (this.value == CONTINUOUS_RUNNING) {
			this.isIndefinite = false;
			AnimationThread.removeAnimationItem( this );
		}
		this.maxValue = maxValue;
		this.isInitialized = false;
	}

	/**
	 * Gets the maximum value of this <code>Gauge</code> object.
	 * 
	 * <p>If this gauge is interactive, the maximum value will be a positive
	 * integer.  If this gauge is non-interactive, the maximum value will be a
	 * positive integer (indicating that the gauge has definite range)
	 * or the special value <code>INDEFINITE</code> (indicating that
	 * the gauge has indefinite range).</p>
	 * 
	 * @return the maximum value of the Gauge, or INDEFINITE
	 * @see #INDEFINITE
	 * @see #setMaxValue(int)
	 */
	public int getMaxValue()
	{
		return this.maxValue;
	}

	/**
	 * Tells whether the user is allowed to change the value of the
	 * <code>Gauge</code>.
	 * 
	 * @return a boolean indicating whether the Gauge is interactive
	 */
	public boolean isInteractive()
	{
		return this.isInteractive;
	}

	/* (non-Javadoc)
	 * @see de.enough.polish.ui.Item#paint(int, int, javax.microedition.lcdui.Graphics)
	 */
	public void paintContent(int x, int y, int leftBorder, int rightBorder, Graphics g) {
		if (this.showValue && this.isValueLeft) {
			g.setFont( this.font );
			g.setColor( this.fontColor );
			g.drawString( this.valueString, x, y, Graphics.TOP | Graphics.LEFT );
			x += this.valueWidth;
		}
		//#ifdef polish.css.gauge-inactive-image
			//# if (this.inactiveImage != null) {
				//# g.drawImage(this.inactiveImage, x, y + this.imageYOffset, Graphics.TOP | Graphics.LEFT );
			//# }
		//#endif
		if (this.isIndefinite) {
			if (this.image != null) {
				g.drawImage( this.image, x + this.indefinitePos, y + this.imageYOffset,  Graphics.TOP | Graphics.LEFT );
			} else if (this.indicatorImage != null ) {
				g.drawImage(this.indicatorImage, x, y + this.imageYOffset, Graphics.TOP | Graphics.LEFT );
			}
		} else if (this.image != null) {
			//#if polish.css.gauge-button-image
				//# if (this.value != 0 || this.useImageAsButton) {
			//#else
				if (this.value != 0) {
			//#endif
				//#if polish.css.gauge-button-image
					//#if polish.css.gauge-slider-image
						//# if (this.sliderImage != null) {
							//# g.drawImage(this.sliderImage, x, y + this.contentHeight / 2, Graphics.LEFT | Graphics.VCENTER);
						//# }
					//#endif
					//# if (this.useImageAsButton) {
						//# int xPos = (this.sliderWidth * this.value) / this.maxValue;
						//# g.drawImage( this.image, x + xPos, y + this.imageYOffset, Graphics.LEFT | Graphics.TOP );
					//# } else {
				//#endif
						int width = (this.image.getWidth() * this.value) / this.maxValue;
						int clipX = g.getClipX();
						int clipY = g.getClipY();
						int clipWidth = g.getClipWidth();
						int clipHeight = g.getClipHeight();
						
						g.clipRect(x, clipY, width, clipHeight);
						g.drawImage(this.image, x, y + this.imageYOffset, Graphics.TOP | Graphics.LEFT );
						
						g.setClip(clipX, clipY, clipWidth, clipHeight);
				//#if polish.css.gauge-button-image
					//# }
				//#endif
			}
		} else {
			g.drawImage(this.indicatorImage, x, y, Graphics.TOP | Graphics.LEFT );
		}
		if (this.showValue && !this.isValueLeft) {
			g.setFont( this.font );
			g.setColor( this.fontColor );
			g.drawString( this.valueString, rightBorder, y, Graphics.TOP | Graphics.RIGHT );
		}
	}

⌨️ 快捷键说明

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