📄 progressbarui.java
字号:
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(TonicUtils.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(TonicUtils.isLeftToRight(progressBar))
{
paintString(g, x, y, width, height, x, amountFull, b);
}
else
{
paintString(g, x, y, width, height, x+width-amountFull, amountFull,
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));
}
}
/** Returns the preferred size for the specified component */
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;
}
/** Returns the maximum size for the specified component */
public Dimension getMinimumSize(JComponent c)
{
Dimension pref=getPreferredSize(progressBar);
if(progressBar.getOrientation()==JProgressBar.HORIZONTAL)
{
pref.width=10;
}
else
{
pref.height=10;
}
return pref;
}
/** Returns the maximum size for the specified component */
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)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -