📄 basicsliderui.java
字号:
{ Object comp = list.nextElement(); if (! (comp instanceof Component)) continue; label = (Component) comp; pref = label.getPreferredSize(); if (pref != null && pref.height > tallest) tallest = pref.height; } return tallest; } /** * This method returns the width of the label whose key has the highest * value. * * @return The width of the high value label or 0 if no label table exists. */ protected int getWidthOfHighValueLabel() { Component highValueLabel = getHighestValueLabel(); if (highValueLabel != null) return highValueLabel.getWidth(); else return 0; } /** * This method returns the width of the label whose key has the lowest * value. * * @return The width of the low value label or 0 if no label table exists. */ protected int getWidthOfLowValueLabel() { Component lowValueLabel = getLowestValueLabel(); if (lowValueLabel != null) return lowValueLabel.getWidth(); else return 0; } /** * This method returns the height of the label whose key has the highest * value. * * @return The height of the high value label or 0 if no label table exists. */ protected int getHeightOfHighValueLabel() { Component highValueLabel = getHighestValueLabel(); if (highValueLabel != null) return highValueLabel.getHeight(); else return 0; } /** * This method returns the height of the label whose key has the lowest * value. * * @return The height of the low value label or 0 if no label table exists. */ protected int getHeightOfLowValueLabel() { Component lowValueLabel = getLowestValueLabel(); if (lowValueLabel != null) return lowValueLabel.getHeight(); else return 0; } /** * This method returns whether the slider is to be drawn inverted. * * @return True is the slider is to be drawn inverted. */ protected boolean drawInverted() { return ! (slider.getInverted() ^ leftToRightCache); } /** * This method returns the label whose key has the lowest value. * * @return The low value label or null if no label table exists. */ protected Component getLowestValueLabel() { Integer key = new Integer(Integer.MAX_VALUE); Integer tmpKey; Dictionary labelTable = slider.getLabelTable(); if (labelTable == null) return null; for (Enumeration list = labelTable.keys(); list.hasMoreElements();) { Object value = list.nextElement(); if (! (value instanceof Integer)) continue; tmpKey = (Integer) value; if (tmpKey.intValue() < key.intValue()) key = tmpKey; } Object comp = labelTable.get(key); if (! (comp instanceof Component)) return null; return (Component) comp; } /** * This method returns the label whose key has the highest value. * * @return The high value label or null if no label table exists. */ protected Component getHighestValueLabel() { Integer key = new Integer(Integer.MIN_VALUE); Integer tmpKey; Dictionary labelTable = slider.getLabelTable(); if (labelTable == null) return null; for (Enumeration list = labelTable.keys(); list.hasMoreElements();) { Object value = list.nextElement(); if (! (value instanceof Integer)) continue; tmpKey = (Integer) value; if (tmpKey.intValue() > key.intValue()) key = tmpKey; } Object comp = labelTable.get(key); if (! (comp instanceof Component)) return null; return (Component) comp; } /** * This method is used to paint the {@link JSlider}. It delegates all its * duties to the various paint methods like paintTicks(), paintTrack(), * paintThumb(), etc. * * @param g The {@link Graphics} object to paint with. * @param c The {@link JComponent} that is being painted. */ public void paint(Graphics g, JComponent c) { // FIXME: Move this to propertyChangeEvent handler, when we get those. leftToRightCache = slider.getComponentOrientation() != ComponentOrientation.RIGHT_TO_LEFT; // FIXME: This next line is only here because the above line is here. calculateGeometry(); if (slider.getPaintTrack()) paintTrack(g); if (slider.getPaintTicks()) paintTicks(g); if (slider.getPaintLabels()) paintLabels(g); //FIXME: Paint focus. paintThumb(g); } /** * This method recalculates any rectangles that need to be recalculated * after the insets of the component have changed. */ protected void recalculateIfInsetsChanged() { // Examining a test program shows that either Sun calls private // methods that we don't know about, or these don't do anything. calculateFocusRect(); calculateContentRect(); calculateThumbSize(); calculateTrackBuffer(); calculateTrackRect(); calculateThumbLocation(); calculateTickRect(); calculateLabelRect(); } /** * This method recalculates any rectangles that need to be recalculated * after the orientation of the slider changes. */ protected void recalculateIfOrientationChanged() { // Examining a test program shows that either Sun calls private // methods that we don't know about, or these don't do anything. calculateThumbSize(); calculateTrackBuffer(); calculateTrackRect(); calculateThumbLocation(); calculateTickRect(); calculateLabelRect(); } /** * This method is called during a repaint if the slider has focus. It draws * an outline of the focusRect using the color returned by * getFocusColor(). * * @param g The {@link Graphics} object to draw with. */ public void paintFocus(Graphics g) { Color saved_color = g.getColor(); g.setColor(getFocusColor()); g.drawRect(focusRect.x, focusRect.y, focusRect.width, focusRect.height); g.setColor(saved_color); } /** * <p> * This method is called during a repaint if the track is to be drawn. It * draws a 3D rectangle to represent the track. The track is not the size * of the trackRect. The top and left edges of the track should be outlined * with the shadow color. The bottom and right edges should be outlined * with the highlight color. * </p> * <pre> * a---d * | | * | | a------------------------d * | | | | * | | b------------------------c * | | * | | * b---c * </pre> * * <p> * The b-a-d path needs to be drawn with the shadow color and the b-c-d path * needs to be drawn with the highlight color. * </p> * * @param g The {@link Graphics} object to draw with. */ public void paintTrack(Graphics g) { Color saved_color = g.getColor(); int width; int height; Point a = new Point(trackRect.x, trackRect.y); Point b = new Point(a); Point c = new Point(a); Point d = new Point(a); if (slider.getOrientation() == JSlider.HORIZONTAL) { width = trackRect.width; height = (thumbRect.height / 4 == 0) ? 1 : thumbRect.height / 4; a.translate(0, (trackRect.height / 2) - (height / 2)); b.translate(0, (trackRect.height / 2) + (height / 2)); c.translate(trackRect.width, (trackRect.height / 2) + (height / 2)); d.translate(trackRect.width, (trackRect.height / 2) - (height / 2)); } else { width = (thumbRect.width / 4 == 0) ? 1 : thumbRect.width / 4; height = trackRect.height; a.translate((trackRect.width / 2) - (width / 2), 0); b.translate((trackRect.width / 2) - (width / 2), trackRect.height); c.translate((trackRect.width / 2) + (width / 2), trackRect.height); d.translate((trackRect.width / 2) + (width / 2), 0); } g.setColor(Color.GRAY); g.fillRect(a.x, a.y, width, height); g.setColor(getHighlightColor()); g.drawLine(b.x, b.y, c.x, c.y); g.drawLine(c.x, c.y, d.x, d.y); g.setColor(getShadowColor()); g.drawLine(b.x, b.y, a.x, a.y); g.drawLine(a.x, a.y, d.x, d.y); g.setColor(saved_color); } /** * This method is called during a repaint if the ticks are to be drawn. This * method must still verify that the majorTickSpacing and minorTickSpacing * are greater than zero before drawing the ticks. * * @param g The {@link Graphics} object to draw with. */ public void paintTicks(Graphics g) { int max = slider.getMaximum(); int min = slider.getMinimum(); int majorSpace = slider.getMajorTickSpacing(); int minorSpace = slider.getMinorTickSpacing(); if (majorSpace > 0) { if (slider.getOrientation() == JSlider.HORIZONTAL) { double loc = tickRect.x + 0.5; double increment = (max == min) ? 0 : majorSpace * (double) (tickRect.width - 1) / (max - min); if (drawInverted()) { loc += tickRect.width; increment *= -1; } g.translate(0, tickRect.y); for (int i = min; i <= max; i += majorSpace) { paintMajorTickForHorizSlider(g, tickRect, (int) loc); loc += increment; } g.translate(0, -tickRect.y); } else { double loc = tickRect.height + tickRect.y + 0.5; double increment = (max == min) ? 0 : -majorSpace * (double) (tickRect.height - 1) / (max - min); if (drawInverted()) { loc = tickRect.y + 0.5; increment *= -1; } g.translate(tickRect.x, 0); for (int i = min; i <= max; i += majorSpace) { paintMajorTickForVertSlider(g, tickRect, (int) loc); loc += increment; } g.translate(-tickRect.x, 0); } } if (minorSpace > 0) { if (slider.getOrientation() == JSlider.HORIZONTAL) { double loc = tickRect.x + 0.5; double increment = (max == min) ? 0 : minorSpace * (double) (tickRect.width - 1) / (max - min); if (drawInverted()) { loc += tickRect.width; increment *= -1; } g.translate(0, tickRect.y); for (int i = min; i <= max; i += minorSpace) { paintMinorTickForHorizSlider(g, tickRect, (int) loc); loc += increment; } g.translate(0, -tickRect.y); } else { double loc = tickRect.height + tickRect.y + 0.5; double increment = (max == min) ? 0 : -minorSpace * (double) (tickRect.height - 1) / (max - min); if (drawInverted()) { loc = tickRect.y + 0.5; increment *= -1; } g.translate(tickRect.x, 0); for (int i = min; i <= max; i += minorSpace) { paintMinorTickForVertSlider(g, tickRect, (int) loc); loc += increment; } g.translate(-tickRect.x, 0); } } } /* Minor ticks start at 1/4 of the height (or width) of the tickRect and extend to 1/2 of the tickRect. Major ticks start at 1/4 of the height and extend to 3/4. */ /** * This method paints a minor tick for a horizontal slider at the given x * value. x represents the x coordinate to paint at. * * @param g The {@link Graphics} object to draw with. * @param tickBounds The tickRect rectangle. * @param x The x coordinate to draw the tick at. */ protected void paintMinorTickForHorizSlider(Graphics g, Rectangle tickBounds, int x) { int y = tickRect.height / 4; Color saved = g.getColor(); g.setColor(Color.BLACK); g.drawLine(x, y, x, y + tickRect.height / 4); g.setColor(saved); } /** * This method paints a major tick for a horizontal slider at the given x * value. x represents the x coordinate to paint at. * * @param g The {@link Graphics} object to draw with. * @param tickBounds The tickRect rectangle. * @param x The x coordinate to draw the tick at. */ protected void paintMajorTickForHorizSlider(Graphics g, Rectangle tickBounds, int x) { int y = tickRect.height / 4; Color saved = g.getColor(); g.setColor(Color.BLACK); g.drawLine(x, y, x, y + tickRect.height / 2); g.setColor(saved); } /** * This method paints a minor tick for a vertical slider at the given y * value. y represents the y coordinate to paint at. * * @param g The {@link Graphics} object to draw with. * @param tickBounds The tickRect rectangle. * @param y The y coordinate to draw the tick at. */ protected void paintMinorTickForVertSlider(Graphics g, Rectangle tickBounds, int y) { int x = tickRect.width / 4; Color saved = g.getColor(); g.setColor(Color.BLACK); g.drawLine(x, y, x + tickRect.width / 4, y); g.setColor(saved);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -