I'm trying to override paintThumb to show a graphic slider thumb. As long as I don't mess with size of the image and let it paint according to the existing size of the thumb the thumb image and slider functions fine. If I change the size of thumbRect or my image, I cannot grab thumb or moving the slider not longer works. This is a vertical JSlider.
NOTE: Thanks to trashgod for the solution. See in code.
NOTE2: UPDATED TO NOW BE A WORKING ACCEPTABLE EXAMPLE.
NOTE3: Original problem was caused by doing resize inside paintThumb.
Here's the slider:
sliders[channel].setUI(new customSliderHandle(sliders[channel], busType));
Here's the class:
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);
}
}