basicprogressbarui.java

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

JAVA
1,340
字号
	    }            g2.drawLine(barRectWidth/2 + b.left,                    b.top + barRectHeight,                    barRectWidth/2 + b.left,                    b.top + barRectHeight - amountFull);	}		// Deal with possible text painting	if (progressBar.isStringPainted()) {	    paintString(g, b.left, b.top,			barRectWidth, barRectHeight,			amountFull, b);	}    }    protected void paintString(Graphics g, int x, int y,			       int width, int height,			       int amountFull, Insets b) {	if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {            if (BasicGraphicsUtils.isLeftToRight(progressBar)) {		if (progressBar.isIndeterminate()) {		    boxRect = getBox(boxRect);		    paintString(g, x, y, width, height, boxRect.x, boxRect.width, b);		} else {		    paintString(g, x, y, width, height, x, amountFull, b);		}            }            else {                paintString(g, x, y, width, height, x + width - amountFull,                            amountFull, b);            }        }        else {	    if (progressBar.isIndeterminate()) {		boxRect = getBox(boxRect);		paintString(g, x, y, width, height, boxRect.y, boxRect.height, b);	    } else {		paintString(g, x, y, width, height, y + height - amountFull,			    amountFull, b);	    }        }    }    /**     * Paints the progress string.     *     * @param g Graphics used for drawing.     * @param x x location of bounding box     * @param y y location of bounding box     * @param width width of bounding box     * @param height height of bounding box     * @param fillStart start location, in x or y depending on orientation,     *        of the filled portion of the progress bar.     * @param amountFull size of the fill region, either width or height     *        depending upon orientation.     * @param b Insets of the progress bar.     */    private void paintString(Graphics g, int x, int y, int width, int height,                             int fillStart, int amountFull, Insets b) {        if (!(g instanceof Graphics2D)) {            return;        }        Graphics2D g2 = (Graphics2D)g;	String progressString = progressBar.getString();	g2.setFont(progressBar.getFont());	Point renderLocation = getStringPlacement(g2, progressString,						  x, y, width, height);	Rectangle oldClip = g2.getClipBounds();		if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {	    g2.setColor(getSelectionBackground());	    g2.drawString(progressString, renderLocation.x, renderLocation.y);	    g2.setColor(getSelectionForeground());            g2.clipRect(fillStart, y, amountFull, height);	    g.drawString(progressString, renderLocation.x, renderLocation.y);	} else { // VERTICAL	    g2.setColor(getSelectionBackground());            AffineTransform rotate =                    AffineTransform.getRotateInstance(Math.PI/2);            g2.setFont(progressBar.getFont().deriveFont(rotate));	    renderLocation = getStringPlacement(g2, progressString,						  x, y, width, height);	    g2.drawString(progressString, renderLocation.x, renderLocation.y);	    g2.setColor(getSelectionForeground());	    g2.clipRect(x, fillStart, width, amountFull);	    g2.drawString(progressString, renderLocation.x, renderLocation.y);	}	g2.setClip(oldClip);    }            /**     * Designate the place where the progress string will be painted.     * This implementation places it at the center of the progress     * bar (in both x and y). Override this if you want to right,     * left, top, or bottom align the progress string or if you need     * to nudge it around for any reason.     */    protected Point getStringPlacement(Graphics g, String progressString,				       int x,int y,int width,int height) {	FontMetrics fontSizer = progressBar.getFontMetrics(	                                    progressBar.getFont());	int stringWidth = fontSizer.stringWidth(progressString);	if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {	    return new Point(x + Math.round(width/2 - stringWidth/2),			     y + ((height +                                 fontSizer.getAscent() -                                 fontSizer.getLeading() -                                 fontSizer.getDescent()) / 2));	} else { // VERTICAL            return new Point(x + ((width - fontSizer.getAscent() +                    fontSizer.getLeading() + fontSizer.getDescent()) / 2),		    y + Math.round(height/2 - stringWidth/2));	}    }            public Dimension getPreferredSize(JComponent c) {	Dimension	size;	Insets		border = progressBar.getInsets();	FontMetrics     fontSizer = progressBar.getFontMetrics(         	                                  progressBar.getFont());		if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {	    size = new Dimension(getPreferredInnerHorizontal());	    // Ensure that the progress string will fit	    if (progressBar.isStringPainted()) {		// I'm doing this for completeness.		String progString = progressBar.getString();		int stringWidth = fontSizer.stringWidth(progString);		if (stringWidth > size.width) {		    size.width = stringWidth;		}		// This uses both Height and Descent to be sure that 		// there is more than enough room in the progress bar		// for everything.		// This does have a strange dependency on 		// getStringPlacememnt() in a funny way.		int stringHeight = fontSizer.getHeight() +		                   fontSizer.getDescent();		if (stringHeight > size.height) {		    size.height = stringHeight;		}	    }	} else {	    size = new Dimension(getPreferredInnerVertical());	    // Ensure that the progress string will fit.	    if (progressBar.isStringPainted()) {		String progString = progressBar.getString();		int stringHeight = fontSizer.getHeight() +                        fontSizer.getDescent();		if (stringHeight > size.width) {		    size.width = stringHeight;		}		// This is also for completeness.		int stringWidth = fontSizer.stringWidth(progString);		if (stringWidth > size.height) {		    size.height = stringWidth;		}	    }	}	size.width += border.left + border.right;	size.height += border.top + border.bottom;	return size;    }    /**     * The Minimum size for this component is 10. The rationale here      * is that there should be at least one pixel per 10 percent.     */    public Dimension getMinimumSize(JComponent c) {	Dimension pref = getPreferredSize(progressBar);	if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {	    pref.width = 10;	} else {	    pref.height = 10;	}	return pref;    }    public Dimension getMaximumSize(JComponent c) {	Dimension pref = getPreferredSize(progressBar);	if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {	    pref.width = Short.MAX_VALUE;	} else {	    pref.height = Short.MAX_VALUE;	}	return pref;    }    /**     * Gets the index of the current animation frame.     *     * @since 1.4     */    protected int getAnimationIndex() {	return animationIndex;    }    /**     * Sets the index of the current animation frame     * to the specified value and requests that the     * progress bar be repainted.     * Subclasses that don't use the default painting code     * might need to override this method     * to change the way that the <code>repaint</code> method     * is invoked.     *     * @param newValue the new animation index; no checking     *                 is performed on its value     * @see #incrementAnimationIndex     *     * @since 1.4     */    protected void setAnimationIndex(int newValue) {        if (DEBUGALL) {            System.out.println("----begin setAnimationIndex----");            System.out.println("    argument = " + newValue);        }        if (animationIndex != newValue) {            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 {

⌨️ 快捷键说明

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