📄 basicsliderui.java
字号:
if ((!slider.getPaintTicks() && paintThumbArrowShape == null) || paintThumbArrowShape == Boolean.FALSE) { // "plain" version g.fillRect(0, 0, w, h); g.setColor(Color.black); g.drawLine(0, h-1, w-1, h-1); g.drawLine(w-1, 0, w-1, h-1); g.setColor(highlightColor); g.drawLine(0, 0, 0, h-2); g.drawLine(1, 0, w-2, 0); g.setColor(shadowColor); g.drawLine(1, h-2, w-2, h-2); g.drawLine(w-2, 1, w-2, h-3); } else if ( slider.getOrientation() == JSlider.HORIZONTAL ) { int cw = w / 2; g.fillRect(1, 1, w-3, h-1-cw); Polygon p = new Polygon(); p.addPoint(1, h-cw); p.addPoint(cw-1, h-1); p.addPoint(w-2, h-1-cw); g.fillPolygon(p); g.setColor(highlightColor); g.drawLine(0, 0, w-2, 0); g.drawLine(0, 1, 0, h-1-cw); g.drawLine(0, h-cw, cw-1, h-1); g.setColor(Color.black); g.drawLine(w-1, 0, w-1, h-2-cw); g.drawLine(w-1, h-1-cw, w-1-cw, h-1); g.setColor(shadowColor); g.drawLine(w-2, 1, w-2, h-2-cw); g.drawLine(w-2, h-1-cw, w-1-cw, h-2); } else { // vertical int cw = h / 2; if(BasicGraphicsUtils.isLeftToRight(slider)) { g.fillRect(1, 1, w-1-cw, h-3); Polygon p = new Polygon(); p.addPoint(w-cw-1, 0); p.addPoint(w-1, cw); p.addPoint(w-1-cw, h-2); g.fillPolygon(p); g.setColor(highlightColor); g.drawLine(0, 0, 0, h - 2); // left g.drawLine(1, 0, w-1-cw, 0); // top g.drawLine(w-cw-1, 0, w-1, cw); // top slant g.setColor(Color.black); g.drawLine(0, h-1, w-2-cw, h-1); // bottom g.drawLine(w-1-cw, h-1, w-1, h-1-cw); // bottom slant g.setColor(shadowColor); g.drawLine(1, h-2, w-2-cw, h-2 ); // bottom g.drawLine(w-1-cw, h-2, w-2, h-cw-1 ); // bottom slant } else { g.fillRect(5, 1, w-1-cw, h-3); Polygon p = new Polygon(); p.addPoint(cw, 0); p.addPoint(0, cw); p.addPoint(cw, h-2); g.fillPolygon(p); g.setColor(highlightColor); g.drawLine(cw-1, 0, w-2, 0); // top g.drawLine(0, cw, cw, 0); // top slant g.setColor(Color.black); g.drawLine(0, h-1-cw, cw, h-1 ); // bottom slant g.drawLine(cw, h-1, w-1, h-1); // bottom g.setColor(shadowColor); g.drawLine(cw, h-2, w-2, h-2 ); // bottom g.drawLine(w-1, 1, w-1, h-2 ); // right } } g.translate(-knobBounds.x, -knobBounds.y); } // Used exclusively by setThumbLocation() private static Rectangle unionRect = new Rectangle(); public void setThumbLocation(int x, int y) { unionRect.setBounds( thumbRect ); thumbRect.setLocation( x, y ); SwingUtilities.computeUnion( thumbRect.x, thumbRect.y, thumbRect.width, thumbRect.height, unionRect ); slider.repaint( unionRect.x, unionRect.y, unionRect.width, unionRect.height ); } public void scrollByBlock(int direction) { synchronized(slider) { int oldValue = slider.getValue(); int blockIncrement = (slider.getMaximum() - slider.getMinimum()) / 10; if (blockIncrement <= 0 && slider.getMaximum() > slider.getMinimum()) { blockIncrement = 1; } int delta = blockIncrement * ((direction > 0) ? POSITIVE_SCROLL : NEGATIVE_SCROLL); slider.setValue(oldValue + delta); } } public void scrollByUnit(int direction) { synchronized(slider) { int oldValue = slider.getValue(); int delta = 1 * ((direction > 0) ? POSITIVE_SCROLL : NEGATIVE_SCROLL); slider.setValue(oldValue + delta); } } /** * This function is called when a mousePressed was detected in the track, not * in the thumb. The default behavior is to scroll by block. You can * override this method to stop it from scrolling or to add additional behavior. */ protected void scrollDueToClickInTrack( int dir ) { scrollByBlock( dir ); } protected int xPositionForValue( int value ) { int min = slider.getMinimum(); int max = slider.getMaximum(); int trackLength = trackRect.width; double valueRange = (double)max - (double)min; double pixelsPerValue = (double)trackLength / valueRange; int trackLeft = trackRect.x; int trackRight = trackRect.x + (trackRect.width - 1); int xPosition; if ( !drawInverted() ) { xPosition = trackLeft; xPosition += Math.round( pixelsPerValue * ((double)value - min) ); } else { xPosition = trackRight; xPosition -= Math.round( pixelsPerValue * ((double)value - min) ); } xPosition = Math.max( trackLeft, xPosition ); xPosition = Math.min( trackRight, xPosition ); return xPosition; } protected int yPositionForValue( int value ) { return yPositionForValue(value, trackRect.y, trackRect.height); } /** * Returns the y location for the specified value. No checking is * done on the arguments. In particular if <code>trackHeight</code> 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 / (double)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 a value give a y position. If yPos is past the track at the top or the * bottom it will set the value to the min or max of the slider, depending 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 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. */ 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(); } } // 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") { 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 <Foo>. */ public class ChangeHandler implements ChangeListener { // NOTE: This class exists only for backward compatability. All // its functionality has been moved into Handler. If you need to add // new functionality add it to the Handler, but make sure this // class calls into the Handler. public void stateChanged(ChangeEvent e) { getHandler().stateChanged(e); } } ///////////////////////////////////////////////////////////////////////// /// Track Listener Class ///////////////////////////////////////////////////////////////////////// /** * Track mouse movements. * * This class should be treated as a "protected" inner class. * Instantiate it only within subclasses of <Foo>. */ public class TrackListener extends MouseInputAdapter { protected transient int offset; protected transient int currentMouseX, currentMouseY; public void mouseReleased(MouseEvent e) { if (!slider.isEnabled()) { return; } offset = 0; scrollTimer.stop(); // This is the way we have to determine snap-to-ticks. It's // hard to explain but since ChangeEvents don't give us any // idea what has changed we don't have a way to stop the thumb // bounds from being recalculated. Recalculating the thumb // bounds moves the thumb over the current value (i.e., snapping // to the ticks). if (slider.getSnapToTicks() /*|| slider.getSnapToValue()*/ ) { isDragging = false; slider.setValueIsAdjusting(false); } else { slider.setValueIsAdjusting(false); isDragging = false; } slider.repaint(); } /** * If the mouse is pressed above the "thumb" component * then reduce the scrollbars value by one page ("page up"), * otherwise increase it by one page. If there is no * thumb then page up if the mouse is in the upper half * of the track. */ public void mousePressed(MouseEvent e) { if (!slider.isEnabled()) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -