andronix3 / SwingHacks

BSD 3-Clause "New" or "Revised" License
2 stars 2 forks source link

Improve Knob Grabbing #2

Closed smasher816 closed 8 years ago

smasher816 commented 8 years ago

Thanks for this neat little library. I wanted to use a RangeSlider but JIDE was too big of a dependency for just one item and https://ernienotes.wordpress.com/2010/12/27/creating-a-java-swing-range-slider/ lacked features like ticks because of the custom ui. Yours fit the bill nicely. It's a shame more people have not found it :+1:

Unfortunately in my testing I found that grabbing the knobs was very awkward. With the default parameters the range was way too small, forcing you to put your cursor towards the inside of the handles. If you put it in the middle or towards the outside your grabs would be ignored. I tried tweaking the constants in mouseMoved but ended up with strange results. With larger values you could grab the knobs past their rendered image on the outer sides, yet still not be able to grab the very inner images. It was clear there was some issue with the math/scaling not centering around the knobs correctly.

On a hunt for clues I looked at BasicSliderUI to see how they determined if a knob was grabbed. It turns out that they use rectangle clipping detection to check if the mouse is over the image. Unfortunately the rectangles it was using were protected and unable to be accessed from your code. However, by using a custom subclass of BasicSliderUI I made a public function to access the protected variable. With access to these variables checking for the various states became trivial.

With these changes grabbing the knobs works perfectly both horizontally and vertically. In my mind not using magic constants is a good thing; what if swing changed their images to a larger one? Suddenly the old acceptable range of '3' would be even more broken. With the rectangle clipping this issue is no longer present.

I hope this change helps you (future) others.

andronix3 commented 8 years ago

Hi Rowan,

at first, thank you for your help,I really appretiate it.

Unfortunately your changes broke thumb rendering (since RangeSliderUI extends BasicSliderUI).

We must seach another solution.

Best regards

Andrey

smasher816 commented 8 years ago

Hi Andrey,

could you post an image of the issue? I have tested both Metal and GTK UI's and the thumbs render fine. If you want you could try my latest commit that simplifies the paint call a lot. https://github.com/smasher816/SwingHacks/commit/24d10c558267393b2ddc0a102f44ff22121cb480

This is what it looks like for me: http://imgur.com/a/wHeXb

andronix3 commented 8 years ago

Hi Rowan,

screenshot attached.

I tried to debug it - it uses painting method from BasicSliderUI.

Best regards

Andrey

