Java Code Examples for sun.swing.SwingUtilities2#stringWidth()

The following examples show how to use sun.swing.SwingUtilities2#stringWidth() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BasicToolTipUI.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
public Dimension getPreferredSize(JComponent c) {
    Font font = c.getFont();
    FontMetrics fm = c.getFontMetrics(font);
    Insets insets = c.getInsets();

    Dimension prefSize = new Dimension(insets.left+insets.right,
                                       insets.top+insets.bottom);
    String text = ((JToolTip)c).getTipText();

    if ((text == null) || text.equals("")) {
        text = "";
    }
    else {
        View v = (c != null) ? (View) c.getClientProperty("html") : null;
        if (v != null) {
            prefSize.width += (int) v.getPreferredSpan(View.X_AXIS) + 6;
            prefSize.height += (int) v.getPreferredSpan(View.Y_AXIS);
        } else {
            prefSize.width += SwingUtilities2.stringWidth(c,fm,text) + 6;
            prefSize.height += fm.getHeight();
        }
    }
    return prefSize;
}
 
Example 2
Source File: BasicProgressBarUI.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Designate the place where the progress string will be painted.
 * This implementation places it at the center of the progress
 * bar (in both x and y). Override this if you want to right,
 * left, top, or bottom align the progress string or if you need
 * to nudge it around for any reason.
 */
protected Point getStringPlacement(Graphics g, String progressString,
                                   int x,int y,int width,int height) {
    FontMetrics fontSizer = SwingUtilities2.getFontMetrics(progressBar, g,
                                        progressBar.getFont());
    int stringWidth = SwingUtilities2.stringWidth(progressBar, fontSizer,
                                                  progressString);

    if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
        return new Point(x + Math.round(width/2 - stringWidth/2),
                         y + ((height +
                             fontSizer.getAscent() -
                             fontSizer.getLeading() -
                             fontSizer.getDescent()) / 2));
    } else { // VERTICAL
        return new Point(x + ((width - fontSizer.getAscent() +
                fontSizer.getLeading() + fontSizer.getDescent()) / 2),
                y + Math.round(height/2 - stringWidth/2));
    }
}
 
Example 3
Source File: BasicTabbedPaneUI.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Calculates the tab width.
 * @param tabPlacement  the placement (left, right, bottom, top) of the tab
 * @param tabIndex      the index of the tab with respect to other tabs
 * @param metrics       the font metrics
 * @return the tab width
 */
protected int calculateTabWidth(int tabPlacement, int tabIndex, FontMetrics metrics) {
    Insets tabInsets = getTabInsets(tabPlacement, tabIndex);
    int width = tabInsets.left + tabInsets.right + 3;
    Component tabComponent = tabPane.getTabComponentAt(tabIndex);
    if (tabComponent != null) {
        width += tabComponent.getPreferredSize().width;
    } else {
        Icon icon = getIconForTab(tabIndex);
        if (icon != null) {
            width += icon.getIconWidth() + textIconGap;
        }
        View v = getTextViewForTab(tabIndex);
        if (v != null) {
            // html
            width += (int) v.getPreferredSpan(View.X_AXIS);
        } else {
            // plain text
            String title = tabPane.getTitleAt(tabIndex);
            width += SwingUtilities2.stringWidth(tabPane, metrics, title);
        }
    }
    return width;
}
 
Example 4
Source File: BasicToolTipUI.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public Dimension getPreferredSize(JComponent c) {
    Font font = c.getFont();
    FontMetrics fm = c.getFontMetrics(font);
    Insets insets = c.getInsets();

    Dimension prefSize = new Dimension(insets.left+insets.right,
                                       insets.top+insets.bottom);
    String text = ((JToolTip)c).getTipText();

    if ((text == null) || text.equals("")) {
        text = "";
    }
    else {
        View v = (c != null) ? (View) c.getClientProperty("html") : null;
        if (v != null) {
            prefSize.width += (int) v.getPreferredSpan(View.X_AXIS) + 6;
            prefSize.height += (int) v.getPreferredSpan(View.Y_AXIS);
        } else {
            prefSize.width += SwingUtilities2.stringWidth(c,fm,text) + 6;
            prefSize.height += fm.getHeight();
        }
    }
    return prefSize;
}
 
