我尝试提高我的Java技能(挥杆)!我想要一个自定义的JSlider。我希望像这个截图(底部)一样具有相同的外观:
因为,在我成功的那一刻,创建一个圆形轨道并填充它。对于轨道部分,我不知道如何绘制滑块的前进。这是我的问题。我成功地拥有了一条灰色的轨道,但是如何画出这个画面呢?这是我的代码:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import javax.swing.JComponent;
import javax.swing.JSlider;
import javax.swing.plaf.basic.BasicSliderUI;
public class CustomJSlider extends BasicSliderUI {
public CustomJSlider(JSlider b) {
super(b);
}
public void paintTicks(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Rectangle t = trackRect;
g2d.setPaint(new Color(0,91,91));
g2d.fillRoundRect(t.x, t.y, t.width, t.height, 5, 5);
}
@Override
public void paint(Graphics g, JComponent c) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
super.paint(g, c);
}
@Override
protected Dimension getThumbSize() {
return new Dimension(15, 15);
}
@Override
public void paintTrack(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Rectangle t = trackRect;
g2d.setPaint(new Color(91,91,91));
g2d.fillRoundRect(t.x, t.y+3, t.width, t.height-5, 5, 5);
}
@Override
public void paintThumb(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
Rectangle t = thumbRect;
Shape circle = new Ellipse2D.Double(t.x, t.y, t.width, t.height);
// GradientPaint blue = new GradientPaint(25, 25, new Color(86,85,30), 15, 25, Color.black, true);
g2d.setPaint(new Color(30,173,216));
g2d.fill(circle);
g2d.draw(circle);
}
}
那么如何绘制曲目 avancement ?这是最好的方式吗?