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

The following are Jave code examples for showing how to use getDescent() 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: trashjam2017   File: UnicodeFont.java   View Source Code Vote up 7 votes
/**
 * Initialise the font to be used based on configuration
 * 
 * @param baseFont The AWT font to render
 * @param size The point size of the font to generated
 * @param bold True if the font should be rendered in bold typeface
 * @param italic True if the font should be rendered in bold typeface
 */
private void initializeFont(Font baseFont, int size, boolean bold, boolean italic) {
	Map attributes = baseFont.getAttributes();
	attributes.put(TextAttribute.SIZE, new Float(size));
	attributes.put(TextAttribute.WEIGHT, bold ? TextAttribute.WEIGHT_BOLD : TextAttribute.WEIGHT_REGULAR);
	attributes.put(TextAttribute.POSTURE, italic ? TextAttribute.POSTURE_OBLIQUE : TextAttribute.POSTURE_REGULAR);
	try {
		attributes.put(TextAttribute.class.getDeclaredField("KERNING").get(null), TextAttribute.class.getDeclaredField(
			"KERNING_ON").get(null));
	} catch (Exception ignored) {
	}
	font = baseFont.deriveFont(attributes);

	FontMetrics metrics = GlyphPage.getScratchGraphics().getFontMetrics(font);
	ascent = metrics.getAscent();
	descent = metrics.getDescent();
	leading = metrics.getLeading();
	
	// Determine width of space glyph (getGlyphPixelBounds gives a width of zero).
	char[] chars = " ".toCharArray();
	GlyphVector vector = font.layoutGlyphVector(GlyphPage.renderContext, chars, 0, chars.length, Font.LAYOUT_LEFT_TO_RIGHT);
	spaceWidth = vector.getGlyphLogicalBounds(0).getBounds().width;
}
 
Example 2
Project: incubator-netbeans   File: ListCompletionView.java   View Source Code Vote up 6 votes
private void renderHtml(Fix f, Graphics g, Font defaultFont, Color defaultColor,
int width, int height, boolean selected) {
    if (icon != null) {
        // The image of the ImageIcon should already be loaded
        // so no ImageObserver should be necessary
        g.drawImage(ImageUtilities.icon2Image(icon), BEFORE_ICON_GAP, (height - icon.getIconHeight()) /2, this);
    }
    int iconWidth = BEFORE_ICON_GAP + icon.getIconWidth() + AFTER_ICON_GAP;
    int textEnd = width - AFTER_ICON_GAP - subMenuIcon.getIconWidth() - AFTER_TEXT_GAP;
    FontMetrics fm = g.getFontMetrics(defaultFont);
    int textY = (height - fm.getHeight())/2 + fm.getHeight() - fm.getDescent();

    // Render left text
    if (textEnd > iconWidth) { // any space for left text?
        HtmlRenderer.renderHTML(f.getText(), g, iconWidth, textY, textEnd, textY,
            defaultFont, defaultColor, HtmlRenderer.STYLE_TRUNCATE, true);//, selected);
    }

    if (HintsControllerImpl.getSubfixes(f).iterator().hasNext()) {
        paintArrowIcon(g, textEnd + AFTER_TEXT_GAP, (height - subMenuIcon.getIconHeight()) /2);
    }
}
 
Example 3
Project: OpenJSharp   File: CompositionArea.java   View Source Code Vote up 6 votes
private Rectangle getCaretRectangle(TextHitInfo caret) {
    int caretLocation = 0;
    TextLayout layout = composedTextLayout;
    if (layout != null) {
        caretLocation = Math.round(layout.getCaretInfo(caret)[0]);
    }
    Graphics g = getGraphics();
    FontMetrics metrics = null;
    try {
        metrics = g.getFontMetrics();
    } finally {
        g.dispose();
    }
    return new Rectangle(TEXT_ORIGIN_X + caretLocation,
                         TEXT_ORIGIN_Y - metrics.getAscent(),
                         0, metrics.getAscent() + metrics.getDescent());
}
 
