import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import javax.swing.BoundedRangeModel;
import javax.swing.JProgressBar;
public class MyJProgressBar2 extends JProgressBar
{
private static final long serialVersionUID = 7011699579650095776L;
private boolean stringPaintedOverride = false;
private Color textColorOutsidePaint = Color.BLACK;
private Color textColorInsidePaint = Color.WHITE;
public MyJProgressBar2()
{
setStringPainted(false);
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if(stringPaintedOverride)
{
drawString(g);
}
}
@Override
public void setStringPainted(boolean painted)
{
super.setStringPainted(false);
setStringPaintedOverride(painted);
}
private void setStringPaintedOverride(boolean stringPaintedOverride)
{
this.stringPaintedOverride = stringPaintedOverride;
}
public void setTextColorOutsidePaint(Color textColorOutsidePaint)
{
this.textColorOutsidePaint = textColorOutsidePaint;
}
public void setTextColorInsidePaint(Color textColorInsidePaint)
{
this.textColorInsidePaint = textColorInsidePaint;
}
private void drawString(Graphics g)
{
g.setFont(getFont());
Rectangle oldClip = g.getClipBounds();
Insets b = getInsets(); // area for border
int barRectWidth = getWidth() - (b.right + b.left);
int barRectHeight = getHeight() - (b.top + b.bottom);
int amountFull = 0;
BoundedRangeModel model = getModel();
if ( (model.getMaximum() - model.getMinimum()) != 0) {
if (getOrientation() == JProgressBar.HORIZONTAL) {
amountFull = (int)Math.round(barRectWidth *
getPercentComplete());
} else {
amountFull = (int)Math.round(barRectHeight *
getPercentComplete());
}
}
int fillStart;
Point renderLocation = getStringPlacement(g);
if (getOrientation() == JProgressBar.HORIZONTAL)
{
if(getComponentOrientation().isLeftToRight())
fillStart = b.left;
else
fillStart = b.left + barRectWidth - amountFull;
g.setColor(textColorOutsidePaint);
drawString(g, renderLocation.x, renderLocation.y);
g.setColor(textColorInsidePaint);
g.clipRect(fillStart, b.top, amountFull, barRectHeight);
drawString(g, renderLocation.x, renderLocation.y);
}
else
{
// VERTICAL
fillStart = b.top + barRectHeight - amountFull;
AffineTransform rotate = AffineTransform.getRotateInstance(Math.PI / 2);
g.setFont(getFont().deriveFont(rotate));
g.setColor(textColorOutsidePaint);
drawString(g, renderLocation.x, renderLocation.y);
g.setColor(textColorInsidePaint);
g.clipRect(b.left, fillStart, barRectWidth, amountFull);
drawString(g, renderLocation.x, renderLocation.y);
}
g.setClip(oldClip);
}
private Point getStringPlacement(Graphics g)
{
FontMetrics fontSizer = getFontMetrics(getFont());
if (getOrientation() == JProgressBar.HORIZONTAL)
{
int x = (getWidth() / 2) - (int)Math.round(fontSizer.getStringBounds(progressString, g).getWidth() / 2);
int y = (getHeight() - (int)Math.round((getHeight()-fontSizer.getStringBounds(progressString, g).getHeight()) / 2)) - 3;
return new Point(x, y);
}
else
{
// VERTICAL
int x = ((getWidth() / 2) - (int)Math.round(fontSizer.getStringBounds(progressString, g).getHeight() / 2)) + 4;
int y = (int)Math.round((getHeight() - fontSizer.getStringBounds(progressString, g).getWidth()) / 2);
return new Point(x, y);
}
}
private void drawString(Graphics g, int x, int y)
{
if(g != null)
{
if(isIndeterminate())
g.setColor(Color.BLACK);
g.drawString(progressString, x, y);
}
}
}