Example 5
Source File: BasicProgressBarUI.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Designate the place where the progress string will be painted.
 * This implementation places it at the center of the progress
 * bar (in both x and y). Override this if you want to right,
 * left, top, or bottom align the progress string or if you need
 * to nudge it around for any reason.
 */
protected Point getStringPlacement(Graphics g, String progressString,
                                   int x,int y,int width,int height) {
    FontMetrics fontSizer = SwingUtilities2.getFontMetrics(progressBar, g,
                                        progressBar.getFont());
    int stringWidth = SwingUtilities2.stringWidth(progressBar, fontSizer,
                                                  progressString);

    if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
        return new Point(x + Math.round(width/2 - stringWidth/2),
                         y + ((height +
                             fontSizer.getAscent() -
                             fontSizer.getLeading() -
                             fontSizer.getDescent()) / 2));
    } else { // VERTICAL
        return new Point(x + ((width - fontSizer.getAscent() +
                fontSizer.getLeading() + fontSizer.getDescent()) / 2),
                y + Math.round(height/2 - stringWidth/2));
    }
}
 
Example 6
Source File: BasicTabbedPaneUI.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
protected int calculateTabWidth(int tabPlacement, int tabIndex, FontMetrics metrics) {
    Insets tabInsets = getTabInsets(tabPlacement, tabIndex);
    int width = tabInsets.left + tabInsets.right + 3;
    Component tabComponent = tabPane.getTabComponentAt(tabIndex);
    if (tabComponent != null) {
        width += tabComponent.getPreferredSize().width;
    } else {
        Icon icon = getIconForTab(tabIndex);
        if (icon != null) {
            width += icon.getIconWidth() + textIconGap;
        }
        View v = getTextViewForTab(tabIndex);
        if (v != null) {
            // html
            width += (int) v.getPreferredSpan(View.X_AXIS);
        } else {
            // plain text
            String title = tabPane.getTitleAt(tabIndex);
            width += SwingUtilities2.stringWidth(tabPane, metrics, title);
        }
    }
    return width;
}
 
Example 7
Source File: BasicToolTipUI.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public Dimension getPreferredSize(JComponent c) {
    Font font = c.getFont();
    FontMetrics fm = c.getFontMetrics(font);
    Insets insets = c.getInsets();

    Dimension prefSize = new Dimension(insets.left+insets.right,
                                       insets.top+insets.bottom);
    String text = ((JToolTip)c).getTipText();

    if ((text == null) || text.equals("")) {
        text = "";
    }
    else {
        View v = (c != null) ? (View) c.getClientProperty("html") : null;
        if (v != null) {
            prefSize.width += (int) v.getPreferredSpan(View.X_AXIS) + 6;
            prefSize.height += (int) v.getPreferredSpan(View.Y_AXIS);
        } else {
            prefSize.width += SwingUtilities2.stringWidth(c,fm,text) + 6;
            prefSize.height += fm.getHeight();
        }
    }
    return prefSize;
}
 
Example 8
Source File: DarkPreviewPanel.java    From darklaf with MIT License 5 votes vote down vote up
private int paintText(final Graphics g, final int offsetX) {
    GraphicsContext config = GraphicsUtil.setupAntialiasing(g);
    g.setFont(getFont());
    JComponent host = getColorChooser();
    if (host == null) {
        host = this;
    }
    FontMetrics fm = SwingUtilities2.getFontMetrics(host, g);

    int ascent = fm.getAscent();
    int height = fm.getHeight();
    int width = SwingUtilities2.stringWidth(host, fm, getSampleText());
    int textXOffset = offsetX + TEXT_GAP;

    Color color = getForeground();
    g.setColor(color);
    SwingUtilities2.drawString(host, g, getSampleText(), textXOffset + (TEXT_GAP / 2), ascent);

    g.fillRect(textXOffset, (height) + TEXT_GAP, width + (TEXT_GAP), height + 2);

    g.setColor(Color.black);
    SwingUtilities2.drawString(host, g, getSampleText(), textXOffset + (TEXT_GAP / 2),
                               height + ascent + TEXT_GAP + 2);

    g.setColor(Color.white);

    g.fillRect(textXOffset, (height + TEXT_GAP) * 2, width + (TEXT_GAP), height + 2);

    g.setColor(color);
    SwingUtilities2.drawString(host, g, getSampleText(), textXOffset + (TEXT_GAP / 2),
                               ((height + TEXT_GAP) * 2) + ascent + 2);

    config.restore();
    return width + TEXT_GAP * 3;
}
 
Example 9
Source File: DefaultPreviewPanel.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
public Dimension getPreferredSize() {
    JComponent host = getColorChooser();
    if (host == null) {
        host = this;
    }
    FontMetrics fm = host.getFontMetrics(getFont());

    int ascent = fm.getAscent();
    int height = fm.getHeight();
    int width = SwingUtilities2.stringWidth(host, fm, getSampleText());

    int y = height*3 + textGap*3;
    int x = squareSize * 3 + squareGap*2 + swatchWidth + width + textGap*3;
    return new Dimension( x,y );
}
 
Example 10
Source File: StyleSheet.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * Draws the letter or number for an ordered list.
 *
 * @param g     the graphics context
 * @param letter type of ordered list to draw
 * @param ax    x coordinate to place the bullet
 * @param ay    y coordinate to place the bullet
 * @param aw    width of the container the bullet is placed in
 * @param ah    height of the container the bullet is placed in
 * @param index position of the list item in the list
 */
void drawLetter(Graphics g, char letter, int ax, int ay, int aw,
                int ah, float align, int index) {
    String str = formatItemNum(index, letter);
    str = isLeftToRight ? str + "." : "." + str;
    FontMetrics fm = SwingUtilities2.getFontMetrics(null, g);
    int stringwidth = SwingUtilities2.stringWidth(null, fm, str);
    int gap = isLeftToRight ? - (stringwidth + bulletgap) :
                                (aw + bulletgap);
    int x = ax + gap;
    int y = Math.max(ay + fm.getAscent(), ay + (int)(ah * align));
    SwingUtilities2.drawString(null, g, str, x, y);
}
 
Example 11
Source File: MotifDesktopIconUI.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public Dimension getPreferredSize() {
    String title = frame.getTitle();
    FontMetrics fm = frame.getFontMetrics(defaultTitleFont);
    int w = 4;
    if (title != null) {
        w += SwingUtilities2.stringWidth(frame, fm, title);
    }
    return new Dimension(w, LABEL_HEIGHT + LABEL_DIVIDER);
}
 
Example 12
Source File: StyleSheet.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * Draws the letter or number for an ordered list.
 *
 * @param g     the graphics context
 * @param letter type of ordered list to draw
 * @param ax    x coordinate to place the bullet
 * @param ay    y coordinate to place the bullet
 * @param aw    width of the container the bullet is placed in
 * @param ah    height of the container the bullet is placed in
 * @param index position of the list item in the list
 */
void drawLetter(Graphics g, char letter, int ax, int ay, int aw,
                int ah, float align, int index) {
    String str = formatItemNum(index, letter);
    str = isLeftToRight ? str + "." : "." + str;
    FontMetrics fm = SwingUtilities2.getFontMetrics(null, g);
    int stringwidth = SwingUtilities2.stringWidth(null, fm, str);
    int gap = isLeftToRight ? - (stringwidth + bulletgap) :
                                (aw + bulletgap);
    int x = ax + gap;
    int y = Math.max(ay + fm.getAscent(), ay + (int)(ah * align));
    SwingUtilities2.drawString(null, g, str, x, y);
}
 
Example 13
Source File: MotifPopupMenuUI.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public Dimension getPreferredSize(JComponent c) {
    LayoutManager layout = c.getLayout();
    Dimension d = layout.preferredLayoutSize(c);
    String title = ((JPopupMenu)c).getLabel();
    if (titleFont == null) {
        UIDefaults table = UIManager.getLookAndFeelDefaults();
        titleFont = table.getFont("PopupMenu.font");
    }
    FontMetrics fm = c.getFontMetrics(titleFont);
    int         stringWidth = 0;

    if (title!=null) {
        stringWidth += SwingUtilities2.stringWidth(c, fm, title);
    }

    if (d.width < stringWidth) {
        d.width = stringWidth + 8;
        Insets i = c.getInsets();
        if (i!=null) {
            d.width += i.left + i.right;
        }
        if (border != null) {
            i = border.getBorderInsets(c);
            d.width += i.left + i.right;
        }

        return d;
    }
    return null;
}
 
