📄 synthsliderui.java
字号:
int trackBottom = trackRect.y + trackRect.height - thumbRect.height / 2 - trackBorder; int trackLength = trackBottom - trackTop; 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; 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; } protected Dimension getThumbSize() { Dimension size = new Dimension(); if (slider.getOrientation() == JSlider.VERTICAL) { size.width = thumbHeight; size.height = thumbWidth; } else { size.width = thumbWidth; size.height = thumbHeight; } return size; } protected void recalculateIfInsetsChanged() { SynthContext context = getContext(slider); Insets newInsets = style.getInsets(context, null); if (!newInsets.equals(insetCache)) { insetCache = newInsets; calculateGeometry(); } context.dispose(); } public Region getRegion(JComponent c) { return SynthLookAndFeel.getRegion(c); } public SynthContext getContext(JComponent c) { return getContext(c, getComponentState(c)); } public SynthContext getContext(JComponent c, int state) { return SynthContext.getContext(SynthContext.class, c, SynthLookAndFeel.getRegion(c), style, state); } public SynthContext getContext(JComponent c, Region subregion) { return getContext(c, subregion, getComponentState(c, subregion)); } private SynthContext getContext(JComponent c, Region subregion, int state) { SynthStyle style = null; Class klass = SynthContext.class; if (subregion == Region.SLIDER_TRACK) { style = sliderTrackStyle; } else if (subregion == Region.SLIDER_THUMB) { style = sliderThumbStyle; } return SynthContext.getContext(klass, c, subregion, style, state); } public int getComponentState(JComponent c) { return SynthLookAndFeel.getComponentState(c); } private int getComponentState(JComponent c, Region region) { if (region == Region.SLIDER_THUMB && thumbActive &&c.isEnabled()) { int state = thumbPressed ? PRESSED : MOUSE_OVER; if (c.isFocusOwner()) state |= FOCUSED; return state; } return SynthLookAndFeel.getComponentState(c); } public void update(Graphics g, JComponent c) { SynthContext context = getContext(c); SynthLookAndFeel.update(context, g); context.getPainter().paintSliderBackground(context, g, 0, 0, c.getWidth(), c.getHeight(), slider.getOrientation()); paint(context, g); context.dispose(); } public void paint(Graphics g, JComponent c) { SynthContext context = getContext(c); paint(context, g); context.dispose(); } public void paint(SynthContext context, Graphics g) { recalculateIfInsetsChanged(); recalculateIfOrientationChanged(); Rectangle clip = g.getClipBounds(); if (lastSize == null || !lastSize.equals(slider.getSize())) { calculateGeometry(); } if (paintValue) { FontMetrics fm = SwingUtilities2.getFontMetrics(slider, g); int labelWidth = context.getStyle().getGraphicsUtils(context). computeStringWidth(context, g.getFont(), fm, "" + slider.getValue()); valueRect.x = thumbRect.x + (thumbRect.width - labelWidth) / 2; // For horizontal sliders, make sure value is not painted // outside slider bounds. if (slider.getOrientation() == JSlider.HORIZONTAL) { if (valueRect.x + labelWidth > contentDim.width) { valueRect.x = contentDim.width - labelWidth; } valueRect.x = Math.max(valueRect.x, 0); } g.setColor(context.getStyle().getColor( context, ColorType.TEXT_FOREGROUND)); context.getStyle().getGraphicsUtils(context).paintText( context, g, "" + slider.getValue(), valueRect.x, valueRect.y, -1); } SynthContext subcontext = getContext(slider, Region.SLIDER_TRACK); paintTrack(subcontext, g, trackRect); subcontext.dispose(); subcontext = getContext(slider, Region.SLIDER_THUMB); paintThumb(subcontext, g, thumbRect); subcontext.dispose(); if (slider.getPaintTicks() && clip.intersects(tickRect)) { paintTicks(g); } if (slider.getPaintLabels() && clip.intersects(labelRect)) { paintLabels(g); } } public void paintBorder(SynthContext context, Graphics g, int x, int y, int w, int h) { context.getPainter().paintSliderBorder(context, g, x, y, w, h, slider.getOrientation()); } public void paintThumb(SynthContext context, Graphics g, Rectangle thumbBounds) { int orientation = slider.getOrientation(); SynthLookAndFeel.updateSubregion(context, g, thumbBounds); context.getPainter().paintSliderThumbBackground(context, g, thumbBounds.x, thumbBounds.y, thumbBounds.width, thumbBounds.height, orientation); context.getPainter().paintSliderThumbBorder(context, g, thumbBounds.x, thumbBounds.y, thumbBounds.width, thumbBounds.height, orientation); } public void paintTrack(SynthContext context, Graphics g, Rectangle trackBounds) { int orientation = slider.getOrientation(); SynthLookAndFeel.updateSubregion(context, g, trackBounds); context.getPainter().paintSliderTrackBackground(context, g, trackBounds.x, trackBounds.y, trackBounds.width, trackBounds.height, orientation); context.getPainter().paintSliderTrackBorder(context, g, trackBounds.x, trackBounds.y, trackBounds.width, trackBounds.height, orientation); } public void propertyChange(PropertyChangeEvent e) { if (SynthLookAndFeel.shouldUpdateStyle(e)) { updateStyle((JSlider)e.getSource()); } } ////////////////////////////////////////////////// /// Track Listener Class ////////////////////////////////////////////////// /** * Track mouse movements. */ protected class SynthTrackListener extends TrackListener { public void mouseExited(MouseEvent e) { setThumbActive(false); } public void mousePressed(MouseEvent e) { super.mousePressed(e); setThumbPressed(thumbRect.contains(e.getX(), e.getY())); } public void mouseReleased(MouseEvent e) { super.mouseReleased(e); updateThumbState(e.getX(), e.getY(), false); } public void mouseDragged(MouseEvent e) { SynthScrollBarUI ui; int thumbMiddle = 0; if (!slider.isEnabled()) { return; } currentMouseX = e.getX(); currentMouseY = e.getY(); if (!isDragging()) { return; } slider.setValueIsAdjusting(true); switch (slider.getOrientation()) { case JSlider.VERTICAL: int halfThumbHeight = thumbRect.height / 2; int thumbTop = e.getY() - offset; int trackTop = trackRect.y; int trackBottom = trackRect.y + trackRect.height - halfThumbHeight - trackBorder; int vMax = yPositionForValue(slider.getMaximum() - slider.getExtent()); if (drawInverted()) { trackBottom = vMax; trackTop = trackTop + halfThumbHeight; } else { trackTop = vMax; } thumbTop = Math.max(thumbTop, trackTop - halfThumbHeight); thumbTop = Math.min(thumbTop, trackBottom - halfThumbHeight); setThumbLocation(thumbRect.x, thumbTop); thumbMiddle = thumbTop + halfThumbHeight; slider.setValue(valueForYPosition(thumbMiddle)); break; case JSlider.HORIZONTAL: int halfThumbWidth = thumbRect.width / 2; int thumbLeft = e.getX() - offset; int trackLeft = trackRect.x + halfThumbWidth + trackBorder; int trackRight = trackRect.x + trackRect.width - halfThumbWidth - trackBorder; int hMax = xPositionForValue(slider.getMaximum() - slider.getExtent()); if (drawInverted()) { trackLeft = hMax; } else { trackRight = hMax; } thumbLeft = Math.max(thumbLeft, trackLeft - halfThumbWidth); thumbLeft = Math.min(thumbLeft, trackRight - halfThumbWidth); setThumbLocation(thumbLeft, thumbRect.y); thumbMiddle = thumbLeft + halfThumbWidth; slider.setValue(valueForXPosition(thumbMiddle)); break; default: return; } if (slider.getValueIsAdjusting()) { setThumbActive(true); } } public void mouseMoved(MouseEvent e) { updateThumbState(e.getX(), e.getY()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -