Java Code Examples for java.awt.FontMetrics.getMaxAscent()

The following are Jave code examples for showing how to use getMaxAscent() of the java.awt.FontMetrics class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: openjdk-jdk10   File: LightweightEventTest.java   View Source Code Vote up 6 votes
@Override
public Dimension getMinimumSize() {
    Dimension minSize = new Dimension();

    if (superIsButton) {
        minSize = super.getMinimumSize();
    } else {

        Graphics g = getGraphics();
        FontMetrics metrics = g.getFontMetrics();

        minSize.width = metrics.stringWidth(labelString) + 14;
        minSize.height = metrics.getMaxAscent()
                + metrics.getMaxDescent() + 9;

        g.dispose();
        g = null;
    }
    return minSize;
}
 
Example 2
Project: jermit   File: SwingScreen.java   View Source Code Vote up 5 votes
/**
 * Figure out my font dimensions.
 */
private void getFontDimensions() {
    Graphics gr = getGraphics();
    FontMetrics fm = gr.getFontMetrics();
    maxDescent = fm.getMaxDescent();
    Rectangle2D bounds = fm.getMaxCharBounds(gr);
    int leading = fm.getLeading();
    textWidth = (int)Math.round(bounds.getWidth());
    // textHeight = (int)Math.round(bounds.getHeight()) - maxDescent;

    // This produces the same number, but works better for ugly
    // monospace.
    textHeight = fm.getMaxAscent() + maxDescent - leading;

    if (gotTerminus == true) {
        textHeight++;
    }

    if (getFontAdjustments() == false) {
        // We were unable to programmatically determine textAdjustX
        // and textAdjustY, so try some guesses based on VM vendor.
        String runtime = System.getProperty("java.runtime.name");
        if ((runtime != null) && (runtime.contains("Java(TM)"))) {
            textAdjustY = -1;
            textAdjustX = 0;
        }
    }
}
 
Example 3
Project: incubator-netbeans   File: HtmlLabelUI.java   View Source Code Vote up 5 votes
private void paintIconAndTextCentered(Graphics g, HtmlRendererImpl r) {
    Insets ins = r.getInsets();
    Icon ic = r.getIcon();
    int w = r.getWidth() - (ins.left + ins.right);
    int txtX = ins.left;
    int txtY = 0;

    if ((ic != null) && (ic.getIconWidth() > 0) && (ic.getIconHeight() > 0)) {
        int iconx = (w > ic.getIconWidth()) ? ((w / 2) - (ic.getIconWidth() / 2)) : txtX;
        int icony = 0;
        ic.paintIcon(r, g, iconx, icony);
        txtY += (ic.getIconHeight() + r.getIconTextGap());
    }

    int txtW = r.getPreferredSize().width;
    txtX = (txtW < r.getWidth()) ? ((r.getWidth() / 2) - (txtW / 2)) : 0;

    int txtH = r.getHeight() - txtY;

    Font f = r.getFont();
    g.setFont(f);

    FontMetrics fm = g.getFontMetrics(f);
    txtY += fm.getMaxAscent();

    Color background = getBackgroundFor(r);
    Color foreground = ensureContrastingColor(getForegroundFor(r), background);

    if (r.isHtml()) {
        HtmlRenderer._renderHTML(
            r.getText(), 0, g, txtX, txtY, txtW, txtH, f, foreground, r.getRenderStyle(), true, background, r.isSelected()
        );
    } else {
        HtmlRenderer.renderString(
            r.getText(), g, txtX, txtY, txtW, txtH, r.getFont(), foreground, r.getRenderStyle(), true
        );
    }
}
 
Example 4
Project: freecol   File: ImageLibrary.java   View Source Code Vote up 5 votes
/**
 * Create a "chip" with the given text and colors.
 *
 * @param g Graphics2D for getting the FontMetrics.
 * @param text The text to display.
 * @param border The border {@code Color}.
 * @param background The background {@code Color}.
 * @param amount How much to fill the chip with the fill color
 * @param fill The fill {@code Color}.
 * @param foreground The foreground {@code Color}.
 * @param filled Whether the chip is filled or not
 * @return A chip.
 */
