synthsliderui.java

来自「java jdk 1.4的源码」· Java 代码 · 共 1,576 行 · 第 1/4 页

JAVA
1,576
字号
                tallest = Math.max(label.getPreferredSize().height, tallest);            }        }        return tallest;    }    protected int getWidthOfHighValueLabel() {        Component label = getHighestValueLabel();        int width = 0;        if (label != null) {            width = label.getPreferredSize().width;        }        return width;    }    protected int getWidthOfLowValueLabel() {        Component label = getLowestValueLabel();        int width = 0;        if (label != null) {            width = label.getPreferredSize().width;        }        return width;    }    protected int getHeightOfHighValueLabel() {        Component label = getHighestValueLabel();        int height = 0;        if (label != null) {            height = label.getPreferredSize().height;        }        return height;    }    protected int getHeightOfLowValueLabel() {        Component label = getLowestValueLabel();        int height = 0;        if (label != null) {            height = label.getPreferredSize().height;        }        return height;    }    protected boolean drawInverted() {        if (slider.getOrientation() == JSlider.HORIZONTAL) {            if (SynthLookAndFeel.isLeftToRight(slider)) {                return slider.getInverted();            } else {                return !slider.getInverted();            }        } else {            return slider.getInverted();        }    }    /**     * Returns the label that corresponds to the highest slider value in the     * label table.     * @see JSlider#setLabelTable     */    protected Component getLowestValueLabel() {        Dictionary dictionary = slider.getLabelTable();        Component label = null;        if (dictionary != null) {            Enumeration keys = dictionary.keys();            if (keys.hasMoreElements()) {                int lowestValue = ((Integer)keys.nextElement()).intValue();                while (keys.hasMoreElements()) {                    int value = ((Integer)keys.nextElement()).intValue();                    lowestValue = Math.min(value, lowestValue);                }                label = (Component)dictionary.get(new Integer(lowestValue));            }        }        return label;    }    /**     * Returns the label that corresponds to the lowest slider value in the     * label table.     * @see JSlider#setLabelTable     */    protected Component getHighestValueLabel() {        Dictionary dictionary = slider.getLabelTable();        Component label = null;        if (dictionary != null) {            Enumeration keys = dictionary.keys();            if (keys.hasMoreElements()) {                int highestValue = ((Integer)keys.nextElement()).intValue();                while (keys.hasMoreElements()) {                    int value = ((Integer)keys.nextElement()).intValue();                    highestValue = Math.max(value, highestValue);                }                label = (Component)dictionary.get(new Integer(highestValue));            }        }        return label;    }    protected void recalculateIfInsetsChanged() {        Insets newInsets = style.getInsets(getContext(slider), null);        if (!newInsets.equals(insetCache)) {            insetCache = newInsets;            calculateGeometry();        }    }    protected void recalculateIfOrientationChanged() {        boolean ltr = SynthLookAndFeel.isLeftToRight(slider);        if (ltr != leftToRightCache) {            leftToRightCache = ltr;            calculateGeometry();        }    }    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()) {            return MOUSE_OVER;        }        return SynthLookAndFeel.getComponentState(c);    }    public void update(Graphics g, JComponent c) {        SynthContext context = getContext(c);        SynthLookAndFeel.update(context, g);        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();        valueRect.x = (thumbRect.x + (thumbRect.width / 2)) -            g.getFontMetrics().stringWidth("" + slider.getValue()) / 2;        context.getStyle().getSynthGraphics(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 paintThumb(SynthContext context, Graphics g,            Rectangle thumbBounds)  {                SynthLookAndFeel.updateSubregion(context, g, thumbBounds);    }    public void paintTrack(SynthContext context, Graphics g,            Rectangle trackBounds) {        SynthLookAndFeel.updateSubregion(context, g, trackBounds);    }    public void paintLabels(Graphics g) {        Rectangle labelBounds = labelRect;        Dictionary dictionary = slider.getLabelTable();        if (dictionary != null) {            Enumeration keys = dictionary.keys();            int minValue = slider.getMinimum();            int maxValue = slider.getMaximum();            while (keys.hasMoreElements()) {                Integer key = (Integer)keys.nextElement();                int value = key.intValue();                if (value >= minValue && value <= maxValue) {                    Component label = (Component)dictionary.get(key);                    if (slider.getOrientation() == JSlider.HORIZONTAL) {                        g.translate( 0, labelBounds.y );                        paintHorizontalLabel( g, value, label );                        g.translate( 0, -labelBounds.y );                    }                    else {                        int offset = 0;                        if (!SynthLookAndFeel.isLeftToRight(slider)) {                            offset = labelBounds.width -                                label.getPreferredSize().width;                        }                        g.translate(labelBounds.x + offset, 0);                        paintVerticalLabel(g, value, label);                        g.translate(-labelBounds.x - offset, 0);                    }                }            }        }    }    /**     * Called for every label in the label table.  Used to draw the labels     * for horizontal sliders.  The graphics have been translated to     * labelRect.y already.     * @see JSlider#setLabelTable     */    protected void paintHorizontalLabel(Graphics g, int value,            Component label) {        int labelCenter = xPositionForValue(value);        int labelLeft = labelCenter - (label.getPreferredSize().width / 2);        g.translate(labelLeft, 0);        label.paint(g);        g.translate(-labelLeft, 0);    }    /**     * Called for every label in the label table.  Used to draw the labels     * for vertical sliders.  The graphics have been translated to     * labelRect.x already.     * @see JSlider#setLabelTable     */    protected void paintVerticalLabel(Graphics g, int value,            Component label) {        int labelCenter = yPositionForValue(value);        int labelTop = labelCenter - (label.getPreferredSize().height / 2);        g.translate(0, labelTop);        label.paint(g);        g.translate(0, -labelTop);    }    public void paintTicks(Graphics g)  {                Rectangle tickBounds = tickRect;        int i;        int maj, min, max;        int w = tickBounds.width;        int h = tickBounds.height;        int centerEffect, tickHeight;        g.setColor(slider.getBackground());        g.fillRect(tickBounds.x, tickBounds.y,            tickBounds.width, tickBounds.height);          g.setColor(Color.black);        maj = slider.getMajorTickSpacing();        min = slider.getMinorTickSpacing();        if (slider.getOrientation() == JSlider.HORIZONTAL) {           g.translate(0, tickBounds.y);            int value = slider.getMinimum();            int xPos = 0;            if (slider.getMinorTickSpacing() > 0) {                while (value <= slider.getMaximum()) {                    xPos = xPositionForValue(value);                    paintMinorTickForHorizSlider(g, tickBounds, xPos);                    value += slider.getMinorTickSpacing();                }            }            if (slider.getMajorTickSpacing() > 0) {                value = slider.getMinimum();                while (value <= slider.getMaximum()) {                    xPos = xPositionForValue(value);                    paintMajorTickForHorizSlider(g, tickBounds, xPos);                    value += slider.getMajorTickSpacing();                }            }            g.translate(0, -tickBounds.y);        }        else {           g.translate(tickBounds.x, 0);            int value = slider.getMinimum();            int yPos = 0;            if (slider.getMinorTickSpacing() > 0) {	        int offset = 0;	        if (!SynthLookAndFeel.isLeftToRight(slider)) {		    offset = tickBounds.width - tickBounds.width / 2;		    g.translate(offset, 0);		}                while (value <= slider.getMaximum()) {                    yPos = yPositionForValue(value);                    paintMinorTickForVertSlider(g, tickBounds, yPos);                    value += slider.getMinorTickSpacing();                }		if (!SynthLookAndFeel.isLeftToRight(slider)) {		    g.translate(-offset, 0);		}            }            if (slider.getMajorTickSpacing() > 0) {                value = slider.getMinimum();	        if (!SynthLookAndFeel.isLeftToRight(slider)) {		    g.translate(2, 0);		}                while (value <= slider.getMaximum()) {                    yPos = yPositionForValue(value);                    paintMajorTickForVertSlider(g, tickBounds, yPos);                    value += slider.getMajorTickSpacing();                }	        if (!SynthLookAndFeel.isLeftToRight(slider)) {		    g.translate(-2, 0);		}            }            g.translate(-tickBounds.x, 0);        }    }    protected void paintMinorTickForHorizSlider(Graphics g,            Rectangle tickBounds, int x) {        g.drawLine(x, 0, x, tickBounds.height / 2 - 1);    }    protected void paintMajorTickForHorizSlider(Graphics g,            Rectangle tickBounds, int x) {        g.drawLine(x, 0, x, tickBounds.height - 2);    }    protected void paintMinorTickForVertSlider(Graphics g,            Rectangle tickBounds, int y) {        g.drawLine(0, y, tickBounds.width / 2 - 1, y);    }    protected void paintMajorTickForVertSlider(Graphics g,            Rectangle tickBounds, int y) {        g.drawLine(0, y,  tickBounds.width - 2, y);    }    class PropertyChangeHandler implements PropertyChangeListener {        public void propertyChange(PropertyChangeEvent e) {            String propertyName = e.getPropertyName();            if (propertyName.equals("orientation") ||                    propertyName.equals("inverted") ||                    propertyName.equals("labelTable") ||                    propertyName.equals("majorTickSpacing") ||                    propertyName.equals("minorTickSpacing") ||                    propertyName.equals("paintTicks") ||                    propertyName.equals("paintTrack") ||

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?