trackHeight
is
* negative undefined results may occur.
*
* @param value the slider value to get the location for
* @param trackY y-origin of the track
* @param trackHeight the height of the track
* @since 1.6
*/
protected int yPositionForValue(int value, int trackY, int trackHeight) {
int min = slider.getMinimum();
int max = slider.getMaximum();
double valueRange = (double)max - (double)min;
double pixelsPerValue = (double)trackHeight / valueRange;
int trackBottom = trackY + (trackHeight - 1);
int yPosition;
if ( !drawInverted() ) {
yPosition = trackY;
yPosition += Math.round( pixelsPerValue * ((double)max - value ) );
}
else {
yPosition = trackY;
yPosition += Math.round( pixelsPerValue * ((double)value - min) );
}
yPosition = Math.max( trackY, yPosition );
yPosition = Math.min( trackBottom, yPosition );
return yPosition;
}
/**
* Returns the value at the y position. If {@code yPos} is beyond the
* track at the the bottom or the top, this method sets the value to either
* the minimum or maximum value of the slider, depending on if the slider
* is inverted or not.
*/
public int valueForYPosition( int yPos ) {
int value;
final int minValue = slider.getMinimum();
final int maxValue = slider.getMaximum();
final int trackLength = trackRect.height;
final int trackTop = trackRect.y;
final int trackBottom = trackRect.y + (trackRect.height - 1);
if ( yPos <= trackTop ) {
value = drawInverted() ? minValue : maxValue;
}
else if ( yPos >= trackBottom ) {
value = drawInverted() ? maxValue : minValue;
}
else {
int distanceFromTrackTop = yPos - trackTop;
double valueRange = (double)maxValue - (double)minValue;
double valuePerPixel = valueRange / (double)trackLength;
int valueFromTrackTop = (int)Math.round( distanceFromTrackTop * valuePerPixel );
value = drawInverted() ? minValue + valueFromTrackTop : maxValue - valueFromTrackTop;
}
return value;
}
/**
* Returns the value at the x position. If {@code xPos} is beyond the
* track at the left or the right, this method sets the value to either the
* minimum or maximum value of the slider, depending on if the slider is
* inverted or not.
*/
public int valueForXPosition( int xPos ) {
int value;
final int minValue = slider.getMinimum();
final int maxValue = slider.getMaximum();
final int trackLength = trackRect.width;
final int trackLeft = trackRect.x;
final int trackRight = trackRect.x + (trackRect.width - 1);
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;
}
private class Handler implements ChangeListener,
ComponentListener, FocusListener, PropertyChangeListener {
// Change Handler
public void stateChanged(ChangeEvent e) {
if (!isDragging) {
calculateThumbLocation();
slider.repaint();
}
lastValue = slider.getValue();
}
// Component Handler
public void componentHidden(ComponentEvent e) { }
public void componentMoved(ComponentEvent e) { }
public void componentResized(ComponentEvent e) {
calculateGeometry();
slider.repaint();
}
public void componentShown(ComponentEvent e) { }
// Focus Handler
public void focusGained(FocusEvent e) { slider.repaint(); }
public void focusLost(FocusEvent e) { slider.repaint(); }
// Property Change Handler
public void propertyChange(PropertyChangeEvent e) {
String propertyName = e.getPropertyName();
if (propertyName == "orientation" ||
propertyName == "inverted" ||
propertyName == "labelTable" ||
propertyName == "majorTickSpacing" ||
propertyName == "minorTickSpacing" ||
propertyName == "paintTicks" ||
propertyName == "paintTrack" ||
propertyName == "font" ||
propertyName == "paintLabels" ||
propertyName == "Slider.paintThumbArrowShape") {
checkedLabelBaselines = false;
calculateGeometry();
slider.repaint();
} else if (propertyName == "componentOrientation") {
calculateGeometry();
slider.repaint();
InputMap km = getInputMap(JComponent.WHEN_FOCUSED, slider);
SwingUtilities.replaceUIInputMap(slider,
JComponent.WHEN_FOCUSED, km);
} else if (propertyName == "model") {
((BoundedRangeModel)e.getOldValue()).removeChangeListener(
changeListener);
((BoundedRangeModel)e.getNewValue()).addChangeListener(
changeListener);
calculateThumbLocation();
slider.repaint();
}
}
}
/////////////////////////////////////////////////////////////////////////
/// Model Listener Class
/////////////////////////////////////////////////////////////////////////
/**
* Data model listener.
*
* This class should be treated as a "protected" inner class.
* Instantiate it only within subclasses of
* This class should be treated as a "protected" inner class.
* Instantiate it only within subclasses of
* This class should be treated as a "protected" inner class.
* Instantiate it only within subclasses of
* Please refer to the key bindings specification for further details.
*
* This class should be treated as a "protected" inner class.
* Instantiate it only within subclasses of ActionMap
, to contain the action,
* and an InputMap
to contain the mapping from KeyStroke
* to action description. The InputMap is is usually described in the
* LookAndFeel tables.
*