Example 14
Source File: StyleSheet.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Draws the letter or number for an ordered list.
 *
 * @param g     the graphics context
 * @param letter type of ordered list to draw
 * @param ax    x coordinate to place the bullet
 * @param ay    y coordinate to place the bullet
 * @param aw    width of the container the bullet is placed in
 * @param ah    height of the container the bullet is placed in
 * @param index position of the list item in the list
 */
void drawLetter(Graphics g, char letter, int ax, int ay, int aw,
                int ah, float align, int index) {
    String str = formatItemNum(index, letter);
    str = isLeftToRight ? str + "." : "." + str;
    FontMetrics fm = SwingUtilities2.getFontMetrics(null, g);
    int stringwidth = SwingUtilities2.stringWidth(null, fm, str);
    int gap = isLeftToRight ? - (stringwidth + bulletgap) :
                                (aw + bulletgap);
    int x = ax + gap;
    int y = Math.max(ay + fm.getAscent(), ay + (int)(ah * align));
    SwingUtilities2.drawString(null, g, str, x, y);
}
 
Example 15
Source File: MotifPopupMenuUI.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public Dimension getPreferredSize(JComponent c) {
    LayoutManager layout = c.getLayout();
    Dimension d = layout.preferredLayoutSize(c);
    String title = ((JPopupMenu)c).getLabel();
    if (titleFont == null) {
        UIDefaults table = UIManager.getLookAndFeelDefaults();
        titleFont = table.getFont("PopupMenu.font");
    }
    FontMetrics fm = c.getFontMetrics(titleFont);
    int         stringWidth = 0;

    if (title!=null) {
        stringWidth += SwingUtilities2.stringWidth(c, fm, title);
    }

    if (d.width < stringWidth) {
        d.width = stringWidth + 8;
        Insets i = c.getInsets();
        if (i!=null) {
            d.width += i.left + i.right;
        }
        if (border != null) {
            i = border.getBorderInsets(c);
            d.width += i.left + i.right;
        }

        return d;
    }
    return null;
}
 
Example 16
Source File: MetalTitlePane.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Renders the TitlePane.
 */
public void paintComponent(Graphics g)  {
    // As state isn't bound, we need a convenience place to check
    // if it has changed. Changing the state typically changes the
    if (getFrame() != null) {
        setState(getFrame().getExtendedState());
    }
    JRootPane rootPane = getRootPane();
    Window window = getWindow();
    boolean leftToRight = (window == null) ?
                           rootPane.getComponentOrientation().isLeftToRight() :
                           window.getComponentOrientation().isLeftToRight();
    boolean isSelected = (window == null) ? true : window.isActive();
    int width = getWidth();
    int height = getHeight();

    Color background;
    Color foreground;
    Color darkShadow;

    MetalBumps bumps;

    if (isSelected) {
        background = activeBackground;
        foreground = activeForeground;
        darkShadow = activeShadow;
        bumps = activeBumps;
    } else {
        background = inactiveBackground;
        foreground = inactiveForeground;
        darkShadow = inactiveShadow;
        bumps = inactiveBumps;
    }

    g.setColor(background);
    g.fillRect(0, 0, width, height);

    g.setColor( darkShadow );
    g.drawLine ( 0, height - 1, width, height -1);
    g.drawLine ( 0, 0, 0 ,0);
    g.drawLine ( width - 1, 0 , width -1, 0);

    int xOffset = leftToRight ? 5 : width - 5;

    if (getWindowDecorationStyle() == JRootPane.FRAME) {
        xOffset += leftToRight ? IMAGE_WIDTH + 5 : - IMAGE_WIDTH - 5;
    }

    String theTitle = getTitle();
    if (theTitle != null) {
        FontMetrics fm = SwingUtilities2.getFontMetrics(rootPane, g);

        g.setColor(foreground);

        int yOffset = ( (height - fm.getHeight() ) / 2 ) + fm.getAscent();

        Rectangle rect = new Rectangle(0, 0, 0, 0);
        if (iconifyButton != null && iconifyButton.getParent() != null) {
            rect = iconifyButton.getBounds();
        }
        int titleW;

        if( leftToRight ) {
            if (rect.x == 0) {
                rect.x = window.getWidth() - window.getInsets().right-2;
            }
            titleW = rect.x - xOffset - 4;
            theTitle = SwingUtilities2.clipStringIfNecessary(
                            rootPane, fm, theTitle, titleW);
        } else {
            titleW = xOffset - rect.x - rect.width - 4;
            theTitle = SwingUtilities2.clipStringIfNecessary(
                            rootPane, fm, theTitle, titleW);
            xOffset -= SwingUtilities2.stringWidth(rootPane, fm,
                                                   theTitle);
        }
        int titleLength = SwingUtilities2.stringWidth(rootPane, fm,
                                                      theTitle);
        SwingUtilities2.drawString(rootPane, g, theTitle, xOffset,
                                   yOffset );
        xOffset += leftToRight ? titleLength + 5  : -5;
    }

    int bumpXOffset;
    int bumpLength;
    if( leftToRight ) {
        bumpLength = width - buttonsWidth - xOffset - 5;
        bumpXOffset = xOffset;
    } else {
        bumpLength = xOffset - buttonsWidth - 5;
        bumpXOffset = buttonsWidth + 5;
    }
    int bumpYOffset = 3;
    int bumpHeight = getHeight() - (2 * bumpYOffset);
    bumps.setBumpArea( bumpLength, bumpHeight );
    bumps.paintIcon(this, g, bumpXOffset, bumpYOffset);
}
 
