synthprogressbarui.java
来自「java jdk 1.4的源码」· Java 代码 · 共 1,230 行 · 第 1/3 页
JAVA
1,230 行
// orientation is. } } } else { progressBounds = getBox(boxRect); } SynthLookAndFeel.paintForeground(context, g, progressBounds); if (pBar.isStringPainted() && !isIndeterminate) { paintText(context, g, pBar.getString()); } } protected void paintText(SynthContext context, Graphics g, String title) { Font font = context.getStyle().getFont(context); FontMetrics metrics = g.getFontMetrics(font); if (progressBar.isStringPainted()) { String pBarString = progressBar.getString(); Rectangle bounds = progressBar.getBounds(); int strLength = metrics.stringWidth(pBarString); // Calculate the bounds for the text. Rectangle textRect = new Rectangle( (bounds.width / 2) - (strLength / 2), (bounds.height - (metrics.getAscent() + metrics.getDescent())) / 2, 0, 0); // Progress bar isn't tall enough for the font. Don't paint it. if (textRect.y < 0) { return; } // Paint the text. SynthStyle style = context.getStyle(); g.setColor(style.getColor(context, ColorType.TEXT_FOREGROUND)); g.setFont(style.getFont(context)); style.getSynthGraphics(context).paintText(context, g, title, textRect.x, textRect.y, -1); } } /** * 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 */ public 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; // Insets on the progress sub region. r.height -= 2; r.width -= 2; 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; // Insets on the progress sub region. r.height -= 2; r.width -= 2; 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); } 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); } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?