/** * 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 (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; }
/** * This determines the amount of the progress bar that should be filled based on the percent done * gathered from the model. This is a common operation so it was abstracted out. It assumes that * your progress bar is linear. That is, if you are making a circular progress indicator, you will * want to override this method. */ protected int getAmountFull(Insets b, int width, int height) { int amountFull = 0; BoundedRangeModel model = progressBar.getModel(); if ((model.getMaximum() - model.getMinimum()) != 0) { if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) { amountFull = (int) Math.round(width * progressBar.getPercentComplete()); } else { amountFull = (int) Math.round(height * progressBar.getPercentComplete()); } } return amountFull; }
/** * 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)); } }
/** * 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); }