2

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);
    }
}
John Smith
  • 3,493
  • 3
  • 25
  • 52

1 Answers1

3

At the time paintThumb() is called, BasicSliderUI has already calculated thumbRect based on the result of getThumbSize(). Moreover, because of its unknown latency, you should not invokeImageIO.read() inside paint(). Instead, load the image asynchronously, perhaps using SwingWorker as shown here, and use the image's dimensions in your implementation of getThumbSize(). Note that you should take the result of getOrientation() into account if your image is not square.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I so appreciate SO and those that help! I just put thumbRect.setSize(20,40) in calculateThumbSize() override and it worked. Not sure exactly how to simplify the image handling but this is definitely the answer. I'll update my OP – John Smith Aug 25 '15 at 17:38
  • If I move the ImageIO out of paintThumb and into the parent method (customSliderHandle) and place the declaration in the class (like I did with sBusType) will that be ok? – John Smith Aug 25 '15 at 17:49
  • @JohnSmith: Passing a reference in the constructor sounds right. – trashgod Aug 25 '15 at 20:12