private BufferedImage createChip(Graphics2D g, String text,
                                 Color border, Color background,
                                 double amount, Color fill,
                                 Color foreground,
                                 Boolean filled) {
    Font font = FontLibrary.createFont(FontLibrary.FontType.SIMPLE,
        FontLibrary.FontSize.TINY, Font.BOLD, scaleFactor);
    FontMetrics fm = g.getFontMetrics(font);
    int padding = (int)(6 * scaleFactor);
    BufferedImage bi = new BufferedImage(fm.stringWidth(text) + padding,
        fm.getMaxAscent() + fm.getMaxDescent() + padding,
        BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bi.createGraphics();
    g2.setFont(font);
    int width = bi.getWidth();
    int height = bi.getHeight();
    g2.setColor(border);
    g2.fillRect(0, 0, width, height);
    g2.setColor(background);
    g2.fillRect(1, 1, width - 2, height - 2);
    if (filled.equals(false)) {
        if (amount > 0.0 && amount <= 1.0) {
            g2.setColor(fill);
            g2.fillRect(1, 1, width - 2, (int)((height - 2) * amount));
        }
    }
    g2.setColor(foreground);
    g2.drawString(text, padding/2, fm.getMaxAscent() + padding/2);
    g2.dispose();
    return bi;
}
 
Example 5
Project: openjdk-jdk10   File: LightweightEventTest.java   View Source Code Vote up 5 votes
@Override
public void paint(Graphics g) {

    super.paint(g);
    Rectangle bounds = getBounds();
    if (superIsButton) {
        return;
    }
    Dimension size = getSize();
    Color oldColor = g.getColor();

    // draw border
    g.setColor(getBackground());
    g.fill3DRect(0, 0, size.width, size.height, false);
    g.fill3DRect(3, 3, size.width - 6, size.height - 6, true);

    // draw text
    FontMetrics metrics = g.getFontMetrics();
    int centerX = size.width / 2;
    int centerY = size.height / 2;
    int textX = centerX - (metrics.stringWidth(labelString) / 2);
    int textY = centerY
            + ((metrics.getMaxAscent() + metrics.getMaxDescent()) / 2);
    g.setColor(getForeground());
    g.drawString(labelString, textX, textY);

    g.setColor(oldColor);
}
 
Example 6
Project: FreeCol   File: ImageLibrary.java   View Source Code Vote up 5 votes
/**
 * Create a "chip" with the given text and colors.
 *
 * @param g Graphics2D for getting the FontMetrics.
 * @param text The text to display.
 * @param border The border {@code Color}.
 * @param background The background {@code Color}.
 * @param amount How much to fill the chip with the fill color
 * @param fill The fill {@code Color}.
 * @param foreground The foreground {@code Color}.
 * @param filled Whether the chip is filled or not
 * @return A chip.
 */
private BufferedImage createChip(Graphics2D g, String text,
                                 Color border, Color background,
                                 double amount, Color fill,
                                 Color foreground,
                                 Boolean filled) {
    Font font = FontLibrary.createFont(FontLibrary.FontType.SIMPLE,
        FontLibrary.FontSize.TINY, Font.BOLD, scaleFactor);
    FontMetrics fm = g.getFontMetrics(font);
    int padding = (int)(6 * scaleFactor);
    BufferedImage bi = new BufferedImage(fm.stringWidth(text) + padding,
        fm.getMaxAscent() + fm.getMaxDescent() + padding,
        BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bi.createGraphics();
    g2.setFont(font);
    int width = bi.getWidth();
    int height = bi.getHeight();
    g2.setColor(border);
    g2.fillRect(0, 0, width, height);
    g2.setColor(background);
    g2.fillRect(1, 1, width - 2, height - 2);
    if (filled.equals(false)) {
        if (amount > 0.0 && amount <= 1.0) {
            g2.setColor(fill);
            g2.fillRect(1, 1, width - 2, (int)((height - 2) * amount));
        }
    }
    g2.setColor(foreground);
    g2.drawString(text, padding/2, fm.getMaxAscent() + padding/2);
    g2.dispose();
    return bi;
}
 
Example 7
Project: incubator-netbeans   File: MarginViewportUI.java   View Source Code Vote up 4 votes
private void calcStringSizes(Font f, Graphics g) {
    FontMetrics fm = g.getFontMetrics(f);
    stringWidth = fm.stringWidth(emptyString);
    stringHeight = fm.getHeight();
    ascent = fm.getMaxAscent();
}
 
Example 8
Project: incubator-netbeans   File: SlidingTabDisplayerButtonUI.java   View Source Code Vote up 4 votes
/** Paints the icon and text using the HTML mini renderer */
protected final void paintIconAndText (Graphics2D g, BasicSlidingTabDisplayerUI.IndexButton b, Object orientation) {
    FontMetrics fm = g.getFontMetrics(b.getFont());
    Insets ins = b.getInsets();
    
    boolean flip = orientation == TabDisplayer.ORIENTATION_EAST || 
        orientation == TabDisplayer.ORIENTATION_WEST;
    
    int txtX = flip ? ins.top : ins.left;
    
    int txtY = orientation == TabDisplayer.ORIENTATION_EAST ? ins.right :
        orientation == TabDisplayer.ORIENTATION_WEST ? ins.left : ins.top;
        
    int txtW = flip ? b.getHeight() - (ins.top + ins.bottom): 
        b.getWidth() - (ins.left + ins.right);
    
    int iconX = txtX;
    int iconY = txtY;
    
    int txtH = fm.getHeight();
    txtY += fm.getMaxAscent();
    
    Icon icon = b.getIcon();
    
    int iconH = icon.getIconHeight();
    int iconW = icon.getIconWidth();

    int workingHeight;
    if (flip) {
        workingHeight = b.getWidth() - (ins.left + ins.right);
    } else {
        workingHeight = b.getHeight() - (ins.top + ins.bottom);
    }
    txtY += (workingHeight / 2) - (txtH / 2);
    iconY += (workingHeight / 2) - (iconH / 2);
    
    if (icon != null && iconW > 0 && iconH > 0) {
        txtX += iconW + b.getIconTextGap();
        icon.paintIcon (b, g, iconX, iconY);
        txtW -= iconH + b.getIconTextGap();
    }
    
    HtmlRenderer.renderString(b.getText(), g, txtX, txtY, txtW, txtH, b.getFont(),
          b.getForeground(), HtmlRenderer.STYLE_TRUNCATE, true);
}
 
Example 9
Project: incubator-netbeans   File: HtmlLabelUI.java   View Source Code Vote up 4 votes
private Dimension calcPreferredSize(HtmlRendererImpl r) {
    Insets ins = r.getInsets();
    Dimension prefSize = new java.awt.Dimension(ins.left + ins.right, ins.top + ins.bottom);
    String text = r.getText();

    Graphics g = r.getGraphics();
    Icon icon = r.getIcon();

    if (text != null) {
        FontMetrics fm = g.getFontMetrics(r.getFont());
        prefSize.height += (fm.getMaxAscent() + fm.getMaxDescent());
    }

    if (icon != null) {
        if (r.isCentered()) {
            prefSize.height += (icon.getIconHeight() + r.getIconTextGap());
            prefSize.width += icon.getIconWidth();
        } else {
            prefSize.height = Math.max(icon.getIconHeight() + ins.top + ins.bottom, prefSize.height);
            prefSize.width += (icon.getIconWidth() + r.getIconTextGap());
        }
    }
    
    //Antialiasing affects the text metrics, so use it if needed when
    //calculating preferred size or the result here will be narrower
    //than the space actually needed
    ((Graphics2D) g).addRenderingHints(getHints());

    int textwidth = textWidth(text, g, r.getFont(), r.isHtml()) + 4;

    if (r.isCentered()) {
        prefSize.width = Math.max(prefSize.width, textwidth + ins.right + ins.left);
    } else {
        prefSize.width += (textwidth + r.getIndent());
    }

    if (FIXED_HEIGHT > 0) {
        prefSize.height = FIXED_HEIGHT;
    }

    return prefSize;
}
 
Example 10
Project: OpenJSharp   File: FontPanel.java   View Source Code Vote up 4 votes
private void calcFontMetrics( Graphics2D g2d, int w, int h ) {
    FontMetrics fm;
    Graphics2D g2 = (Graphics2D)g2d.create();

    /// ABP
    if ( g2Transform != NONE && textToUse != FILE_TEXT ) {
        g2.setFont( g2.getFont().deriveFont( getAffineTransform( g2Transform )) );
        fm = g2.getFontMetrics();
    }
    else {
        fm = g2.getFontMetrics();
    }

    maxAscent = fm.getMaxAscent();
    maxDescent = fm.getMaxDescent();
    if (maxAscent == 0) maxAscent = 10;
    if (maxDescent == 0) maxDescent = 5;
    if ( textToUse == RANGE_TEXT || textToUse == ALL_GLYPHS ) {
        /// Give slight extra room for each character
        maxAscent += 3;
        maxDescent += 3;
        gridWidth = fm.getMaxAdvance() + 6;
        gridHeight = maxAscent + maxDescent;
        if ( force16Cols )
          numCharAcross = 16;
        else
          numCharAcross = ( w - 10 ) / gridWidth;
        numCharDown = ( h - 10 ) / gridHeight;

        canvasInset_X = ( w - numCharAcross * gridWidth ) / 2;
        canvasInset_Y = ( h - numCharDown * gridHeight ) / 2;
        if ( numCharDown == 0 || numCharAcross == 0 )
          throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW );

        if ( !isPrinting )
          resetScrollbar( verticalBar.getValue() * numCharAcross );
    }
    else {
        maxDescent += fm.getLeading();
        canvasInset_X = 5;
        canvasInset_Y = 5;
        /// gridWidth and numCharAcross will not be used in this mode...
        gridHeight = maxAscent + maxDescent;
        numCharDown = ( h - canvasInset_Y * 2 ) / gridHeight;

        if ( numCharDown == 0 )
          throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW );
        /// If this is text loaded from file, prepares the LineBreak'ed
        /// text layout at this point
        if ( textToUse == FILE_TEXT ) {
            if ( !isPrinting )
              f2dt.fireChangeStatus( "LineBreaking Text... Please Wait", false );
            lineBreakTLs = new Vector();
            for ( int i = 0; i < fileText.length; i++ ) {
                AttributedString as =
                  new AttributedString( fileText[i], g2.getFont().getAttributes() );

                LineBreakMeasurer lbm =
                  new LineBreakMeasurer( as.getIterator(), g2.getFontRenderContext() );

                while ( lbm.getPosition() < fileText[i].length() )
                  lineBreakTLs.add( lbm.nextLayout( (float) w ));

            }
        }
        if ( !isPrinting )
          resetScrollbar( verticalBar.getValue() );
    }
}
 
