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

📄 progressbarui.java

📁 用于java swing的皮肤软件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
			if(DEBUGALL)
			{
				System.out.println("    Changing animation index from "
										 +animationIndex+" to "+newValue);
			}

			if(sizeChanged())
			{
				if(DEBUGALL)
				{
					System.out.println("    size changed; resetting maxPosition, delta");
				}
				animationIndex=newValue;
				maxPosition=0;   //needs to be recalculated
				delta=0.0;   //needs to be recalculated
				progressBar.repaint();

				return;
			}

			//Get the previous box drawn.
			nextPaintRect=getBox(nextPaintRect);

			if(DEBUGALL)
			{
				System.out.println("    previous paint rect =  "+nextPaintRect);
				System.out.println("    before setting, boxRect =  "+boxRect);
			}

			//Update the frame number.
			animationIndex=newValue;

			//Get the next box to draw.
			if(nextPaintRect!=null)
			{
				boxRect=getBox(boxRect);
				if(boxRect!=null)
				{
					nextPaintRect.add(boxRect);
				}
			}

			if(DEBUGALL)
			{
				System.out.println("    after setting, boxRect =  "+boxRect);
				System.out.println("    after setting, nextPaintRect =  "
										 +nextPaintRect);
			}
		}
		else
		{   //animationIndex == newValue
			if(DEBUGALL)
			{
				System.out.println("    No change in value");
				System.out.println("----end setAnimationIndex----");
			}

			return;
		}

		if(nextPaintRect!=null)
		{
			progressBar.repaint(nextPaintRect);
		}
		else
		{
			progressBar.repaint();
			if(DEBUGALL)
			{
				System.out.println("    repaint without args");
			}
		}

		if(DEBUGALL)
		{
			System.out.println("----end setAnimationIndex----");
		}
	}


	private boolean sizeChanged()
	{
		if((oldComponentInnards==null) || (componentInnards==null))
		{
			return true;
		}

		oldComponentInnards.setRect(componentInnards);
		componentInnards=SwingUtilities.calculateInnerArea(progressBar,
																			componentInnards);

		return !oldComponentInnards.equals(componentInnards);
	}


	/**
	 * Sets the index of the current animation frame,
	 * to the next valid value,
	 * which results in the progress bar being repainted.
	 * The next valid value is, by default,
	 * the current animation index plus one.
	 * If the new value would be too large,
	 * this method sets the index to 0.
	 * Subclasses might need to override this method
	 * to ensure that the index does not go over
	 * the number of frames needed for the particular
	 * progress bar instance.
	 * This method is invoked by the default animation thread
	 * every <em>X</em> milliseconds,
	 * where <em>X</em> is specified by the "ProgressBar.repaintInterval"
	 * UI default.
	 *
	 * @see #setAnimationIndex
	 * @since 1.4
	 */
	protected void incrementAnimationIndex()
	{
		int newValue=getAnimationIndex()+1;
		if(DEBUGALL)
		{
			System.out.println();
			System.out.println("----begin incrementAnimationIndex----");
			System.out.println("    newValue = "+newValue);
			System.out.println("    numFrames = "+numFrames);
		}

		if(newValue<numFrames)
		{
			setAnimationIndex(newValue);
		}
		else
		{
			setAnimationIndex(0);
			if(LOGSTATS)
			{
				numLoops++;
				long time=System.currentTimeMillis();
				System.out.println("Loop #"+numLoops+": "+(time-lastLoopTime)+" ("
										 +(time-startTime)+" total)");
				lastLoopTime=time;
			}
		}
		if(DEBUGALL)
		{
			System.out.println("----end incrementAnimationIndex----");
		}
	}


	/**
	 * Returns the desired number of milliseconds between repaints.
	 * This value is meaningful
	 * only if the progress bar is in indeterminate mode.
	 * The repaint interval determines how often the
	 * default animation thread's timer is fired.
	 * It's also used by the default indeterminate progress bar
	 * painting code when determining
	 * how far to move the bouncing box per frame.
	 * The repaint interval is specified by
	 * the "ProgressBar.repaintInterval" UI default.
	 *
	 * @return  the repaint interval, in milliseconds
	 */
	private int getRepaintInterval()
	{
		return repaintInterval;
	}


	private int initRepaintInterval()
	{
		repaintInterval=UIManager.getInt("ProgressBar.repaintInterval");
		if(BASICDEBUG)
		{
			System.out.println("    value of ProgressBar.repaintInterval is "
									 +repaintInterval);
		}

		return repaintInterval;
	}


	/**
	 * Returns the number of milliseconds per animation cycle.
	 * This value is meaningful
	 * only if the progress bar is in indeterminate mode.
	 * The cycle time is used by the default indeterminate progress bar
	 * painting code when determining
	 * how far to move the bouncing box per frame.
	 * The cycle time is specified by
	 * the "ProgressBar.cycleTime" UI default
	 * and adjusted, if necessary,
	 * by the initIndeterminateDefaults method.
	 *
	 * @return  the cycle time, in milliseconds
	 */
	private int getCycleTime()
	{
		return cycleTime;
	}


	private int initCycleTime()
	{
		cycleTime=UIManager.getInt("ProgressBar.cycleTime");
		if(BASICDEBUG)
		{
			System.out.println("    value of ProgressBar.cycleTime is "+cycleTime);
		}

		return cycleTime;
	}


	/** Initialize cycleTime, repaintInterval, numFrames, animationIndex. */
	private void initIndeterminateDefaults()
	{
		if(DEBUGALL)
		{
			System.out.println("----begin initIndeterminateDefaults----");
		}
		initRepaintInterval();   //initialize repaint interval
		initCycleTime();   //initialize cycle length

		// Make sure repaintInterval is reasonable.
		if(repaintInterval<=0)
		{
			repaintInterval=100;
		}

		// Make sure cycleTime is reasonable.
		if(repaintInterval>cycleTime)
		{
			cycleTime=repaintInterval*20;
			if(DEBUGALL)
			{
				System.out.println("cycleTime changed to "+cycleTime);
			}
		}
		else
		{
			// Force cycleTime to be a even multiple of repaintInterval.
			int factor=(int)Math.ceil(((double)cycleTime)/((double)repaintInterval*2));
			if(DEBUGALL)
			{
				int newCycleTime=repaintInterval*factor*2;
				if(cycleTime!=newCycleTime)
				{
					System.out.println("cycleTime being changed to "+newCycleTime);
				}
			}

			cycleTime=repaintInterval*factor*2;
		}

		if(BASICDEBUG)
		{
			System.out.println("    cycle length: "+cycleTime);
			System.out.println("    repaint interval: "+repaintInterval);
		}
		if(DEBUGALL)
		{
			System.out.println("----end initIndeterminateDefaults----");
		}
	}


	/**
	 * Invoked by PropertyChangeHandler before startAnimationTimer().
	 *
	 *  NOTE: This might not be invoked until after the first
	 *  paintIndeterminate call.
	 */
	private void initIndeterminateValues()
	{
		if(DEBUGALL)
		{
			System.out.println();
			System.out.println("----begin initIndeterminateValues----");
		}
		if(LOGSTATS)
		{
			startTime=lastLoopTime=System.currentTimeMillis();
			numLoops=0;
		}

		if(BASICDEBUG)
		{
			System.out.println("ADJUSTTIMER = "+ADJUSTTIMER);
		}

		initIndeterminateDefaults();

		//assert cycleTime/repaintInterval is a whole multiple of 2.
		numFrames=cycleTime/repaintInterval;
		initAnimationIndex();

		boxRect=new Rectangle();
		nextPaintRect=new Rectangle();
		componentInnards=new Rectangle();
		oldComponentInnards=new Rectangle();

		if(BASICDEBUG)
		{
			System.out.println("    numFrames: "+numFrames);
		}
		if(DEBUGALL)
		{
			System.out.println("----end initIndeterminateValues----");
		}
	}


	/** Invoked by PropertyChangeHandler after stopAnimationTimer(). */
	private void cleanUpIndeterminateValues()
	{
		if(DEBUGALL)
		{
			System.out.println();
			System.out.println("----begin cleanUpIndeterminateValues----");
		}

		cycleTime=repaintInterval=0;
		numFrames=animationIndex=0;
		maxPosition=0;
		delta=0.0;

		boxRect=nextPaintRect=null;
		componentInnards=oldComponentInnards=null;

		if(LOGSTATS)
		{
			startTime=lastLoopTime=numLoops=0;
		}

		if(DEBUGALL)
		{
			System.out.println("----end cleanUpIndeterminateValues----");
		}
	}


	// Called from initIndeterminateValues to initialize the animation index.
	// This assumes that numFrames is set to a correct value.
	private void initAnimationIndex()
	{
		if((progressBar.getOrientation()==JProgressBar.HORIZONTAL)
					&& (TonicUtils.isLeftToRight(progressBar)))
		{
			// If this is a left-to-right progress bar,
			// start at the first frame.
			setAnimationIndex(0);
		}
		else
		{
			// If we go right-to-left or vertically, start at the right/bottom.
			setAnimationIndex(numFrames/2);
		}
	}

	//
	// Animation Thread
	//


	/**
	 * Implements an animation thread that invokes repaint
	 * at a fixed rate.  If ADJUSTTIMER is true, this thread
	 * will continuously adjust the repaint interval to
	 * try to make the actual time between repaints match
	 * the requested rate.
	 */
	private class Animator implements ActionListener
	{
		private Timer timer;
		private long  previousDelay;   //used to tune the repaint interval
		private int   interval;   //the fixed repaint interval
		private long  lastCall;   //the last time actionPerformed was called
		private int   MINIMUM_DELAY=5;

		/**
		 * Creates a timer if one doesn't already exist,
		 * then starts the timer thread.
		 */
		private void start(int interval)
		{
			previousDelay=interval;
			lastCall=0;

			if(timer==null)
			{
				timer=new Timer(interval, this);
			}
			else
			{
				timer.setDelay(interval);
			}

			if(ADJUSTTIMER)
			{
				timer.setRepeats(false);
				timer.setCoalesce(false);
			}

			timer.start();
		}


		/**
		 * Stops the timer thread.
		 */
		private void stop()
		{
			timer.stop();
		}


		/**
		 * Reacts to the timer's action events.
		 */
		public void actionPerformed(ActionEvent e)
		{
			if(ADJUSTTIMER)
			{
				long time=System.currentTimeMillis();

				if(lastCall>0)
				{   //adjust nextDelay
					int nextDelay=(int)(previousDelay-time+lastCall
									  +getRepaintInterval());
					if(nextDelay<MINIMUM_DELAY)
					{
						nextDelay=MINIMUM_DELAY;
					}
					timer.setInitialDelay(nextDelay);
					previousDelay=nextDelay;
					if(DEBUGTIMER)
					{
						System.out.println("---------------------");
						System.out.println("actual delay = "+(time-lastCall));
						System.out.println("next delay = "+nextDelay);
					}
				}
				timer.start();
				lastCall=time;
			}

			incrementAnimationIndex();   //paint next frame
		}
	}


	//
	// Property Change Events
	//


	/**
	 * [PENDING: add doc here]
	 * [PENDING: make this static?]
	 */
	private class PropertyChangeHandler implements PropertyChangeListener
	{
		public void propertyChange(PropertyChangeEvent e)
		{
			String prop=e.getPropertyName();
			if("indeterminate".equals(prop))
			{
				isIndeterminate=progressBar.isIndeterminate();

				if(isIndeterminate)
				{
					initIndeterminateValues();

					//start the animation thread
					startAnimationTimer();
				}
				else
				{
					//stop the animation thread
					stopAnimationTimer();

					//clean up
					cleanUpIndeterminateValues();
				}

				progressBar.repaint();
			}
		}
	}


	//
	// Change Events
	//


	/**
	 * This inner class is marked &quot;public&quot; due to a compiler bug.
	 * This class should be treated as a &quot;protected&quot; inner class.
	 * Instantiate it only within subclasses of BasicProgressBarUI.
	 */
	public class ChangeHandler implements ChangeListener
	{
		public void stateChanged(ChangeEvent e)
		{
			BoundedRangeModel model=progressBar.getModel();
			int				   newRange=model.getMaximum()-model.getMinimum();
			int				   newPercent;
			int				   oldPercent=getCachedPercent();

			if(newRange>0)
			{
				newPercent=(int)((100*(long)model.getValue())/newRange);
			}
			else
			{
				newPercent=0;
			}

			if(newPercent!=oldPercent)
			{
				setCachedPercent(newPercent);
				progressBar.repaint();
			}
		}
	}
}

⌨️ 快捷键说明

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