使用paintThumb,我的箭头在 0 或 100 时从 JSlider 上掉下来,如何解决这个问题?
图片说明了一切。我画了一个新的 Thumb,当高于 95 或低于 5 时,它会离开 JSlider 区域。我尝试填充 Track,但没有成功。
有人有任何提示吗?
这是我的代码。我相信我的问题是在 getThumbSize 覆盖下转动拇指的大小。
private static class MySliderUI extends BasicSliderUI {
private int thumbHeight = 22;
private int thumbWidth = 22;
public MySliderUI(JSlider b) {
super(b);
// this.thumbHeight = slider.getHeight();
// this.thumbWidth = slider.getHeight();
}
@Override
protected Dimension getThumbSize() {
return new Dimension(thumbHeight, thumbWidth);
}
@Override
public void paintTrack(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Rectangle r = trackRect;
r.width = 40; // (int) (0.15 * r.getHeight());
float[] dist = { 0.1f, 0.5f, 0.9f };
Color[] colors = { new Color(34, 177, 76), new Color(255, 242, 0),
new Color(237, 28, 36) };
Point2D start = new Point2D.Float(r.x, r.y);
Point2D end = new Point2D.Float(r.x, r.y + r.height);
LinearGradientPaint p = new LinearGradientPaint(start, end, dist,
colors);
g2d.setPaint(p);
g2d.fill(r);
}
@Override
public void paintThumb(Graphics g) {
Graphics2D g1 = (Graphics2D) g;
// Make a triangle - Arrow on Meter
int[] x = new int[3];
int[] y = new int[3];
int n; // count of points
// Set Points for Arrow
Integer arrowX = thumbRect.x;
Integer arrowY = thumbRect.y;
x[0] = arrowX - 25;
x[1] = arrowX - 25;
x[2] = arrowX - 2;
y[0] = arrowY - 17; // Top Left
y[1] = arrowY + 27; // Bottom Left
y[2] = arrowY + 5; // Tip of Arrow
n = 3; // Number of points, 3 because its a triangle
// Draw Arrow Border
Polygon myTriShadow = new Polygon(x, y, n); // a triangle
g1.setPaint(Color.black);
g1.fill(myTriShadow);
// Set Points for Arrow Board
x[0] = x[0] + 2;
x[1] = x[1] + 2;
x[2] = x[2] - 3;
y[0] = y[0] + 5;
y[1] = y[1] - 5;
y[2] = y[2];
// Color colorMeter = robot.getPixelColor(x[2]+10, y[2]);
// Draw Arrow
Polygon myTri = new Polygon(x, y, n); // a triangle
// Color colr = new Color();
g1.setPaint(Color.yellow);
g1.fill(myTri);
// super.paintThumb(g); // replace with your fill()
}
}
The picture explains it all. I've painted a new Thumb and it goes off the JSlider area when at higher than 95 or below 5. I've tried padding the Track with no success.
Does anyone have any tips?
Here is my code. I believe my issue is fine turning the size of the thumb under the getThumbSize override.
private static class MySliderUI extends BasicSliderUI {
private int thumbHeight = 22;
private int thumbWidth = 22;
public MySliderUI(JSlider b) {
super(b);
// this.thumbHeight = slider.getHeight();
// this.thumbWidth = slider.getHeight();
}
@Override
protected Dimension getThumbSize() {
return new Dimension(thumbHeight, thumbWidth);
}
@Override
public void paintTrack(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Rectangle r = trackRect;
r.width = 40; // (int) (0.15 * r.getHeight());
float[] dist = { 0.1f, 0.5f, 0.9f };
Color[] colors = { new Color(34, 177, 76), new Color(255, 242, 0),
new Color(237, 28, 36) };
Point2D start = new Point2D.Float(r.x, r.y);
Point2D end = new Point2D.Float(r.x, r.y + r.height);
LinearGradientPaint p = new LinearGradientPaint(start, end, dist,
colors);
g2d.setPaint(p);
g2d.fill(r);
}
@Override
public void paintThumb(Graphics g) {
Graphics2D g1 = (Graphics2D) g;
// Make a triangle - Arrow on Meter
int[] x = new int[3];
int[] y = new int[3];
int n; // count of points
// Set Points for Arrow
Integer arrowX = thumbRect.x;
Integer arrowY = thumbRect.y;
x[0] = arrowX - 25;
x[1] = arrowX - 25;
x[2] = arrowX - 2;
y[0] = arrowY - 17; // Top Left
y[1] = arrowY + 27; // Bottom Left
y[2] = arrowY + 5; // Tip of Arrow
n = 3; // Number of points, 3 because its a triangle
// Draw Arrow Border
Polygon myTriShadow = new Polygon(x, y, n); // a triangle
g1.setPaint(Color.black);
g1.fill(myTriShadow);
// Set Points for Arrow Board
x[0] = x[0] + 2;
x[1] = x[1] + 2;
x[2] = x[2] - 3;
y[0] = y[0] + 5;
y[1] = y[1] - 5;
y[2] = y[2];
// Color colorMeter = robot.getPixelColor(x[2]+10, y[2]);
// Draw Arrow
Polygon myTri = new Polygon(x, y, n); // a triangle
// Color colr = new Color();
g1.setPaint(Color.yellow);
g1.fill(myTri);
// super.paintThumb(g); // replace with your fill()
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
覆盖 < code>getThumbSize(),返回拇指边框的尺寸。如果不对称,您应该考虑滑块的
方向
。附录:顺便说一句,拇指顶部没有“脱落”;它被剪掉了。请参阅在 AWT 和 Swing 中绘制有关剪辑矩形的更多信息。
Override
getThumbSize()
, returning the dimensions of the bounding rectangle of your thumb. If not symmetric, you should account for the slider'sorientation
.Addendum: As an aside, the top of the thumb didn't "fall off"; it was clipped. See Painting in AWT and Swing for more about the clip rectangle.