synthsliderui.java
来自「java jdk 1.4的源码」· Java 代码 · 共 1,576 行 · 第 1/4 页
JAVA
1,576 行
trackRect.height = trackHeight; tickRect.height = 0; if (slider.getPaintTicks()) { tickRect.height = getTickLength(); } labelRect.height = 0; if (slider.getPaintLabels()) { labelRect.height = getHeightOfTallestLabel(); } contentDim.height = valueRect.height + trackRect.height + trackInsets.top + trackInsets.bottom + tickRect.height + labelRect.height + 4; contentDim.width = slider.getWidth() - insetCache.left - insetCache.right; int centerY = slider.getHeight() / 2 - contentDim.height / 2; // Layout the components. valueRect.x = trackRect.x = tickRect.x = labelRect.x = insetCache.left; valueRect.width = trackRect.width = tickRect.width = labelRect.width = contentDim.width; valueRect.y = centerY; centerY += valueRect.height + 2; trackRect.y = centerY + trackInsets.top; centerY += trackRect.height + trackInsets.top + trackInsets.bottom; tickRect.y = centerY; centerY += tickRect.height + 2; labelRect.y = centerY; centerY += labelRect.height; } else { // Calculate the width of all the subcomponents so we can center // them. trackRect.width = trackHeight; tickRect.width = 0; if (slider.getPaintTicks()) { tickRect.width = getTickLength(); } labelRect.width = 0; if (slider.getPaintLabels()) { labelRect.width = getWidthOfWidestLabel(); } valueRect.y = insetCache.top; valueRect.height = 0; if (paintValue) { valueRect.height = synthGraphics.getMaximumCharHeight(slider); } contentDim.width = trackRect.width + trackInsets.left + trackInsets.right + tickRect.width + labelRect.width + 2 + insetCache.left + insetCache.right; contentDim.height = slider.getHeight() - insetCache.top - insetCache.bottom; int startX = slider.getWidth() / 2 - contentDim.width / 2; // Get the max width of the min or max value of the slider. valueRect.width = Math.max( synthGraphics.computeStringWidth(context, slider.getFont(), slider.getToolkit().getFontMetrics(slider.getFont()), "" + slider.getMaximum()), synthGraphics.computeStringWidth(context, slider.getFont(), slider.getToolkit().getFontMetrics(slider.getFont()), "" + slider.getMinimum())); // Check to see if we need to make the width larger due to the size // of the value string. The value string is centered above the // track. if (valueRect.width > (trackRect.width + trackInsets.left + trackInsets.right)) { int diff = (valueRect.width - (trackRect.width + trackInsets.left + trackInsets.right)) / 2; contentDim.width += diff; startX += diff; } // Layout the components. trackRect.y = tickRect.y = labelRect.y = valueRect.y + valueRect.height; trackRect.height = tickRect.height = labelRect.height = contentDim.height - valueRect.height; trackRect.x = startX + trackInsets.left; startX += trackRect.width + trackInsets.right + trackInsets.left; tickRect.x = startX; startX += tickRect.width + 2; labelRect.x = startX; startX += labelRect.width; } } protected void calculateThumbSize() { Dimension size = getThumbSize(); thumbRect.setSize(size.width, size.height); } protected Color getShadowColor() { return shadowColor; } protected Color getHighlightColor() { return highlightColor; } protected void calculateThumbLocation() { if (slider.getSnapToTicks()) { int sliderValue = slider.getValue(); int snappedValue = sliderValue; int majorTickSpacing = slider.getMajorTickSpacing(); int minorTickSpacing = slider.getMinorTickSpacing(); int tickSpacing = 0; if (minorTickSpacing > 0) { tickSpacing = minorTickSpacing; } else if (majorTickSpacing > 0) { tickSpacing = majorTickSpacing; } if (tickSpacing != 0) { // If it's not on a tick, change the value if ((sliderValue - slider.getMinimum()) % tickSpacing != 0) { float temp = (float)(sliderValue - slider.getMinimum()) / (float)tickSpacing; int whichTick = Math.round( temp ); snappedValue = slider.getMinimum() + (whichTick * tickSpacing); } if (snappedValue != sliderValue) { slider.setValue(snappedValue); } } } if (slider.getOrientation() == JSlider.HORIZONTAL) { int valuePosition = xPositionForValue(slider.getValue()); thumbRect.x = valuePosition - (thumbRect.width / 2); thumbRect.y = trackRect.y + trackBorder; } else { int valuePosition = yPositionForValue(slider.getValue()); thumbRect.x = trackRect.x + trackBorder; thumbRect.y = valuePosition - (thumbRect.height / 2); } } /** * Gets the height of the tick area for horizontal sliders and the width * of the tick area for vertical sliders. SynthSliderUI uses the returned * value to determine the tick area rectangle. If you want to give your * ticks some room, make this larger than you need and paint your ticks * away from the sides in paintTicks(). */ protected int getTickLength() { return 8; } protected void calculateTickRect() { if (slider.getOrientation() == JSlider.HORIZONTAL) { tickRect.x = trackRect.x; tickRect.y = trackRect.y + trackRect.height + 2 + getTickLength(); tickRect.width = trackRect.width; tickRect.height = getTickLength(); if (!slider.getPaintTicks()) { --tickRect.y; tickRect.height = 0; } } else { if (SynthLookAndFeel.isLeftToRight(slider)) { tickRect.x = trackRect.x + trackRect.width; tickRect.width = getTickLength(); } else { tickRect.width = getTickLength(); tickRect.x = trackRect.x - tickRect.width; } tickRect.y = trackRect.y; tickRect.height = trackRect.height; if (!slider.getPaintTicks()) { --tickRect.x; tickRect.width = 0; } } } 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); // Value rect is tied to the thumb location. We need to repaint when // the thumb repaints. // PENDING (joutwate): look into optimizing the width that we repaint. slider.repaint(valueRect.x, valueRect.y, valueRect.width, valueRect.height); setThumbActive(false); } 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 trackLeft = trackRect.x + thumbRect.width / 2 + trackBorder; int trackRight = trackRect.x + trackRect.width - thumbRect.width / 2 - trackBorder; int trackLength = trackRight - trackLeft; double valueRange = (double)max - (double)min; double pixelsPerValue = (double)trackLength / valueRange; 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) { int min = slider.getMinimum(); int max = slider.getMaximum(); int trackTop = trackRect.y + thumbRect.height / 2 + trackBorder; int trackBottom = trackRect.y + trackRect.height - thumbRect.height / 2 - trackBorder; int trackLength = trackBottom - trackTop; double valueRange = (double)max - (double)min; double pixelsPerValue = (double)trackLength / (double)valueRange; int yPosition; if (!drawInverted()) { yPosition = trackTop; yPosition += Math.round(pixelsPerValue * ((double)max - value)); } else { yPosition = trackTop; yPosition += Math.round(pixelsPerValue * ((double)value - min)); } yPosition = Math.max(trackTop, 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; int minValue = slider.getMinimum(); int maxValue = slider.getMaximum(); int trackTop = trackRect.y + thumbRect.height / 2 + trackBorder; 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 int getWidthOfWidestLabel() { Dictionary dictionary = slider.getLabelTable(); int widest = 0; if (dictionary != null) { Enumeration keys = dictionary.keys(); while (keys.hasMoreElements()) { Component label = (Component)dictionary.get(keys.nextElement()); widest = Math.max(label.getPreferredSize().width, widest); } } return widest; } protected int getHeightOfTallestLabel() { Dictionary dictionary = slider.getLabelTable(); int tallest = 0; if (dictionary != null) { Enumeration keys = dictionary.keys(); while (keys.hasMoreElements()) { Component label = (Component)dictionary.get(keys.nextElement());
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?