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

The following are Jave code examples for showing how to use getMaxDescent() 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: 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 4
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 5
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 6
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 7
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 8
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 9
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() );
    }
}
 
Example 10
Project: rapidminer   File: JEditTextArea.java   View Source Code Vote up 2 votes
/**
 * Converts a line index to a y co-ordinate.
 * 
 * @param line
 *            The line
 */
public int lineToY(int line) {
	FontMetrics fm = painter.getFontMetrics();
	return (line - firstLine) * fm.getHeight() - (fm.getLeading() + fm.getMaxDescent());
}