public int valueForXPosition ( int xPos ) { int value ; int minValue = slider . getMinimum ( ) ; int maxValue = slider . getMaximum ( ) ; int trackLeft = trackRect . x + thumbRect . width / 2 + trackBorder ; int trackRight = trackRect . x + trackRect . width - thumbRect . width / 2 - trackBorder ; int trackLength = trackRight - trackLeft ; if ( xPos <= trackLeft ) { value = drawInverted ( ) ? maxValue : minValue ; } else if ( xPos >= trackRight ) { value = drawInverted ( ) ? minValue : maxValue ; } else { int distanceFromTrackLeft = xPos - trackLeft ; double valueRange = ( double ) maxValue - ( double ) minValue ; double valuePerPixel = valueRange / ( double ) trackLength ; int valueFromTrackLeft = ( int ) Math . round ( distanceFromTrackLeft * valuePerPixel ) ; value = drawInverted ( ) ? maxValue - valueFromTrackLeft : minValue + valueFromTrackLeft ; } return value ; }
Returns a value give an x position . If xPos is past the track at the left or the right it will set the value to the min or max of the slider depending if the slider is inverted or not .