Example 17
Source File: BasicInternalFrameTitlePane.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public Dimension minimumLayoutSize(Container c) {
    // Calculate width.
    int width = 22;

    if (frame.isClosable()) {
        width += 19;
    }
    if (frame.isMaximizable()) {
        width += 19;
    }
    if (frame.isIconifiable()) {
        width += 19;
    }

    FontMetrics fm = frame.getFontMetrics(getFont());
    String frameTitle = frame.getTitle();
    int title_w = frameTitle != null ? SwingUtilities2.stringWidth(
                       frame, fm, frameTitle) : 0;
    int title_length = frameTitle != null ? frameTitle.length() : 0;

    // Leave room for three characters in the title.
    if (title_length > 3) {
        int subtitle_w = SwingUtilities2.stringWidth(
            frame, fm, frameTitle.substring(0, 3) + "...");
        width += (title_w < subtitle_w) ? title_w : subtitle_w;
    } else {
        width += title_w;
    }

    // Calculate height.
    Icon icon = frame.getFrameIcon();
    int fontHeight = fm.getHeight();
    fontHeight += 2;
    int iconHeight = 0;
    if (icon != null) {
        // SystemMenuBar forces the icon to be 16x16 or less.
        iconHeight = Math.min(icon.getIconHeight(), 16);
    }
    iconHeight += 2;

    int height = Math.max( fontHeight, iconHeight );

    Dimension dim = new Dimension(width, height);

    // Take into account the border insets if any.
    if (getBorder() != null) {
        Insets insets = getBorder().getBorderInsets(c);
        dim.height += insets.top + insets.bottom;
        dim.width += insets.left + insets.right;
    }
    return dim;
}
 
Example 18
Source File: MetalTitlePane.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Renders the TitlePane.
 */
