basicsliderui.java

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

JAVA
2,219
字号
  /**   * 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.    calculateThumbLocation();    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);    Polygon high;    Polygon shadow;    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;	    double increment = (max == min) ? 0	                                    : majorSpace * (double) tickRect.width / (max	                                    - min);	    if (drawInverted())	      {		loc += tickRect.width;		increment *= -1;	      }	    for (int i = min; i <= max; i += majorSpace)	      {		paintMajorTickForHorizSlider(g, tickRect, (int) loc);		loc += increment;	      }	  }	else	  {	    double loc = tickRect.height + tickRect.y;	    double increment = (max == min) ? 0	                                    : -majorSpace * (double) tickRect.height / (max	                                    - min);	    if (drawInverted())	      {		loc = tickRect.y;		increment *= -1;	      }	    for (int i = min; i <= max; i += majorSpace)	      {		paintMajorTickForVertSlider(g, tickRect, (int) loc);		loc += increment;	      }	  }      }    if (minorSpace > 0)      {	if (slider.getOrientation() == JSlider.HORIZONTAL)	  {	    double loc = tickRect.x;	    double increment = (max == min) ? 0	                                    : minorSpace * (double) tickRect.width / (max	                                    - min);	    if (drawInverted())	      {		loc += tickRect.width;		increment *= -1;	      }	    for (int i = min; i <= max; i += minorSpace)	      {		paintMinorTickForHorizSlider(g, tickRect, (int) loc);		loc += increment;	      }	  }	else	  {	    double loc = tickRect.height + tickRect.y;	    double increment = (max == min) ? 0	                                    : -minorSpace * (double) tickRect.height / (max	                                    - min);	    if (drawInverted())	      {		loc = tickRect.y;		increment *= -1;	      }	    for (int i = min; i <= max; i += minorSpace)	      {		paintMinorTickForVertSlider(g, tickRect, (int) loc);		loc += increment;	      }	  }      }  }  /* 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.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.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.x + tickRect.width / 4;    Color saved = g.getColor();    g.setColor(Color.BLACK);    g.drawLine(x, y, x + tickRect.width / 4, y);    g.setColor(saved);  }  /**   * This method paints a major 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 paintMajorTickForVertSlider(Graphics g, Rectangle tickBounds,                                             int y)  {    int x = tickRect.x + tickRect.width / 4;    Color saved = g.getColor();    g.setColor(Color.BLACK);    g.drawLine(x, y, x + tickRect.width / 2, y);    g.setColor(saved);  }  /**   * This method paints all the labels from the slider's label table. This   * method must make sure that the label table is not null before painting   * the labels. Each entry in the label table is a (integer, component)   * pair. Every label is painted at the value of the integer.   *   * @param g The {@link Graphics} object to draw with.   */  public void paintLabels(Graphics g)  {    if (slider.getLabelTable() != null)      {	Dictionary table = slider.getLabelTable();	Integer tmpKey;	Object key;	Object element;	Component label;	if (slider.getOrientation() == JSlider.HORIZONTAL)	  {	    for (Enumeration list = table.keys(); list.hasMoreElements();)	      {		key = list.nextElement();		if (! (key instanceof Integer))		  continue;		tmpKey = (Integer) key;		element = table.get(tmpKey);		// We won't paint them if they're not		// JLabels so continue anyway		if (! (element instanceof JLabel))		  continue;		label = (Component) element;		paintHorizontalLabel(g, tmpKey.intValue(), label);	      }	  }	else	  {	    for (Enumeration list = table.keys(); list.hasMoreElements();)	      {		key = list.nextElement();		if (! (key instanceof Integer))		  continue;		tmpKey = (Integer) key;		element = table.get(tmpKey);		// We won't paint them if they're not		// JLabels so continue anyway		if (! (element instanceof JLabel))		  continue;		label = (Component) element;		paintVerticalLabel(g, tmpKey.intValue(), label);

⌨️ 快捷键说明

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