【发布时间】:2015-08-25 16:05:16
【问题描述】:
我正在尝试覆盖paintThumb 以显示图形滑块拇指。只要我不弄乱图像的大小并让它根据拇指的现有大小进行绘制,拇指图像和滑块就可以正常工作。如果我更改 thumbRect 或我的图像的大小,我无法抓住拇指或移动滑块不再起作用。这是一个垂直的 JSlider。
注意:感谢垃圾神提供的解决方案。见代码。
注 2:已更新为现在可接受的示例。
注意 3:原始问题是由于在paintThumb 中调整大小造成的。
这是滑块:
sliders[channel].setUI(new customSliderHandle(sliders[channel], busType));
课程如下:
private static class customSliderHandle extends BasicSliderUI {
String sBusType;
Image image;
public customSliderHandle(JSlider slider, String busType) {
super(slider);
sBusType = busType;
try {
if(sBusType.equals("ch")) {
//noinspection ConstantConditions
image = ImageIO.read(getClass().getClassLoader().getResource("web/images/black-slider.png"));
} else {
//noinspection ConstantConditions
image = ImageIO.read(getClass().getClassLoader().getResource("web/images/yellow-slider.png"));
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void calculateThumbSize() {
super.calculateThumbSize();
//// Fixed!!!
thumbRect.setSize(20, 40);
}
@Override
public void paintThumb(Graphics g) {
///// This was the original problem. Move into
///// calculateThumbSize() resolved the problem.
///// thumbRect.setSize(20, 40);
int x = thumbRect.x;
int y = thumbRect.y;
int width = thumbRect.width;
int height = thumbRect.height;
g.drawImage(image, x, y, width, height, null);
}
}
【问题讨论】: