茅侃侃
扩展了TrashGod非常有用的答案,我在几行代码中添加了一些代码,以便将条形图填充到移动它的位置。栏的其余部分将为空白:执行此操作的代码是:@Overridepublic void paintTrack(Graphics g) {
// ... TrashGod's code ...
// calculate how much of the progress bar to fill
double percentage = (double)slider.getValue()/(double)slider.getMaximum();
// fill the progress bar with a rectange of that size, (with curved corners of 4px diameter)
g2d.fillRoundRect(t.x, t.y, (int)(t.width*percentage), t.height, 4, 4);
// ...}如果你想要一个背景颜色,在绘制第一个颜色之前绘制第二个rectange: // ... TrashGod's code ...
// calculate how much of the progress bar to fill
double percentage = (double)slider.getValue()/(double)slider.getMaximum();
// PAINT THE BACKGROUND
// create a gradient paint for the background
p = new LinearGradientPaint(start, end, new float[] {0.4f,0.8f}, new Color[] {Color.gray.brighter(), Color.gray.brighter().brighter()});
g2d.setPaint(p);
g2d.fillRoundRect((int)(t.width*percentage), t.y, t.width - (int)(t.width*percentage), t.height, 4, 4);
// PAINT THE FOREGROUND
// create the gradient paint
p = new LinearGradientPaint(start, end, fracs, colors);
g2d.setPaint(p);
// fill the progress bar with a rectange of that size, (with curved corners of 4px diameter)
g2d.fillRoundRect(t.x, t.y, (int)(t.width*percentage), t.height, 4, 4);