Example 11
Project: jdk8u-jdk   File: FontPanel.java   View Source Code Vote up 4 votes
private void calcFontMetrics( Graphics2D g2d, int w, int h ) {
    FontMetrics fm;
    Graphics2D g2 = (Graphics2D)g2d.create();

    /// ABP
    if ( g2Transform != NONE && textToUse != FILE_TEXT ) {
        g2.setFont( g2.getFont().deriveFont( getAffineTransform( g2Transform )) );
        fm = g2.getFontMetrics();
    }
    else {
        fm = g2.getFontMetrics();
    }

    maxAscent = fm.getMaxAscent();
    maxDescent = fm.getMaxDescent();
    if (maxAscent == 0) maxAscent = 10;
    if (maxDescent == 0) maxDescent = 5;
    if ( textToUse == RANGE_TEXT || textToUse == ALL_GLYPHS ) {
        /// Give slight extra room for each character
        maxAscent += 3;
        maxDescent += 3;
        gridWidth = fm.getMaxAdvance() + 6;
        gridHeight = maxAscent + maxDescent;
        if ( force16Cols )
          numCharAcross = 16;
        else
          numCharAcross = ( w - 10 ) / gridWidth;
        numCharDown = ( h - 10 ) / gridHeight;

        canvasInset_X = ( w - numCharAcross * gridWidth ) / 2;
        canvasInset_Y = ( h - numCharDown * gridHeight ) / 2;
        if ( numCharDown == 0 || numCharAcross == 0 )
          throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW );

        if ( !isPrinting )
          resetScrollbar( verticalBar.getValue() * numCharAcross );
    }
    else {
        maxDescent += fm.getLeading();
        canvasInset_X = 5;
        canvasInset_Y = 5;
        /// gridWidth and numCharAcross will not be used in this mode...
        gridHeight = maxAscent + maxDescent;
        numCharDown = ( h - canvasInset_Y * 2 ) / gridHeight;

        if ( numCharDown == 0 )
          throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW );
        /// If this is text loaded from file, prepares the LineBreak'ed
        /// text layout at this point
        if ( textToUse == FILE_TEXT ) {
            if ( !isPrinting )
              f2dt.fireChangeStatus( "LineBreaking Text... Please Wait", false );
            lineBreakTLs = new Vector();
            for ( int i = 0; i < fileText.length; i++ ) {
                AttributedString as =
                  new AttributedString( fileText[i], g2.getFont().getAttributes() );

                LineBreakMeasurer lbm =
                  new LineBreakMeasurer( as.getIterator(), g2.getFontRenderContext() );

                while ( lbm.getPosition() < fileText[i].length() )
                  lineBreakTLs.add( lbm.nextLayout( (float) w ));

            }
        }
        if ( !isPrinting )
          resetScrollbar( verticalBar.getValue() );
    }
}
 
