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

📄 basicprogressbarui.java

📁 JAVA 所有包
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     */    protected Rectangle getBox(Rectangle r) {        int currentFrame = getAnimationIndex();        int middleFrame = numFrames/2;        if (sizeChanged() || delta == 0.0 || maxPosition == 0.0) {            updateSizes();        }        r = getGenericBox(r);         if (r == null) {            return null;        }        if (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));            }        }        return r;    }    /**     * Updates delta, max position.     * Assumes componentInnards is correct (e.g. call after sizeChanged()).     */    private void 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.        delta = 2.0 * (double)maxPosition/(double)numFrames;    }    /**     * Assumes that the component innards, max position, etc. are up-to-date.     */    private Rectangle getGenericBox(Rectangle 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        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>     *     * @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.5     */    protected 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);                if (barRectWidth <= 0 || barRectHeight <= 0) {            return;        }        Graphics2D g2 = (Graphics2D)g;        // Paint the bouncing box.        boxRect = getBox(boxRect);        if (boxRect != null) {            g2.setColor(progressBar.getForeground());            g2.fillRect(boxRect.x, boxRect.y,                       boxRect.width, boxRect.height);        }	// 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);                if (barRectWidth <= 0 || barRectHeight <= 0) {            return;        }        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));	    }            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());	    SwingUtilities2.drawString(progressBar, g2, progressString,                                       renderLocation.x, renderLocation.y);	    g2.setColor(getSelectionForeground());            g2.clipRect(fillStart, y, amountFull, height);	    SwingUtilities2.drawString(progressBar, g2, 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);	    SwingUtilities2.drawString(progressBar, g2, progressString,                                       renderLocation.x, renderLocation.y);	    g2.setColor(getSelectionForeground());	    g2.clipRect(x, fillStart, width, amountFull);	    SwingUtilities2.drawString(progressBar, g2, 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 = SwingUtilities2.getFontMetrics(progressBar, g,	                                    progressBar.getFont());	int stringWidth = SwingUtilities2.stringWidth(progressBar, fontSizer,                                                      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 = SwingUtilities2.stringWidth(                          progressBar, fontSizer, 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 = SwingUtilities2.stringWidth(                                       progressBar, fontSizer, progString);

⌨️ 快捷键说明

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