Example 4
Project: jdk8u-jdk   File: CompositionArea.java   View Source Code Vote up 6 votes
private Rectangle getCaretRectangle(TextHitInfo caret) {
    int caretLocation = 0;
    TextLayout layout = composedTextLayout;
    if (layout != null) {
        caretLocation = Math.round(layout.getCaretInfo(caret)[0]);
    }
    Graphics g = getGraphics();
    FontMetrics metrics = null;
    try {
        metrics = g.getFontMetrics();
    } finally {
        g.dispose();
    }
    return new Rectangle(TEXT_ORIGIN_X + caretLocation,
                         TEXT_ORIGIN_Y - metrics.getAscent(),
                         0, metrics.getAscent() + metrics.getDescent());
}
 
Example 5
Project: OpenJSharp   File: MotifBorders.java   View Source Code Vote up 6 votes
/**
 * Reinitialize the insets parameter with this Border's current Insets.
 * @param c the component for which this border insets value applies
 * @param insets the object to be reinitialized
 */
public Insets getBorderInsets(Component c, Insets insets) {
    if (!(c instanceof JPopupMenu)) {
        return insets;
    }
    FontMetrics fm;
    int         descent = 0;
    int         ascent = 16;

    String title = ((JPopupMenu)c).getLabel();
    if (title == null) {
        insets.left = insets.top = insets.right = insets.bottom = 0;
        return insets;
    }

    fm = c.getFontMetrics(font);

    if(fm != null) {
        descent = fm.getDescent();
        ascent = fm.getAscent();
    }

    insets.top += ascent + descent + TEXT_SPACING + GROOVE_HEIGHT;
    return insets;
}
 
Example 6
Project: litiengine   File: GuiComponent.java   View Source Code Vote up 5 votes
private void renderText(Graphics2D g) {
  if (this.getText() == null || this.getText().isEmpty()) {
    return;
  }

  final FontMetrics fm = g.getFontMetrics();

  double defaultTextX;
  double defaultTextY = fm.getAscent() + (this.getHeight() - (fm.getAscent() + fm.getDescent())) / 2;
  switch (this.getTextAlign()) {
  case LEFT:
    defaultTextX = this.getTextXMargin();
    break;
  case RIGHT:
    defaultTextX = this.getWidth() - this.getTextXMargin() - fm.stringWidth(this.getTextToRender(g));
    break;
  case CENTER:
  default:
    defaultTextX = this.getWidth() / 2 - fm.stringWidth(this.getTextToRender(g)) / 2.0;
    break;
  }
  if (this.getTextY() == 0) {
    this.setTextY(defaultTextY);
  }

  if (this.getTextX() == 0) {
    this.setTextX(defaultTextX);
  }
  if (this.getTextAngle() == 0) {
    if (this.drawTextShadow()) {
      RenderEngine.drawTextWithShadow(g, this.getTextToRender(g), this.getX() + this.getTextX(), this.getY() + this.getTextY(), this.getTextShadowColor());
    } else {
      RenderEngine.drawText(g, this.getTextToRender(g), this.getX() + this.getTextX(), this.getY() + this.getTextY());
    }
  } else if (this.getTextAngle() == 90) {
    RenderEngine.drawRotatedText(g, this.getX() + this.getTextX(), this.getY() + this.getTextY() - fm.stringWidth(this.getTextToRender(g)), this.getTextAngle(), this.getTextToRender(g));
  } else {
    RenderEngine.drawRotatedText(g, this.getX() + this.getTextX(), this.getY() + this.getTextY(), this.getTextAngle(), this.getTextToRender(g));
  }
}
 
Example 7
Project: incubator-netbeans   File: CodeFoldingSideBar.java   View Source Code Vote up 5 votes
protected int getMarkSize(Graphics g){
    if (g != null){
        FontMetrics fm = g.getFontMetrics(getColoring().getFont());
        if (fm != null){
            int ret = fm.getAscent() - fm.getDescent();
            return ret - ret%2;
        }
    }
    return -1;
}
 
Example 8
Project: incubator-netbeans   File: NimbusViewTabDisplayerUI.java   View Source Code Vote up 5 votes
@Override
public Dimension getPreferredSize(JComponent c) {
    FontMetrics fm = getTxtFontMetrics();
    int height = fm == null ?
            19 : fm.getAscent() + 2 * fm.getDescent() + 5;
    Insets insets = c.getInsets();
    prefSize.height = height + insets.bottom + insets.top;
    return prefSize;
}
 
Example 9
Project: Princeton_Algorithms   File: StdDraw.java   View Source Code Vote up 5 votes
/**
 * Write the given text string in the current font, right-aligned at (<em>x</em>, <em>y</em>).
 *
 * @param  x the <em>x</em>-coordinate of the text
 * @param  y the <em>y</em>-coordinate of the text
 * @param  text the text to write
 */
public static void textRight(double x, double y, String text) {
    if (text == null) throw new IllegalArgumentException();
    offscreen.setFont(font);
    FontMetrics metrics = offscreen.getFontMetrics();
    double xs = scaleX(x);
    double ys = scaleY(y);
    int ws = metrics.stringWidth(text);
    int hs = metrics.getDescent();
    offscreen.drawString(text, (float) (xs - ws), (float) (ys + hs));
    draw();
}
 
Example 10
Project: incubator-netbeans   File: AbstractWinViewTabDisplayerUI.java   View Source Code Vote up 5 votes
@Override
public Dimension getPreferredSize(JComponent c) {
    FontMetrics fm = getTxtFontMetrics();
    int height = fm == null ?
            17 : fm.getAscent() + 2 * fm.getDescent() + 3;
    Insets insets = c.getInsets();
    prefSize.height = height + insets.bottom + insets.top;
    return prefSize;
}
 
Example 11
Project: brModelo   File: baseDrawerItem.java   View Source Code Vote up 5 votes
private void medidaV(Graphics2D g, int l, int t) {
    FontMetrics fm = g.getFontMetrics();
    String vl = dono.FormateUnidadeMedida(height);
    int traco = width;
    int xIni = l;// + (traco) / 2;
    int xFim = xIni + traco;
    int yIni = t;
    int yFim = t + height;
    int xLin = l + (width / 2);

    g.drawLine(xIni, yIni, xFim, yIni);
    g.drawLine(xIni, yFim, xFim, yFim);
    g.drawLine(xLin, yIni, xLin, yFim);

    int degrees = isInvertido() ? 90 : -90;
    int desse = isInvertido() ? 0 : fm.stringWidth(vl);
    //int centra = fm.getHeight() / 2 - fm.getDescent();
    int centra = fm.getHeight() - fm.getDescent();
    centra = isInvertido() ? -centra : centra;

    AffineTransform at = AffineTransform.getRotateInstance(Math.toRadians(degrees));
    Font f = new Font(g.getFont().getName(), Font.BOLD, g.getFont().getSize());
    Font f2 = g.getFont();
    g.setFont(f.deriveFont(at));
    yIni = yIni + (height - fm.stringWidth(vl)) / 2 + desse;
    g.drawString(vl, xLin + centra, yIni);
    g.setFont(f2);
}
 
