📄 synthsliderui.java
字号:
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; // Check if any of the labels will paint out of bounds. int pad = 0; if (slider.getPaintLabels()) { // Calculate the track rectangle. It is necessary for // xPositionForValue to return correct values. trackRect.x = insetCache.left; trackRect.width = contentDim.width; Dictionary dictionary = slider.getLabelTable(); if (dictionary != null) { int minValue = slider.getMinimum(); int maxValue = slider.getMaximum(); // Iterate through the keys in the dictionary and find the // first and last labels indices that fall within the // slider range. int firstLblIdx = Integer.MAX_VALUE; int lastLblIdx = Integer.MIN_VALUE; for (Enumeration keys = dictionary.keys(); keys.hasMoreElements(); ) { int keyInt = ((Integer)keys.nextElement()).intValue(); if (keyInt >= minValue && keyInt < firstLblIdx) { firstLblIdx = keyInt; } if (keyInt <= maxValue && keyInt > lastLblIdx) { lastLblIdx = keyInt; } } // Calculate the pad necessary for the labels at the first // and last visible indices. pad = getPadForLabel(firstLblIdx); pad = Math.max(pad, getPadForLabel(lastLblIdx)); } } // Calculate the painting rectangles for each of the different // slider areas. valueRect.x = trackRect.x = tickRect.x = labelRect.x = (insetCache.left + pad); valueRect.width = trackRect.width = tickRect.width = labelRect.width = (contentDim.width - (pad * 2)); int centerY = slider.getHeight() / 2 - contentDim.height / 2; 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(context); } // Get the max width of the min or max value of the slider. FontMetrics fm = slider.getFontMetrics(slider.getFont()); valueRect.width = Math.max( synthGraphics.computeStringWidth(context, slider.getFont(), fm, "" + slider.getMaximum()), synthGraphics.computeStringWidth(context, slider.getFont(), fm, "" + slider.getMinimum())); int l = valueRect.width / 2; int w1 = trackInsets.left + trackRect.width / 2; int w2 = trackRect.width / 2 + trackInsets.right + tickRect.width + labelRect.width; contentDim.width = Math.max(w1, l) + Math.max(w2, l) + 2 + insetCache.left + insetCache.right; contentDim.height = slider.getHeight() - insetCache.top - insetCache.bottom; // Layout the components. trackRect.y = tickRect.y = labelRect.y = valueRect.y + valueRect.height; trackRect.height = tickRect.height = labelRect.height = contentDim.height - valueRect.height; int startX = slider.getWidth() / 2 - contentDim.width / 2; if (SynthLookAndFeel.isLeftToRight(slider)) { if (l > w1) { startX += (l - w1); } trackRect.x = startX + trackInsets.left; startX += trackInsets.left + trackRect.width + trackInsets.right; tickRect.x = startX; labelRect.x = startX + tickRect.width + 2; } else { if (l > w2) { startX += (l - w2); } labelRect.x = startX; startX += labelRect.width + 2; tickRect.x = startX; trackRect.x = startX + tickRect.width + trackInsets.left; } } context.dispose(); lastSize = slider.getSize(); } /** * Calculates the pad for the label at the specified index. * * @param index index of the label to calculate pad for. * @return padding required to keep label visible. */ private int getPadForLabel(int i) { Dictionary dictionary = slider.getLabelTable(); int pad = 0; Object o = dictionary.get(i); if (o != null) { Component c = (Component)o; int centerX = xPositionForValue(i); int cHalfWidth = c.getPreferredSize().width / 2; if (centerX - cHalfWidth < insetCache.left) { pad = Math.max(pad, insetCache.left - (centerX - cHalfWidth)); } if (centerX + cHalfWidth > slider.getWidth() - insetCache.right) { pad = Math.max(pad, (centerX + cHalfWidth) - (slider.getWidth() - insetCache.right)); } } return pad; } 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); } Point mousePosition = slider.getMousePosition(); if(mousePosition != null) { updateThumbState(mousePosition.x, mousePosition.y); } } 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) { super.setThumbLocation(x, y); // Value rect is tied to the thumb location. We need to repaint when // the thumb repaints. slider.repaint(valueRect.x, valueRect.y, valueRect.width, valueRect.height); setThumbActive(false); } 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 trackY, int trackHeight) { int min = slider.getMinimum(); int max = slider.getMaximum(); int trackTop = trackY + thumbRect.height / 2 + trackBorder; int trackBottom = trackY + trackHeight - 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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -