繪制JSlider的滑塊圖標想要重新標記標記或拇指JSlider而不是標準灰色。我怎樣才能做到這一點?
3 回答

慕標5832272
TA貢獻1966條經驗 獲得超4個贊
擴展了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);
添加回答
舉報
0/150
提交
取消