Example 12
Project: Logisim   File: EditableLabel.java   View Source Code Vote up 5 votes
private void computeDimensions(Graphics g, Font font, FontMetrics fm) {
	String s = text;
	FontRenderContext frc = ((Graphics2D) g).getFontRenderContext();
	width = fm.stringWidth(s);
	ascent = fm.getAscent();
	descent = fm.getDescent();
	int[] xs = new int[s.length()];
	int[] ys = new int[s.length()];
	for (int i = 0; i < xs.length; i++) {
		xs[i] = fm.stringWidth(s.substring(0, i + 1));
		TextLayout lay = new TextLayout(s.substring(i, i + 1), font, frc);
		Rectangle2D rect = lay.getBounds();
		int asc = (int) Math.ceil(-rect.getMinY());
		int desc = (int) Math.ceil(rect.getMaxY());
		if (asc < 0)
			asc = 0;
		if (asc > 0xFFFF)
			asc = 0xFFFF;
		if (desc < 0)
			desc = 0;
		if (desc > 0xFFFF)
			desc = 0xFFFF;
		ys[i] = (asc << 16) | desc;
	}
	charX = xs;
	charY = ys;
	dimsKnown = true;
}
 
Example 13
Project: incubator-netbeans   File: ListCompletionView.java   View Source Code Vote up 5 votes
private static void renderHtml(ClipboardHistoryElement f, Graphics g, Font defaultFont, Color defaultColor,
int width, int height, boolean selected) {
    int textEnd = width - AFTER_ICON_GAP - AFTER_TEXT_GAP;
    FontMetrics fm = g.getFontMetrics(defaultFont);
    int textY = (height - fm.getHeight())/2 + fm.getHeight() - fm.getDescent();

    HtmlRenderer.renderHTML(f.getNumber() + " " + StringEscapeUtils.escapeHtml(f.getShortenText()), g, 1, textY, textEnd, textY, //NOI18N
            defaultFont, defaultColor, HtmlRenderer.STYLE_TRUNCATE, true);//, selected);
}
 
Example 14
Project: Java-Algorithms-Learning   File: StdDraw.java   View Source Code Vote up 5 votes
/**
 * Write the given text string in the current font, right-aligned at (<em>x</em>, <em>y</em>).
 *
 * @param  x the <em>x</em>-coordinate of the text
 * @param  y the <em>y</em>-coordinate of the text
 * @param  text the text to write
 */
public static void textRight(double x, double y, String text) {
    if (text == null) throw new IllegalArgumentException();
    offscreen.setFont(font);
    FontMetrics metrics = offscreen.getFontMetrics();
    double xs = scaleX(x);
    double ys = scaleY(y);
    int ws = metrics.stringWidth(text);
    int hs = metrics.getDescent();
    offscreen.drawString(text, (float) (xs - ws), (float) (ys + hs));
    draw();
}
 
