metalscrollbarui.java

来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 581 行 · 第 1/2 页

JAVA
581
字号
   * @param g  the graphics device.   * @param c  the component.   * @param x  the x-coordinate for the track bounds.   * @param y  the y-coordinate for the track bounds.   * @param w  the width for the track bounds.   * @param h  the height for the track bounds.   */  private void paintTrackVertical(Graphics g, JComponent c,       int x, int y, int w, int h)  {    if (c.isEnabled())      {        g.setColor(MetalLookAndFeel.getControlDarkShadow());        g.drawLine(x, y, x, y + h - 1);        g.drawLine(x, y, x + w - 1, y);        g.drawLine(x, y + h - 1, x + w - 1, y + h - 1);                g.setColor(scrollBarShadowColor);        g.drawLine(x + 1, y + 1, x + w - 1, y + 1);        g.drawLine(x + 1, y + 1, x + 1, y + h - 2);                if (isFreeStanding)           {            g.setColor(MetalLookAndFeel.getControlDarkShadow());            g.drawLine(x + w - 2, y, x + w - 2, y + h - 1);            g.setColor(MetalLookAndFeel.getControlHighlight());            g.drawLine(x + w - 1, y, x + w - 1, y + h - 1);          }      }    else      {        g.setColor(MetalLookAndFeel.getControlDisabled());        if (isFreeStanding)          g.drawRect(x, y, w - 1, h - 1);        else          {            g.drawLine(x, y, x + w - 1, y);            g.drawLine(x, y, x, y + h - 1);            g.drawLine(x, y + h - 1, x + w - 1, y + h - 1);          }      }  }  /**   * Paints the slider button of the ScrollBar.   *   * @param g the Graphics context to use   * @param c the JComponent on which we paint   * @param thumbBounds the rectangle that is the slider button   */  protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds)  {    // a disabled scrollbar has no thumb in the metal look and feel    if (!c.isEnabled())      return;    if (scrollbar.getOrientation() == HORIZONTAL)      paintThumbHorizontal(g, c, thumbBounds);    else       paintThumbVertical(g, c, thumbBounds);    // Draw the pattern when the theme is not Ocean.    if (! (MetalLookAndFeel.getCurrentTheme() instanceof OceanTheme))      {        MetalUtils.fillMetalPattern(c, g, thumbBounds.x + 3, thumbBounds.y + 3,                                    thumbBounds.width - 6,                                    thumbBounds.height - 6,                                    thumbHighlightColor,                                    thumbLightShadowColor);      }  }  /**   * Paints the thumb for a horizontal scroll bar.   *    * @param g  the graphics device.   * @param c  the scroll bar component.   * @param thumbBounds  the thumb bounds.   */  private void paintThumbHorizontal(Graphics g, JComponent c,           Rectangle thumbBounds)   {    int x = thumbBounds.x;    int y = thumbBounds.y;    int w = thumbBounds.width;    int h = thumbBounds.height;        // First we fill the background.    MetalTheme theme = MetalLookAndFeel.getCurrentTheme();    if (theme instanceof OceanTheme        && UIManager.get("ScrollBar.gradient") != null)      {        MetalUtils.paintGradient(g, x + 2, y + 2, w - 4, h - 2,                                 SwingConstants.VERTICAL,                                 "ScrollBar.gradient");      }    else      {        g.setColor(thumbColor);        if (isFreeStanding)          g.fillRect(x, y, w, h - 1);        else          g.fillRect(x, y, w, h);      }    // then draw the dark box    g.setColor(thumbLightShadowColor);    if (isFreeStanding)      g.drawRect(x, y, w - 1, h - 2);    else      {        g.drawLine(x, y, x + w - 1, y);        g.drawLine(x, y, x, y + h - 1);        g.drawLine(x + w - 1, y, x + w - 1, y + h -1);      }        // then the highlight    g.setColor(thumbHighlightColor);    if (isFreeStanding)      {        g.drawLine(x + 1, y + 1, x + w - 3, y + 1);        g.drawLine(x + 1, y + 1, x + 1, y + h - 3);      }    else      {        g.drawLine(x + 1, y + 1, x + w - 3, y + 1);        g.drawLine(x + 1, y + 1, x + 1, y + h - 1);      }        // draw the shadow line    g.setColor(UIManager.getColor("ScrollBar.shadow"));    g.drawLine(x + w, y + 1, x + w, y + h - 1);    // For the OceanTheme, draw the 3 lines in the middle.    if (theme instanceof OceanTheme)      {        g.setColor(thumbLightShadowColor);        int middle = x + w / 2;        g.drawLine(middle - 2, y + 4, middle - 2, y + h - 5);        g.drawLine(middle, y + 4, middle, y + h - 5);        g.drawLine(middle + 2, y + 4, middle + 2, y + h - 5);        g.setColor(UIManager.getColor("ScrollBar.highlight"));        g.drawLine(middle - 1, y + 5, middle - 1, y + h - 4);        g.drawLine(middle + 1, y + 5, middle + 1, y + h - 4);        g.drawLine(middle + 3, y + 5, middle + 3, y + h - 4);      }  }    /**   * Paints the thumb for a vertical scroll bar.   *    * @param g  the graphics device.   * @param c  the scroll bar component.   * @param thumbBounds  the thumb bounds.   */  private void paintThumbVertical(Graphics g, JComponent c,           Rectangle thumbBounds)  {    int x = thumbBounds.x;    int y = thumbBounds.y;    int w = thumbBounds.width;    int h = thumbBounds.height;        // First we fill the background.    MetalTheme theme = MetalLookAndFeel.getCurrentTheme();    if (theme instanceof OceanTheme        && UIManager.get("ScrollBar.gradient") != null)      {        MetalUtils.paintGradient(g, x + 2, y + 2, w - 2, h - 4,                                 SwingConstants.HORIZONTAL,                                 "ScrollBar.gradient");      }    else      {        g.setColor(thumbColor);        if (isFreeStanding)          g.fillRect(x, y, w - 1, h);        else          g.fillRect(x, y, w, h);      }    // then draw the dark box    g.setColor(thumbLightShadowColor);    if (isFreeStanding)      g.drawRect(x, y, w - 2, h - 1);    else      {        g.drawLine(x, y, x + w - 1, y);        g.drawLine(x, y, x, y + h - 1);        g.drawLine(x, y + h - 1, x + w - 1, y + h - 1);      }        // then the highlight    g.setColor(thumbHighlightColor);    if (isFreeStanding)      {        g.drawLine(x + 1, y + 1, x + w - 3, y + 1);        g.drawLine(x + 1, y + 1, x + 1, y + h - 3);      }    else      {        g.drawLine(x + 1, y + 1, x + w - 1, y + 1);        g.drawLine(x + 1, y + 1, x + 1, y + h - 3);      }        // draw the shadow line    g.setColor(UIManager.getColor("ScrollBar.shadow"));    g.drawLine(x + 1, y + h, x + w - 2, y + h);    // For the OceanTheme, draw the 3 lines in the middle.    if (theme instanceof OceanTheme)      {        g.setColor(thumbLightShadowColor);        int middle = y + h / 2;        g.drawLine(x + 4, middle - 2, x + w - 5, middle - 2);        g.drawLine(x + 4, middle, x + w - 5, middle);        g.drawLine(x + 4, middle + 2, x + w - 5, middle + 2);        g.setColor(UIManager.getColor("ScrollBar.highlight"));        g.drawLine(x + 5, middle - 1, x + w - 4, middle - 1);        g.drawLine(x + 5, middle + 1, x + w - 4, middle + 1);        g.drawLine(x + 5, middle + 3, x + w - 4, middle + 3);      }  }    /**   * Returns the minimum thumb size.  For a free standing scroll bar the    * minimum size is <code>17 x 17</code> pixels, whereas for a non free    * standing scroll bar the minimum size is <code>15 x 15</code> pixels.   *   * @return The minimum thumb size.   */  protected Dimension getMinimumThumbSize()  {    Dimension retVal;    if (scrollbar != null)      {        if (isFreeStanding)          retVal = MIN_THUMB_SIZE_FREE_STANDING;        else          retVal = MIN_THUMB_SIZE;      }    else      retVal = new Dimension(0, 0);    return retVal;  }  /**   * Returns the <code>preferredSize</code> for the specified scroll bar.   * For a vertical scrollbar the height is the sum of the preferred heights   * of the buttons plus <code>30</code>. The width is fetched from the   * <code>UIManager</code> property <code>ScrollBar.width</code>.   *   * For horizontal scrollbars the width is the sum of the preferred widths   * of the buttons plus <code>30</code>. The height is fetched from the   * <code>UIManager</code> property <code>ScrollBar.height</code>.   *   * @param c the scrollbar for which to calculate the preferred size   *   * @return the <code>preferredSize</code> for the specified scroll bar   */  public Dimension getPreferredSize(JComponent c)  {    int height;    int width;    height = width = 0;    if (scrollbar.getOrientation() == SwingConstants.HORIZONTAL)      {        width += incrButton.getPreferredSize().getWidth();        width += decrButton.getPreferredSize().getWidth();        width += 30;        height = UIManager.getInt("ScrollBar.width");      }    else      {        height += incrButton.getPreferredSize().getHeight();        height += decrButton.getPreferredSize().getHeight();        height += 30;        width = UIManager.getInt("ScrollBar.width");      }    Insets insets = scrollbar.getInsets();    height += insets.top + insets.bottom;    width += insets.left + insets.right;    return new Dimension(width, height);  } }

⌨️ 快捷键说明

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