public void paintComponent(Graphics g)  {
    // As state isn't bound, we need a convenience place to check
    // if it has changed. Changing the state typically changes the
    if (getFrame() != null) {
        setState(getFrame().getExtendedState());
    }
    JRootPane rootPane = getRootPane();
    Window window = getWindow();
    boolean leftToRight = (window == null) ?
                           rootPane.getComponentOrientation().isLeftToRight() :
                           window.getComponentOrientation().isLeftToRight();
    boolean isSelected = (window == null) ? true : window.isActive();
    int width = getWidth();
    int height = getHeight();

    Color background;
    Color foreground;
    Color darkShadow;

    MetalBumps bumps;

    if (isSelected) {
        background = activeBackground;
        foreground = activeForeground;
        darkShadow = activeShadow;
        bumps = activeBumps;
    } else {
        background = inactiveBackground;
        foreground = inactiveForeground;
        darkShadow = inactiveShadow;
        bumps = inactiveBumps;
    }

    g.setColor(background);
    g.fillRect(0, 0, width, height);

    g.setColor( darkShadow );
    g.drawLine ( 0, height - 1, width, height -1);
    g.drawLine ( 0, 0, 0 ,0);
    g.drawLine ( width - 1, 0 , width -1, 0);

    int xOffset = leftToRight ? 5 : width - 5;

    if (getWindowDecorationStyle() == JRootPane.FRAME) {
        xOffset += leftToRight ? IMAGE_WIDTH + 5 : - IMAGE_WIDTH - 5;
    }

    String theTitle = getTitle();
    if (theTitle != null) {
        FontMetrics fm = SwingUtilities2.getFontMetrics(rootPane, g);

        g.setColor(foreground);

        int yOffset = ( (height - fm.getHeight() ) / 2 ) + fm.getAscent();

        Rectangle rect = new Rectangle(0, 0, 0, 0);
        if (iconifyButton != null && iconifyButton.getParent() != null) {
            rect = iconifyButton.getBounds();
        }
        int titleW;

        if( leftToRight ) {
            if (rect.x == 0) {
                rect.x = window.getWidth() - window.getInsets().right-2;
            }
            titleW = rect.x - xOffset - 4;
            theTitle = SwingUtilities2.clipStringIfNecessary(
                            rootPane, fm, theTitle, titleW);
        } else {
            titleW = xOffset - rect.x - rect.width - 4;
            theTitle = SwingUtilities2.clipStringIfNecessary(
                            rootPane, fm, theTitle, titleW);
            xOffset -= SwingUtilities2.stringWidth(rootPane, fm,
                                                   theTitle);
        }
        int titleLength = SwingUtilities2.stringWidth(rootPane, fm,
                                                      theTitle);
        SwingUtilities2.drawString(rootPane, g, theTitle, xOffset,
                                   yOffset );
        xOffset += leftToRight ? titleLength + 5  : -5;
    }

    int bumpXOffset;
    int bumpLength;
    if( leftToRight ) {
        bumpLength = width - buttonsWidth - xOffset - 5;
        bumpXOffset = xOffset;
    } else {
        bumpLength = xOffset - buttonsWidth - 5;
        bumpXOffset = buttonsWidth + 5;
    }
    int bumpYOffset = 3;
    int bumpHeight = getHeight() - (2 * bumpYOffset);
    bumps.setBumpArea( bumpLength, bumpHeight );
    bumps.paintIcon(this, g, bumpXOffset, bumpYOffset);
}
 
Example 19
Source File: MotifBorders.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Paints the border for the specified component with the
 * specified position and size.
 * @param c the component for which this border is being painted
 * @param g the paint graphics
 * @param x the x position of the painted border
 * @param y the y position of the painted border
 * @param width the width of the painted border
 * @param height the height of the painted border
 */
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
    if (!(c instanceof JPopupMenu)) {
        return;
    }

    Font origFont = g.getFont();
    Color origColor = g.getColor();
    JPopupMenu popup = (JPopupMenu)c;

    String title = popup.getLabel();
    if (title == null) {
        return;
    }

    g.setFont(font);

    FontMetrics fm = SwingUtilities2.getFontMetrics(popup, g, font);
    int         fontHeight = fm.getHeight();
    int         descent = fm.getDescent();
    int         ascent = fm.getAscent();
    Point       textLoc = new Point();
    int         stringWidth = SwingUtilities2.stringWidth(popup, fm,
                                                          title);

    textLoc.y = y + ascent + TEXT_SPACING;
    textLoc.x = x + ((width - stringWidth) / 2);

    g.setColor(background);
    g.fillRect(textLoc.x - TEXT_SPACING, textLoc.y - (fontHeight-descent),
               stringWidth + (2 * TEXT_SPACING), fontHeight - descent);
    g.setColor(foreground);
    SwingUtilities2.drawString(popup, g, title, textLoc.x, textLoc.y);

    MotifGraphicsUtils.drawGroove(g, x, textLoc.y + TEXT_SPACING,
                                  width, GROOVE_HEIGHT,
                                  shadowColor, highlightColor);

    g.setFont(origFont);
    g.setColor(origColor);
}
 
Example 20
Source File: SwingUtilities.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Compute the width of the string using a font with the specified
 * "metrics" (sizes).
 *
 * @param fm   a FontMetrics object to compute with
 * @param str  the String to compute
 * @return an int containing the string width
 */
public static int computeStringWidth(FontMetrics fm,String str) {
    // You can't assume that a string's width is the sum of its
    // characters' widths in Java2D -- it may be smaller due to
    // kerning, etc.
    return SwingUtilities2.stringWidth(null, fm, str);
}