basicsliderui.java

来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 2,219 行 · 第 1/5 页

JAVA
2,219
字号
	      }	  }      }  }  /**   * This method paints the label on the horizontal slider at the value   * specified. The value is not a coordinate. It is a value within the range   * of the  slider. If the value is not within the range of the slider, this   * method will do nothing. This method should not paint outside the   * boundaries of the labelRect.   *   * @param g The {@link Graphics} object to draw with.   * @param value The value to paint at.   * @param label The label to paint.   */  protected void paintHorizontalLabel(Graphics g, int value, Component label)  {    // This relies on clipping working properly or we'll end up    // painting all over the place. If our preferred size is ignored, then    // the labels may not fit inside the slider's bounds. Rather than mucking     // with font sizes and possible icon sizes, we'll set the bounds for    // the label and let it get clipped.    Dimension dim = label.getPreferredSize();    int w = (int) dim.getWidth();    int h = (int) dim.getHeight();    int max = slider.getMaximum();    int min = slider.getMinimum();    if (value > max || value < min)      return;    //           value    //             |    //        ------------    //        |          |    //        |          |    //        |          |    //  The label must move w/2 to the right to fit directly under the value.    int xpos = xPositionForValue(value) - w / 2;    int ypos = labelRect.y;    // We want to center the label around the xPositionForValue    // So we use xpos - w / 2. However, if value is min and the label     // is large, we run the risk of going out of bounds. So we bring it back    // to 0 if it becomes negative.    if (xpos < 0)      xpos = 0;    // If the label + starting x position is greater than    // the x space in the label rectangle, we reset it to the largest    // amount possible in the rectangle. This means ugliness.    if (xpos + w > labelRect.x + labelRect.width)      w = labelRect.x + labelRect.width - xpos;    // If the label is too tall. We reset it to the height of the label    // rectangle.    if (h > labelRect.height)      h = labelRect.height;    label.setBounds(xpos, ypos, w, h);    javax.swing.SwingUtilities.paintComponent(g, label, null, label.getBounds());  }  /**   * This method paints the label on the vertical slider at the value   * specified. The value is not a coordinate. It is a value within the range   * of the  slider. If the value is not within the range of the slider, this   * method will do nothing. This method should not paint outside the   * boundaries of the labelRect.   *   * @param g The {@link Graphics} object to draw with.   * @param value The value to paint at.   * @param label The label to paint.   */  protected void paintVerticalLabel(Graphics g, int value, Component label)  {    Dimension dim = label.getPreferredSize();    int w = (int) dim.getWidth();    int h = (int) dim.getHeight();    int max = slider.getMaximum();    int min = slider.getMinimum();    if (value > max || value < min)      return;    int xpos = labelRect.x;    int ypos = yPositionForValue(value) - h / 2;    if (ypos < 0)      ypos = 0;    if (ypos + h > labelRect.y + labelRect.height)      h = labelRect.y + labelRect.height - ypos;    if (w > labelRect.width)      w = labelRect.width;    label.setBounds(xpos, ypos, w, h);    javax.swing.SwingUtilities.paintComponent(g, label, null, label.getBounds());  }  /**   * <p>   * This method paints a thumb. There are two types of thumb:   * </p>   * <pre>   *   Vertical         Horizontal   *    a---b            a-----b   *    |   |            |      \   *    e   c            |       c   *     \ /             |      /   *      d              e-----d   *  </pre>   *    * <p>   * In the case of vertical thumbs, we highlight the path b-a-e-d and shadow   * the path b-c-d. In the case of horizontal thumbs, we highlight the path   * c-b-a-e and shadow the path c-d-e. In both cases we fill the path   * a-b-c-d-e before shadows and highlights are drawn.   * </p>   *   * @param g The graphics object to paint with   */  public void paintThumb(Graphics g)  {    Color saved_color = g.getColor();    Polygon thumb = new Polygon();    Point a = new Point(thumbRect.x, thumbRect.y);    Point b = new Point(a);    Point c = new Point(a);    Point d = new Point(a);    Point e = new Point(a);    Polygon bright;    Polygon dark;    Polygon all;    // This will be in X-dimension if the slider is inverted and y if it isn't.	  	      int turnPoint;    if (slider.getOrientation() == JSlider.HORIZONTAL)      {	turnPoint = thumbRect.height * 3 / 4;	b.translate(thumbRect.width, 0);	c.translate(thumbRect.width, turnPoint);	d.translate(thumbRect.width / 2, thumbRect.height);	e.translate(0, turnPoint);	bright = new Polygon(new int[] { b.x, a.x, e.x, d.x },	                     new int[] { b.y, a.y, e.y, d.y }, 4);	dark = new Polygon(new int[] { b.x, c.x, d.x },	                   new int[] { b.y, c.y, d.y }, 3);	all = new Polygon(new int[] { a.x + 1, b.x, c.x, d.x, e.x + 1 },	                  new int[] { a.y + 1, b.y + 1, c.y, d.y + 1, e.y }, 5);      }    else      {	turnPoint = thumbRect.width * 3 / 4;	b.translate(turnPoint, 0);	c.translate(thumbRect.width, thumbRect.height / 2);	d.translate(turnPoint, thumbRect.height);	e.translate(0, thumbRect.height);	bright = new Polygon(new int[] { c.x, b.x, a.x, e.x },	                     new int[] { c.y, b.y, a.y, e.y }, 4);	dark = new Polygon(new int[] { c.x, d.x, e.x + 1 },	                   new int[] { c.y, d.y, e.y }, 3);	all = new Polygon(new int[] { a.x + 1, b.x, c.x - 1, d.x, e.x + 1 },	                  new int[] { a.y + 1, b.y + 1, c.y, d.y, e.y }, 5);      }    g.setColor(Color.WHITE);    g.drawPolyline(bright.xpoints, bright.ypoints, bright.npoints);    g.setColor(Color.BLACK);    g.drawPolyline(dark.xpoints, dark.ypoints, dark.npoints);    g.setColor(Color.GRAY);    g.fillPolygon(all);    g.setColor(saved_color);  }  /**   * This method sets the position of the thumbRect.   *   * @param x The new x position.   * @param y The new y position.   */  public void setThumbLocation(int x, int y)  {    thumbRect.x = x;    thumbRect.y = y;  }  /**   * This method is used to move the thumb one  block in the direction   * specified. If the slider  snaps to ticks, this method is responsible for   * snapping it to a tick after the thumb  has been moved.   *   * @param direction The direction to move in.   */  public void scrollByBlock(int direction)  {    // The direction is -1 for backwards and 1 for forwards.    int unit = direction * (slider.getMaximum() - slider.getMinimum()) / 10;    int moveTo = slider.getValue() + unit;    if (slider.getSnapToTicks())      moveTo = findClosestTick(moveTo);    slider.setValue(moveTo);  }  /**   * This method is used to move the thumb one unit in the direction   * specified. If the slider snaps to ticks, this method is responsible for   * snapping it to a tick after the thumb has been moved.   *   * @param direction The direction to move in.   */  public void scrollByUnit(int direction)  {    // The direction is -1 for backwards and 1 for forwards.    int moveTo = slider.getValue() + direction;    if (slider.getSnapToTicks())      moveTo = findClosestTick(moveTo);    slider.setValue(moveTo);  }  /**   * This method is called when there has been a click in the track and the   * thumb needs to be scrolled  on regular intervals. This method is only   * responsible  for starting the timer and not for stopping it.   *   * @param dir The direction to move in.   */  protected void scrollDueToClickInTrack(int dir)  {    scrollTimer.stop();    scrollListener.setDirection(dir);    scrollListener.setScrollByBlock(true);    scrollTimer.start();  }  /**   * This method returns the X coordinate for the value passed in.   *   * @param value The value to calculate an x coordinate for.   *   * @return The x coordinate for the value.   */  protected int xPositionForValue(int value)  {    int min = slider.getMinimum();    int max = slider.getMaximum();    int extent = slider.getExtent();    int len = trackRect.width;    int xPos = (max == min) ? 0 : (value - min) * len / (max - min);    if (! drawInverted())      xPos += trackRect.x;    else      {	xPos = trackRect.width - xPos;	xPos += trackRect.x;      }    return xPos;  }  /**   * This method returns the y coordinate for the value passed in.   *   * @param value The value to calculate a y coordinate for.   *   * @return The y coordinate for the value.   */  protected int yPositionForValue(int value)  {    int min = slider.getMinimum();    int max = slider.getMaximum();    int extent = slider.getExtent();    int len = trackRect.height;    int yPos = (max == min) ? 0 : (value - min) * len / (max - min);    if (! drawInverted())      {	yPos = trackRect.height - yPos;	yPos += trackRect.y;      }    else      yPos += trackRect.y;    return yPos;  }  /**   * This method returns the value in the slider's range given the y   * coordinate. If the value is out of range, it will  return the closest   * legal value.   *   * @param yPos The y coordinate to calculate a value for.   *   * @return The value for the y coordinate.   */  public int valueForYPosition(int yPos)  {    int min = slider.getMinimum();    int max = slider.getMaximum();    int len = trackRect.height;    int value;    // If the length is 0, you shouldn't be able to even see where the slider is.    // This really shouldn't ever happen, but just in case, we'll return the middle.    if (len == 0)      return ((max - min) / 2);    if (! drawInverted())      value = ((len - (yPos - trackRect.y)) * (max - min) / len + min);    else      value = ((yPos - trackRect.y) * (max - min) / len + min);    // If this isn't a legal value, then we'll have to move to one now.    if (value > max)      value = max;    else if (value < min)      value = min;    return value;  }  /**   * This method returns the value in the slider's range given the x   * coordinate. If the value is out of range, it will return the closest   * legal value.   *   * @param xPos The x coordinate to calculate a value for.   *   * @return The value for the x coordinate.   */  public int valueForXPosition(int xPos)  {    int min = slider.getMinimum();    int max = slider.getMaximum();    int len = trackRect.width;    int value;    // If the length is 0, you shouldn't be able to even see where the slider is.    // This really shouldn't ever happen, but just in case, we'll return the middle.    if (len == 0)      return ((max - min) / 2);    if (! drawInverted())      value = ((xPos - trackRect.x) * (max - min) / len + min);    else      value = ((len - (xPos - trackRect.x)) * (max - min) / len + min);    // If this isn't a legal value, then we'll have to move to one now.    if (value > max)      value = max;    else if (value < min)      value = min;    return value;  }  /**   * This method finds the closest value that has a tick associated with it.   *   * @param value The value to search from.   *   * @return The closest value that has a tick associated with it.   */  private int findClosestTick(int value)  {    int min = slider.getMinimum();    int max = slider.getMaximum();    int majorSpace = slider.getMajorTickSpacing();    int minorSpace = slider.getMinorTickSpacing();    // The default value to return is value + minor or    // value + major.     // Initializing at min - value leaves us with a default    // return value of min, which always has tick marks    // (if ticks are painted).    int minor = min - value;    int major = min - value;    // If there are no major tick marks or minor tick marks     // e.g. snap is set to true but no ticks are set, then    // we can just return the value.    if (majorSpace <= 0 && minorSpace <= 0)      return value;    // First check the major ticks.    if (majorSpace > 0)      {	int lowerBound = (value - min) / majorSpace;	int majLower = majorSpace * lowerBound + min;	int majHigher = majorSpace * (lowerBound + 1) + min;	if (majHigher <= max && majHigher - value <= value - majLower)	  major = majHigher - value;	else	  major = majLower - value;      }    if (minorSpace > 0)      {	int lowerBound = value / minorSpace;	int minLower = minorSpace * lowerBound;	int minHigher = minorSpace * (lowerBound + 1);	if (minHigher <= max && minHigher - value <= value - minLower)	  minor = minHigher - value;	else	  minor = minLower - value;      }    // Give preference to minor ticks    if (Math.abs(minor) > Math.abs(major))      return value + major;    else      return value + minor;  }}

⌨️ 快捷键说明

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