自定义的进度条

jdk自带的JProgressBar效果不太符合需求,如下是一个跑马灯式渐进效果的progressbar,效果就是一段渐进色的矩形来回滚动。
实现原理很简单,就是用一个线程循环绘制一个矩形区域,并用渐进色填充。详细代码如下。
public class ProcessBar extends JComponent implements ActionListener {

private static final long serialVersionUID = 1L;

private String showText;



private final int TIMER_DELAY = 41;

private final Dimension PREFERRED_SIZE = new Dimension(240, 16);

private int tailLength;

private int increment;

private boolean movingRight;

private int currentPosition;

private Timer timer;

private boolean constructed;

public ProcessBar() {
tailLength = 120;
increment = 13;
movingRight = true;
currentPosition = 0;
constructed = false;
timer = new Timer(TIMER_DELAY, this);
timer.setRepeats(true);
setAttributes();
Insets insets = getBorder().getBorderInsets(this);
Dimension d = PREFERRED_SIZE;
d.width += insets.left + insets.right;
d.height += insets.top + insets.bottom;
setPreferredSize(d);
setMinimumSize(d);
constructed = true;
}

public void setShowText(String text) {
this.showText = text;
}

public void removeNotify() {
super.removeNotify();
if (timer != null) {
timer.stop();
timer.removeActionListener(this);
timer = null;
}
}

public void addNotify() {
super.addNotify();
if (timer == null) {
timer = new Timer(41, this);
timer.setRepeats(true);
}
}

public void actionPerformed(ActionEvent e) {
repaint();
}

public void updateUI() {
super.updateUI();
if (constructed)
setAttributes();
}

public void setIncrement(int increment) {
if (increment > 0 && increment < 500)
this.increment = increment;
}

public int getIncrement() {
return increment;
}

public void setTailLength(int tailLength) {
if (tailLength > 0 && tailLength < 500)
this.tailLength = tailLength;
}

public int getTailLength() {
return tailLength;
}

protected void setAttributes() {
setOpaque(true);
if (getBackground() == null
|| (getBackground() instanceof ColorUIResource))
setBackground(new ColorUIResource(UIManager.getColor("control")));
if (getForeground() == null
|| (getForeground() instanceof ColorUIResource))
setForeground(new ColorUIResource(UIManager
.getColor("ProgressBar.foreground")));
if (getBorder() == null || (getBorder() instanceof BorderUIResource))
setBorder(createDefaultBorder());
}

protected Border createDefaultBorder() {
return new BorderUIResource(BorderFactory.createCompoundBorder(
BorderFactory.createEtchedBorder(0, UIManager
.getColor("controlLtHighlight"), UIManager
.getColor("controlShadow")),
BorderFactory.createCompoundBorder(new GradientBorder(),
BorderFactory.createLineBorder(GradientBorder.getBrighterColor(
UIManager.getColor("controlShadow"), 0.4F)))));
}

public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Color background = getBackground();
Color foreground = getForeground();
Insets insets = getInsets();
Rectangle rect = getBounds();
int height = rect.height - (insets.top + insets.bottom);
int width = rect.width - (insets.left + insets.right);
currentPosition += increment;
if (currentPosition > (rect.width - (insets.left + insets.right))
+ tailLength + increment * 2) {
currentPosition = 0;
movingRight = !movingRight;
}
g2.setColor(background);
g2.fillRect(g.getClipBounds().x, g.getClipBounds().y,
g.getClipBounds().width, g.getClipBounds().height);
int x = 0;
background = new Color(foreground.getRed(), foreground.getGreen(),
foreground.getBlue(), 0);
if (movingRight) {
x = (insets.left + currentPosition) - tailLength;
Color temp = foreground;
foreground = background;
background = temp;
} else {
x = (insets.left + rect.width) - currentPosition;
}
GradientPaint gp = new GradientPaint(x, (float) insets.top
+ (float) (height / 2), foreground, x + tailLength, insets.top
+ height / 2, background, false);
g2.setPaint(gp);
Rectangle r = new Rectangle(x, insets.top, tailLength, height);
r = r
.intersection(new Rectangle(insets.left, insets.top, width,
height));
r = r.intersection(g.getClipBounds());
g2.fillRect(r.x, r.y, r.width, r.height);

g2.setPaint(Color.black);
if(showText != null){
g2.drawString(showText, 170, 16);
}
if (timer != null && !timer.isRunning())
timer.start();
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值