⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basicsliderui.java

📁 JAVA的一些源码 JAVA2 STANDARD EDITION DEVELOPMENT KIT 5.0
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                while ( value <= slider.getMaximum() ) {                    yPos = yPositionForValue( value );                    paintMajorTickForVertSlider( g, tickBounds, yPos );                    value += slider.getMajorTickSpacing();                }	        if(!BasicGraphicsUtils.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 );    }    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 (!BasicGraphicsUtils.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 paintThumb(Graphics g)  {                Rectangle knobBounds = thumbRect;        int w = knobBounds.width;        int h = knobBounds.height;              g.translate(knobBounds.x, knobBounds.y);        if ( slider.isEnabled() ) {            g.setColor(slider.getBackground());        }        else {            g.setColor(slider.getBackground().darker());        }	Boolean paintThumbArrowShape =	    (Boolean)slider.getClientProperty("Slider.paintThumbArrowShape");	if ((!slider.getPaintTicks() && paintThumbArrowShape == null) ||	    paintThumbArrowShape == Boolean.FALSE) {	    // "plain" version            g.fillRect(0, 0, w, h);            g.setColor(Color.black);            g.drawLine(0, h-1, w-1, h-1);                g.drawLine(w-1, 0, w-1, h-1);                g.setColor(highlightColor);            g.drawLine(0, 0, 0, h-2);            g.drawLine(1, 0, w-2, 0);            g.setColor(shadowColor);            g.drawLine(1, h-2, w-2, h-2);            g.drawLine(w-2, 1, w-2, h-3);        }        else if ( slider.getOrientation() == JSlider.HORIZONTAL ) {            int cw = w / 2;            g.fillRect(1, 1, w-3, h-1-cw);            Polygon p = new Polygon();            p.addPoint(1, h-cw);            p.addPoint(cw-1, h-1);            p.addPoint(w-2, h-1-cw);            g.fillPolygon(p);                   g.setColor(highlightColor);            g.drawLine(0, 0, w-2, 0);            g.drawLine(0, 1, 0, h-1-cw);            g.drawLine(0, h-cw, cw-1, h-1);             g.setColor(Color.black);            g.drawLine(w-1, 0, w-1, h-2-cw);                g.drawLine(w-1, h-1-cw, w-1-cw, h-1);                   g.setColor(shadowColor);            g.drawLine(w-2, 1, w-2, h-2-cw);                g.drawLine(w-2, h-1-cw, w-1-cw, h-2);               }        else {  // vertical            int cw = h / 2;	    if(BasicGraphicsUtils.isLeftToRight(slider)) {		  g.fillRect(1, 1, w-1-cw, h-3);	          Polygon p = new Polygon();                  p.addPoint(w-cw-1, 0);                  p.addPoint(w-1, cw);                  p.addPoint(w-1-cw, h-2);                  g.fillPolygon(p);                  g.setColor(highlightColor);	          g.drawLine(0, 0, 0, h - 2);                  // left	          g.drawLine(1, 0, w-1-cw, 0);                 // top	          g.drawLine(w-cw-1, 0, w-1, cw);              // top slant                  g.setColor(Color.black);	          g.drawLine(0, h-1, w-2-cw, h-1);             // bottom	          g.drawLine(w-1-cw, h-1, w-1, h-1-cw);        // bottom slant                  g.setColor(shadowColor);                  g.drawLine(1, h-2, w-2-cw,  h-2 );         // bottom                  g.drawLine(w-1-cw, h-2, w-2, h-cw-1 );     // bottom slant	    }	    else {		  g.fillRect(5, 1, w-1-cw, h-3);	          Polygon p = new Polygon();                  p.addPoint(cw, 0);                  p.addPoint(0, cw);                  p.addPoint(cw, h-2);                  g.fillPolygon(p);                  g.setColor(highlightColor);                  g.drawLine(cw-1, 0, w-2, 0);             // top                  g.drawLine(0, cw, cw, 0);                // top slant                  g.setColor(Color.black);                  g.drawLine(0, h-1-cw, cw, h-1 );         // bottom slant                  g.drawLine(cw, h-1, w-1, h-1);           // bottom                  g.setColor(shadowColor);                  g.drawLine(cw, h-2, w-2,  h-2 );         // bottom                  g.drawLine(w-1, 1, w-1,  h-2 );          // right	    }        }        g.translate(-knobBounds.x, -knobBounds.y);    }    // Used exclusively by setThumbLocation()    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 );    }    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 trackLength = trackRect.width;        double valueRange = (double)max - (double)min;        double pixelsPerValue = (double)trackLength / valueRange;        int trackLeft = trackRect.x;        int trackRight = trackRect.x + (trackRect.width - 1);        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 trackLength = trackRect.height;         double valueRange = (double)max - (double)min;        double pixelsPerValue = (double)trackLength / (double)valueRange;        int trackTop = trackRect.y;        int trackBottom = trackRect.y + (trackRect.height - 1);        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;	final int minValue = slider.getMinimum();	final int maxValue = slider.getMaximum();	final int trackLength = trackRect.height;	final int trackTop = trackRect.y;	final int trackBottom = trackRect.y + (trackRect.height - 1);		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;	final int minValue = slider.getMinimum();	final int maxValue = slider.getMaximum();	final int trackLength = trackRect.width;	final int trackLeft = trackRect.x; 	final int trackRight = trackRect.x + (trackRect.width - 1);		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;    }    private class Handler implements ChangeListener,            ComponentListener, FocusListener, PropertyChangeListener {        // Change Handler        public void stateChanged(ChangeEvent e) {	    if (!isDragging) {	        calculateThumbLocation();		slider.repaint();	    }        }        // Component Handler        public void componentHidden(ComponentEvent e) { }        public void componentMoved(ComponentEvent e) { }        public void componentResized(ComponentEvent e) {	    calculateGeometry();	    slider.repaint();        }        public void componentShown(ComponentEvent e) { }        // Focus Handler        public void focusGained(FocusEvent e) { slider.repaint(); }        public void focusLost(FocusEvent e) { slider.repaint(); }        // Property Change Handler        public void propertyChange(PropertyChangeEvent e) {            String propertyName = e.getPropertyName();            if (propertyName == "orientation" ||                    propertyName == "inverted" ||                    propertyName == "labelTable" ||                    propertyName == "majorTickSpacing" ||                    propertyName == "minorTickSpacing" ||                    propertyName == "paintTicks" ||                    propertyName == "paintTrack" ||                    propertyName == "paintLabels") {                calculateGeometry();                slider.repaint();            } else if (propertyName == "componentOrientation") {                calculateGeometry();                slider.repaint();                InputMap km = getInputMap(JComponent.WHEN_FOCUSED, slider);                SwingUtilities.replaceUIInputMap(slider,                    JComponent.WHEN_FOCUSED, km);            } else if (propertyName == "model") {                ((BoundedRangeModel)e.getOldValue()).removeChangeListener(                    changeListener);                ((BoundedRangeModel)e.getNewValue()).addChangeListener(                    changeListener);                calculateThumbLocation();                slider.repaint();            }        }    }    /////////////////////////////////////////////////////////////////////////    /// Model Listener Class    /////////////////////////////////////////////////////////////////////////            /**     * Data model listener.     *     * This class should be treated as a &quot;protected&quot; inner class.     * Instantiate it only within subclasses of <Foo>.     */    public class ChangeHandler implements ChangeListener {        // NOTE: This class exists only for backward compatability. All        // its functionality has been moved into Handler. If you need to add        // new functionality add it to the Handler, but make sure this              // class calls into the Handler.        public void stateChanged(ChangeEvent e) {            getHandler().stateChanged(e);        }    }    /////////////////////////////////////////////////////////////////////////    /// Track Listener Class    /////////////////////////////////////////////////////////////////////////            /**     * Track mouse movements.     *     * This class should be treated as a &quot;protected&quot; inner class.     * Instantiate it only within subclasses of <Foo>.     */    public class TrackListener extends MouseInputAdapter {        protected transient int offset;        protected transient int currentMouseX, currentMouseY;        public void mouseReleased(MouseEvent e) {            if (!slider.isEnabled()) {                return;            }            offset = 0;

⌨️ 快捷键说明

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