Von: Rowan Decker [mailto:notifications@github.com] Gesendet: Freitag, 18. Dezember 2015 20:35 An: andronix3/SwingHacks SwingHacks@noreply.github.com Cc: Andrey Kuznetsov andronix@gmx.net Betreff: Re: [SwingHacks] Improve Knob Grabbing (#2)

Hi Andrey,

could you post an image of the issue? I have tested both Metal and GTK UI's and the thumbs render fine. If you want you could try my latest commit that simplifies the paint call a lot. smasher816@ https://github.com/smasher816/SwingHacks/commit/24d10c558267393b2ddc0a102f44ff22121cb480 24d10c5

This is what it looks like for me: http://imgur.com/a/wHeXb

— Reply to this email directly or view it on GitHub https://github.com/andronix3/SwingHacks/pull/2#issuecomment-165878613 . https://github.com/notifications/beacon/AMFsujEsQ66OJCjMvgbBDsIsNLqX5avTks5pRFdtgaJpZM4G3-qp.gif

smasher816 commented 8 years ago

Unfortunately I can't seem to find an attached image. I'm not sure if github supports email attachments as these replies are also displayed through the website.

On Fri, Dec 18, 2015 at 1:42 PM, Andrey Kuznetsov notifications@github.com wrote:

Hi Rowan,

screenshot attached.

I tried to debug it - it uses painting method from BasicSliderUI.

Best regards

Andrey

Von: Rowan Decker [mailto:notifications@github.com] Gesendet: Freitag, 18. Dezember 2015 20:35 An: andronix3/SwingHacks SwingHacks@noreply.github.com Cc: Andrey Kuznetsov andronix@gmx.net Betreff: Re: [SwingHacks] Improve Knob Grabbing (#2)

Hi Andrey,

could you post an image of the issue? I have tested both Metal and GTK UI's and the thumbs render fine. If you want you could try my latest commit that simplifies the paint call a lot. smasher816@ < https://github.com/smasher816/SwingHacks/commit/24d10c558267393b2ddc0a102f44ff22121cb480> 24d10c5

This is what it looks like for me: http://imgur.com/a/wHeXb

— Reply to this email directly or view it on GitHub < https://github.com/andronix3/SwingHacks/pull/2#issuecomment-165878613> . < https://github.com/notifications/beacon/AMFsujEsQ66OJCjMvgbBDsIsNLqX5avTks5pRFdtgaJpZM4G3-qp.gif>

— Reply to this email directly or view it on GitHub https://github.com/andronix3/SwingHacks/pull/2#issuecomment-165903766.

andronix3 commented 8 years ago

http://imgur.com/Zfv4OXY

Von: Rowan Decker [mailto:notifications@github.com] Gesendet: Freitag, 18. Dezember 2015 22:47 An: andronix3/SwingHacks SwingHacks@noreply.github.com Cc: Andrey Kuznetsov andronix@gmx.net Betreff: Re: [SwingHacks] Improve Knob Grabbing (#2)

Unfortunately I can't seem to find an attached image. I'm not sure if github supports email attachments as these replies are also displayed through the website.

On Fri, Dec 18, 2015 at 1:42 PM, Andrey Kuznetsov <notifications@github.com mailto:notifications@github.com > wrote:

Hi Rowan,

screenshot attached.

I tried to debug it - it uses painting method from BasicSliderUI.

Best regards

Andrey

Von: Rowan Decker [mailto:notifications@github.com] Gesendet: Freitag, 18. Dezember 2015 20:35 An: andronix3/SwingHacks <SwingHacks@noreply.github.com mailto:SwingHacks@noreply.github.com > Cc: Andrey Kuznetsov <andronix@gmx.net mailto:andronix@gmx.net > Betreff: Re: [SwingHacks] Improve Knob Grabbing (#2)

Hi Andrey,

could you post an image of the issue? I have tested both Metal and GTK UI's and the thumbs render fine. If you want you could try my latest commit that simplifies the paint call a lot. smasher816@ < https://github.com/smasher816/SwingHacks/commit/24d10c558267393b2ddc0a102f44ff22121cb480> 24d10c5

This is what it looks like for me: http://imgur.com/a/wHeXb

— Reply to this email directly or view it on GitHub < https://github.com/andronix3/SwingHacks/pull/2#issuecomment-165878613> . < https://github.com/notifications/beacon/AMFsujEsQ66OJCjMvgbBDsIsNLqX5avTks5pRFdtgaJpZM4G3-qp.gif>

— Reply to this email directly or view it on GitHub https://github.com/andronix3/SwingHacks/pull/2#issuecomment-165903766.

— Reply to this email directly or view it on GitHub https://github.com/andronix3/SwingHacks/pull/2#issuecomment-165904692 . https://github.com/notifications/beacon/AMFsuoAZ67tRiBHyL7X_D54TfjkVg_L3ks5pRHY2gaJpZM4G3-qp.gif

andronix3 commented 8 years ago

Hi Rowan,

I found the solution.

We need RangeSliderUI just for thumb calculations.

So instead of slider.setUI() we call installUI(slider) and keep reference on it.

Best regards

Andrey

Von: Rowan Decker [mailto:notifications@github.com] Gesendet: Freitag, 18. Dezember 2015 22:47 An: andronix3/SwingHacks SwingHacks@noreply.github.com Cc: Andrey Kuznetsov andronix@gmx.net Betreff: Re: [SwingHacks] Improve Knob Grabbing (#2)

Unfortunately I can't seem to find an attached image. I'm not sure if github supports email attachments as these replies are also displayed through the website.

On Fri, Dec 18, 2015 at 1:42 PM, Andrey Kuznetsov <notifications@github.com mailto:notifications@github.com > wrote:

Hi Rowan,

screenshot attached.

I tried to debug it - it uses painting method from BasicSliderUI.

Best regards

Andrey

Von: Rowan Decker [mailto:notifications@github.com] Gesendet: Freitag, 18. Dezember 2015 20:35 An: andronix3/SwingHacks <SwingHacks@noreply.github.com mailto:SwingHacks@noreply.github.com > Cc: Andrey Kuznetsov <andronix@gmx.net mailto:andronix@gmx.net > Betreff: Re: [SwingHacks] Improve Knob Grabbing (#2)

Hi Andrey,

could you post an image of the issue? I have tested both Metal and GTK UI's and the thumbs render fine. If you want you could try my latest commit that simplifies the paint call a lot. smasher816@ < https://github.com/smasher816/SwingHacks/commit/24d10c558267393b2ddc0a102f44ff22121cb480> 24d10c5

This is what it looks like for me: http://imgur.com/a/wHeXb

— Reply to this email directly or view it on GitHub < https://github.com/andronix3/SwingHacks/pull/2#issuecomment-165878613> . < https://github.com/notifications/beacon/AMFsujEsQ66OJCjMvgbBDsIsNLqX5avTks5pRFdtgaJpZM4G3-qp.gif>

— Reply to this email directly or view it on GitHub https://github.com/andronix3/SwingHacks/pull/2#issuecomment-165903766.

— Reply to this email directly or view it on GitHub https://github.com/andronix3/SwingHacks/pull/2#issuecomment-165904692 . https://github.com/notifications/beacon/AMFsuoAZ67tRiBHyL7X_D54TfjkVg_L3ks5pRHY2gaJpZM4G3-qp.gif