basicprogressbarui.java

来自「java jdk 1.4的源码」· Java 代码 · 共 1,340 行 · 第 1/4 页

JAVA
1,340
字号
            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) &&            (BasicGraphicsUtils.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                //XXX maybe should cache this after a while                    //actual = time - lastCall                    //difference = actual - interval                    //nextDelay = previousDelay - difference                    //          = previousDelay - (time - lastCall - interval)                   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 + =
减小字号Ctrl + -
显示快捷键?