Example 12
Project: openjdk-jdk10   File: FontPanel.java   View Source Code Vote up 4 votes
private void calcFontMetrics( Graphics2D g2d, int w, int h ) {
    FontMetrics fm;
    Graphics2D g2 = (Graphics2D)g2d.create();

    /// ABP
    if ( g2Transform != NONE && textToUse != FILE_TEXT ) {
        g2.setFont( g2.getFont().deriveFont( getAffineTransform( g2Transform )) );
        fm = g2.getFontMetrics();
    }
    else {
        fm = g2.getFontMetrics();
    }

    maxAscent = fm.getMaxAscent();
    maxDescent = fm.getMaxDescent();
    if (maxAscent == 0) maxAscent = 10;
    if (maxDescent == 0) maxDescent = 5;
    if ( textToUse == RANGE_TEXT || textToUse == ALL_GLYPHS ) {
        /// Give slight extra room for each character
        maxAscent += 3;
        maxDescent += 3;
        gridWidth = fm.getMaxAdvance() + 6;
        gridHeight = maxAscent + maxDescent;
        if ( force16Cols )
          numCharAcross = 16;
        else
          numCharAcross = ( w - 10 ) / gridWidth;
        numCharDown = ( h - 10 ) / gridHeight;

        canvasInset_X = ( w - numCharAcross * gridWidth ) / 2;
        canvasInset_Y = ( h - numCharDown * gridHeight ) / 2;
        if ( numCharDown == 0 || numCharAcross == 0 )
          throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW );

        if ( !isPrinting )
          resetScrollbar( verticalBar.getValue() * numCharAcross );
    }
    else {
        maxDescent += fm.getLeading();
        canvasInset_X = 5;
        canvasInset_Y = 5;
        /// gridWidth and numCharAcross will not be used in this mode...
        gridHeight = maxAscent + maxDescent;
        numCharDown = ( h - canvasInset_Y * 2 ) / gridHeight;

        if ( numCharDown == 0 )
          throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW );
        /// If this is text loaded from file, prepares the LineBreak'ed
        /// text layout at this point
        if ( textToUse == FILE_TEXT ) {
            if ( !isPrinting )
              f2dt.fireChangeStatus( "LineBreaking Text... Please Wait", false );
            lineBreakTLs = new Vector();
            for ( int i = 0; i < fileText.length; i++ ) {
                AttributedString as =
                  new AttributedString( fileText[i], g2.getFont().getAttributes() );

                LineBreakMeasurer lbm =
                  new LineBreakMeasurer( as.getIterator(), g2.getFontRenderContext() );

                while ( lbm.getPosition() < fileText[i].length() )
                  lineBreakTLs.add( lbm.nextLayout( (float) w ));

            }
        }
        if ( !isPrinting )
          resetScrollbar( verticalBar.getValue() );
    }
}