Example 15
Project: openjdk-jdk10   File: MotifBorders.java   View Source Code Vote up 4 votes
/**
 * 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 16
Project: brModelo   File: baseDrawer.java   View Source Code Vote up 4 votes
public void bordaLeftRigth(Graphics2D g, boolean isrigth) {
        FontMetrics fm = g.getFontMetrics();
        String vl = FormateUnidadeMedida(H);
        int pre_x = (isrigth ? getLeftWidth() : getLeft());
        int traco = largTraco < margem ? largTraco : margem;
        traco = (isrigth ? -traco : traco);
        int xIni = pre_x + (isrigth ? -2 : 2);
        int xFim = xIni + 2 * traco;
        int yIni = getTop() + margem;
        int yFim = getTopHeight() - margem;
        int xLin = xIni;
//        int pre_x = (isrigth ? getLeftWidth() - margem : getLeft());
//        int traco = largTraco < margem ? largTraco : margem;
//        int xIni = pre_x + (margem - traco) / 2;
//        int xFim = xIni + traco;
//        int yIni = getTop() + margem;
//        int yFim = getTopHeight() - margem;
//        int xLin = pre_x + margem / 2;

        g.setColor(getCorRegua());
        g.drawLine(xIni, yIni, xFim, yIni);
        g.drawLine(xIni, yFim, xFim, yFim);
        g.drawLine(xLin, yIni, xLin, yFim);

        int blc = calculeSubEspaco(W);
        int sr = yIni;
        xFim -= traco;

        int dv = modInteiro(blc);
        int subblc = 0;
        if (dv > 0) {
            subblc = blc / dv;
        }
        while (sr < yFim) {
            if (dv > 0) {
                int a = blc - subblc;
                while (a > 0) {
                    if (sr + a < yFim) {
                        g.drawLine(xIni, sr + a, xFim - traco / 2, sr + a);
                    }
                    a -= subblc;
                }
            }
            g.drawLine(xIni, sr, xFim, sr);
            sr += blc;
        }

        if (isMostrarTextoRegua()) {
            int degrees = isrigth ? 90 : -90;
            int desse = isrigth ? 0 : fm.stringWidth(vl);
            int centra = fm.getHeight() / 2 - fm.getDescent();
            centra = isrigth ? -centra : centra;

            AffineTransform at = AffineTransform.getRotateInstance(Math.toRadians(degrees));
            Font f = new Font(getFont().getName(), Font.BOLD, getFont().getSize());
            g.setFont(f.deriveFont(at));
            g.setColor(getForeColor());
            xLin = pre_x - (isrigth ? margem / 2 : -margem / 2);
            yIni = yIni + (H - fm.stringWidth(vl)) / 2 + desse;
            g.drawString(vl, xLin + centra, yIni);
            g.setFont(getFont());
        }
    }
 
Example 17
Project: myfaces-trinidad   File: BlafImageUtils.java   View Source Code Vote up 4 votes
static ImmInsets __getDialogPadding()
{
  if (_sDialogPadding != null)
    return _sDialogPadding;

  // Create a BufferedImage that we can use to rasterize some glyphs.
  int width = 40;
  int height = 40;
  BufferedImage image = new BufferedImage(40,
                                          40,
                                          BufferedImage.TYPE_INT_ARGB);

  // Get the Graphics object to use to draw into the image
  Graphics g = image.getGraphics();

  // Clear out the image
  g.setColor(Color.white);
  g.fillRect(0, 0, width, height);

  // Render our glyphs
  g.setColor(Color.black);
  g.setFont(new Font("Dialog", Font.PLAIN, 12));

  FontMetrics metrics = g.getFontMetrics();
  int baseline = metrics.getAscent();

  g.drawString("X", 0, baseline);

  // Now that we have rendered the glyphs, we examine the
  // image to see how many lines of padding we've got.
  int top = 0;
  for (int y = 0; y < height; y++)
  {
    if (!_isWhiteScanline(image, y, width))
    {
      top = y;
      break;
    }
  }

  // Just use the descent as the bottom padding
  int bottom = metrics.getDescent();

  _sDialogPadding = new ImmInsets(top, 0, bottom, 0);


  // Clean up
  g.dispose();
  image.flush();

  return _sDialogPadding;
}
 
Example 18
Project: openjdk-jdk10   File: MotifGraphicsUtils.java   View Source Code Vote up 4 votes
static void drawStringInRect(JComponent c, Graphics g, String aString,
                                 int x, int y, int width, int height,
                                 int justification) {
        FontMetrics  fontMetrics;
        int          drawWidth, startX, startY, delta;

        if (g.getFont() == null) {
//            throw new InconsistencyException("No font set");
            return;
        }
        fontMetrics = SwingUtilities2.getFontMetrics(c, g);
        if (fontMetrics == null) {
//            throw new InconsistencyException("No metrics for Font " + font());
            return;
        }

        if (justification == CENTER) {
            drawWidth = SwingUtilities2.stringWidth(c, fontMetrics, aString);
            if (drawWidth > width) {
                drawWidth = width;
            }
            startX = x + (width - drawWidth) / 2;
        } else if (justification == RIGHT) {
            drawWidth = SwingUtilities2.stringWidth(c, fontMetrics, aString);
            if (drawWidth > width) {
                drawWidth = width;
            }
            startX = x + width - drawWidth;
        } else {
            startX = x;
        }

        delta = (height - fontMetrics.getAscent() - fontMetrics.getDescent()) / 2;
        if (delta < 0) {
            delta = 0;
        }

        startY = y + height - delta - fontMetrics.getDescent();

        SwingUtilities2.drawString(c, g, aString, startX, startY);
    }
 
Example 19
Project: Logisim   File: EditableLabel.java   View Source Code Vote up 4 votes
public void configureTextField(EditableLabelField field, double zoom) {
	Font f = font;
	if (zoom != 1.0) {
		f = f.deriveFont(AffineTransform.getScaleInstance(zoom, zoom));
	}
	field.setFont(f);

	Dimension dim = field.getPreferredSize();
	int w;
	int border = EditableLabelField.FIELD_BORDER;
	if (dimsKnown) {
		w = width + 1 + 2 * border;
	} else {
		FontMetrics fm = field.getFontMetrics(font);
		ascent = fm.getAscent();
		descent = fm.getDescent();
		w = 0;
	}

	int x0 = x;
	int y0 = getBaseY() - ascent;
	if (zoom != 1.0) {
		x0 = (int) Math.round(x0 * zoom);
		y0 = (int) Math.round(y0 * zoom);
		w = (int) Math.round(w * zoom);
	}

	w = Math.max(w, dim.width);
	int h = dim.height;
	switch (horzAlign) {
	case LEFT:
		x0 = x0 - border;
		break;
	case CENTER:
		x0 = x0 - (w / 2) + 1;
		break;
	case RIGHT:
		x0 = x0 - w + border + 1;
		break;
	default:
		x0 = x0 - border;
	}
	y0 = y0 - border;

	field.setHorizontalAlignment(horzAlign);
	field.setForeground(color);
	field.setBounds(x0, y0, w, h);
}
 
Example 20
Project: brModelo   File: baseDrawer.java   View Source Code Vote up 4 votes
public void bordaTopDown(Graphics2D g, boolean isTop) {
        FontMetrics fm = g.getFontMetrics();
        String vl = FormateUnidadeMedida(W);
        int xini = getLeft() + margem;
        int pre_y = (isTop ? getTop() : getTopHeight());
        int xfim = getLeftWidth() - margem;
        int yfim = pre_y + (isTop ? 2 : -2);

        int traco = largTraco < margem ? largTraco : margem;
        traco = (isTop ? traco : -traco);
        int ytraco = yfim;
//        int xini = getLeft() + margem;
//        int pre_y = (isTop ? getTop() : getTopHeight() - margem);
//        int xfim = getLeftWidth() - margem;
//        int yfim = pre_y + margem / 2;
//
//        int traco = largTraco < margem ? largTraco : margem;
//        int ytraco = pre_y + (margem - traco) / 2;

        g.setColor(getCorRegua());
        g.drawLine(xini, ytraco, xini, ytraco + 2 * traco);
        g.drawLine(xfim, ytraco, xfim, ytraco + 2 * traco);
        g.drawLine(xini, yfim, xfim, yfim);

        int blc = calculeSubEspaco(W);
        int sr = xini;
        int dv = modInteiro(blc);
        int subblc = 0;
        if (dv > 0) {
            subblc = blc / dv;
        }
        while (sr < xfim) {
            if (dv > 0) {
                int a = subblc;
                while (a < blc) {
                    if (sr + a < xfim) {
                        g.drawLine(sr + a, ytraco, sr + a, ytraco + traco / 2);
                    }
                    a += subblc;
                }
            }
            sr += blc;
            if (sr < xfim) {
                g.drawLine(sr, ytraco, sr, ytraco + traco);
            }
        }

        if (isMostrarTextoRegua()) {
            g.setColor(getForeColor());
            yfim = pre_y + (isTop ? margem / 2 : -margem / 2);
            xini = xini + (W - fm.stringWidth(vl)) / 2;
            int yini = yfim + (fm.getHeight()) / 2 - fm.getDescent();
            g.drawString(vl, xini, yini);
        }
    }