basicprogressbarui.java

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

JAVA
1,340
字号
        }    }    /**     * Stores the position and size of     * the bouncing box that would be painted for the current animation index     * in <code>r</code> and returns <code>r</code>.     * Subclasses that add to the painting performed     * in this class's implementation of <code>paintIndeterminate</code> --     * to draw an outline around the bouncing box, for example --     * can use this method to get the location of the bouncing     * box that was just painted.     * By overriding this method,     * you have complete control over the size and position      * of the bouncing box,     * without having to reimplement <code>paintIndeterminate</code>.     *     * @param r  the Rectangle instance to be modified;     *           may be <code>null</code>     * @return   <code>null</code> if no box should be drawn;     *           otherwise, returns the passed-in rectangle     *           (if non-null)     *           or a new rectangle     *     * @see #setAnimationIndex     * @since 1.4     */    protected Rectangle getBox(Rectangle r) {        int currentFrame = getAnimationIndex();        int middleFrame = numFrames/2;        if (DEBUGALL) {            System.out.println("----begin getBox----");            System.out.println("    getBox argument: " + r);            System.out.println("    currentFrame = " + currentFrame);            System.out.println("    middleFrame = " + middleFrame);        }        if (sizeChanged() || delta == 0.0 || maxPosition == 0.0) {            updateSizes();        }        r = getGenericBox(r);         if (r == null) {            if (DEBUGALL) {                System.out.println("    Exiting because r is null");            }            return null;        }        if (middleFrame <= 0) {            if (DEBUGALL) {                System.out.println("    Exiting because middleFrame <= 0.");            }            return null;        }        //assert currentFrame >= 0 && currentFrame < numFrames        if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {            if (currentFrame < middleFrame) {                r.x = componentInnards.x                      + (int)Math.round(delta * (double)currentFrame);            } else {                r.x = maxPosition                      - (int)Math.round(delta *                                         (currentFrame - middleFrame));            }        } else { //VERTICAL indeterminate progress bar            if (currentFrame < middleFrame) {                r.y = componentInnards.y                      + (int)Math.round(delta * currentFrame);            } else {                r.y = maxPosition                      - (int)Math.round(delta *                                        (currentFrame - middleFrame));            }        }        if (DEBUGALL) {            System.out.println("    getBox return value: " + r);            System.out.println("----end getBox----");        }        return r;    }    /**     * Updates delta, max position.     * Assumes componentInnards is correct (e.g. call after sizeChanged()).     */    private void updateSizes() {        if (DEBUGALL) {            System.out.println("----begin updateSizes----");        }        int length = 0;        if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {            length = getBoxLength(componentInnards.width,                                  componentInnards.height);            maxPosition = componentInnards.x + componentInnards.width                          - length;        } else { //VERTICAL progress bar            length = getBoxLength(componentInnards.height,                                  componentInnards.width);            maxPosition = componentInnards.y + componentInnards.height                          - length;        }        //If we're doing bouncing-box animation, update delta.        if (DEBUGALL) {            System.out.println("    Updating delta.");        }        delta = 2.0 * (double)maxPosition/(double)numFrames;        if (BASICDEBUG) {            System.out.println("    delta: " + delta);            System.out.println("    maxPosition: " + maxPosition);        }        if (DEBUGALL) {            System.out.println("----end updateSizes----");        }        return;     }    /**     * Assumes that the component innards, max position, etc. are up-to-date.     */    private Rectangle getGenericBox(Rectangle r) {        if (DEBUGALL) {            System.out.println("----begin getGenericBox----");            System.out.println("    argument: " + r);        }        if (r == null) {            r = new Rectangle();        }        if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {            r.width = getBoxLength(componentInnards.width,                                   componentInnards.height);            if (r.width < 0) {                r = null;            } else {                r.height = componentInnards.height;                r.y = componentInnards.y;            }          // end of HORIZONTAL        } else { //VERTICAL progress bar            r.height = getBoxLength(componentInnards.height,                                    componentInnards.width);            if (r.height < 0) {                r = null;            } else {                r.width = componentInnards.width;                r.x = componentInnards.x;            }        } // end of VERTICAL        if (DEBUGALL) {            System.out.println("    getGenericBox returns: " + r);            System.out.println("----end getGenericBox----");        }        return r;    }    /**     * Returns the length     * of the "bouncing box" to be painted.     * This method is invoked by the      * default implementation of <code>paintIndeterminate</code>     * to get the width (if the progress bar is horizontal)     * or height (if vertical) of the box.     * For example:     * <blockquote>     * <pre>     *boxRect.width = getBoxLength(componentInnards.width,     *                             componentInnards.height);     * </pre>     * </blockquote>     *     * <p>     * By default, this method returns the available length     * divided by 6.  Another possibility might     * be to make the bouncing box a square,      * which you could implement by overriding this method as follows:     * <blockquote>     * <pre>     *protected double getBoxLength(int availableLength,     *                              int otherDimension) {     *    return Math.min(availableLength, otherDimension);     *}     * </blockquote>     * </pre>     *     * @param availableLength  the amount of space available     *                         for the bouncing box to move in;     *                         for a horizontal progress bar,     *                         for example,     *                         this should be     *                         the inside width of the progress bar     *                         (the component width minus borders)     * @param otherDimension   for a horizontal progress bar, this should be     *                         the inside height of the progress bar; this     *                         value might be used to constrain or determine     *                         the return value      *     * @return the size of the box dimension being determined;      *         must be no larger than <code>availableLength</code>     *     * @see javax.swing.SwingUtilities#calculateInnerArea     * @since 1.4     */    private int getBoxLength(int availableLength, int otherDimension) {        return (int)Math.round(availableLength/6.0);    }    /**     * All purpose paint method that should do the right thing for all     * linear bouncing-box progress bars.      * Override this if you are making another kind of      * progress bar.     *     * @see #paintDeterminate     *     * @since 1.4     */    protected void paintIndeterminate(Graphics g, JComponent c) {        if (!(g instanceof Graphics2D)) {            return;        }	Insets b = progressBar.getInsets(); // area for border	int barRectWidth = progressBar.getWidth() - (b.right + b.left);	int barRectHeight = progressBar.getHeight() - (b.top + b.bottom);        Graphics2D g2 = (Graphics2D)g;        if (DEBUGALL) {            System.out.println();            System.out.println("basic: paintIndeterminate");        }        // Paint the bouncing box.        boxRect = getBox(boxRect);        if (boxRect != null) {            g2.setColor(progressBar.getForeground());            g2.fillRect(boxRect.x, boxRect.y,                       boxRect.width, boxRect.height);        } else if (DEBUGALL) {            //we're not initialized yet            System.out.println("boxRect == null; returning");        }	// Deal with possible text painting	if (progressBar.isStringPainted()) {            if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {                paintString(g2, b.left, b.top,                            barRectWidth, barRectHeight,                            boxRect.x, boxRect.width, b);            }            else {                paintString(g2, b.left, b.top,                            barRectWidth, barRectHeight,                            boxRect.y, boxRect.height, b);            }        }    }    /**     * All purpose paint method that should do the right thing for almost     * all linear, determinate progress bars. By setting a few values in     * the defaults     * table, things should work just fine to paint your progress bar.     * Naturally, override this if you are making a circular or     * semi-circular progress bar.     *      * @see #paintIndeterminate     *     * @since 1.4     */    protected void paintDeterminate(Graphics g, JComponent c) {        if (!(g instanceof Graphics2D)) {            return;        }	Insets b = progressBar.getInsets(); // area for border	int barRectWidth = progressBar.getWidth() - (b.right + b.left);	int barRectHeight = progressBar.getHeight() - (b.top + b.bottom);        int cellLength = getCellLength();        int cellSpacing = getCellSpacing();	// amount of progress to draw	int amountFull = getAmountFull(b, barRectWidth, barRectHeight);	        Graphics2D g2 = (Graphics2D)g;	g2.setColor(progressBar.getForeground());	if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {	    // draw the cells	    if (cellSpacing == 0 && amountFull > 0) {                // draw one big Rect because there is no space between cells                g2.setStroke(new BasicStroke((float)barRectHeight,                        BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));	    } else {                // draw each individual cell                g2.setStroke(new BasicStroke((float)barRectHeight,                        BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL,                        0.f, new float[] { cellLength, cellSpacing }, 0.f));            }            if (BasicGraphicsUtils.isLeftToRight(c)) {                g2.drawLine(b.left, (barRectHeight/2) + b.top,                        amountFull + b.left, (barRectHeight/2) + b.top);            } else {                g2.drawLine((barRectWidth + b.left),                        (barRectHeight/2) + b.top,                        barRectWidth + b.left - amountFull,                        (barRectHeight/2) + b.top);            }            	} else { // VERTICAL	    // draw the cells	    if (cellSpacing == 0 && amountFull > 0) {                // draw one big Rect because there is no space between cells                g2.setStroke(new BasicStroke((float)barRectWidth,                        BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));	    } else {                // draw each individual cell                g2.setStroke(new BasicStroke((float)barRectWidth,                        BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL,                        0f, new float[] { cellLength, cellSpacing }, 0f));

⌨️ 快捷键说明

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