java.awt.Graphics2D.getFontRenderContext()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(10.2k)|赞(0)|评价(0)|浏览(193)

本文整理了Java中java.awt.Graphics2D.getFontRenderContext()方法的一些代码示例,展示了Graphics2D.getFontRenderContext()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Graphics2D.getFontRenderContext()方法的具体详情如下:
包路径:java.awt.Graphics2D
类名称:Graphics2D
方法名:getFontRenderContext

Graphics2D.getFontRenderContext介绍

暂无

代码示例

代码示例来源:origin: stackoverflow.com

Graphics2D g = ...;
Point2D loc = ...;
Font font = Font.getFont("Helvetica-bold-italic");
FontRenderContext frc = g.getFontRenderContext();
TextLayout layout = new TextLayout("This is a string", font, frc);
layout.draw(g, (float)loc.getX(), (float)loc.getY());

Rectangle2D bounds = layout.getBounds();
bounds.setRect(bounds.getX()+loc.getX(),
       bounds.getY()+loc.getY(),
       bounds.getWidth(),
       bounds.getHeight());
g.draw(bounds);

代码示例来源:origin: Dreampie/Resty

protected TextString convertToCharacters(String text, Graphics2D g, FontFactory fontFactory, ColorFactory colorFactory) {
 TextString characters = new TextString();
 FontRenderContext frc = g.getFontRenderContext();
 double lastx = 0;
 for (int i = 0; i < text.length(); i++) {
  Font font = fontFactory.getFont(i);
  char c = text.charAt(i);
  FontMetrics fm = g.getFontMetrics(font);
  Rectangle2D bounds = font.getStringBounds(String.valueOf(c), frc);
  TextCharacter tc = new TextCharacter();
  tc.setCharacter(c);
  tc.setFont(font);
  tc.setWidth(fm.charWidth(c));
  tc.setHeight(fm.getAscent() + fm.getDescent());
  tc.setAscent(fm.getAscent());
  tc.setDescent(fm.getDescent());
  tc.setX(lastx);
  tc.setY(0);
  tc.setFont(font);
  tc.setColor(colorFactory.getColor(i));
  lastx += bounds.getWidth();
  characters.addCharacter(tc);
 }
 return characters;
}

代码示例来源:origin: magefree/mage

private int calculateOffsetForTop(Graphics2D g2d, String text) {
  if (topTextOffsetX == -1) { // calculate once
    FontRenderContext frc = g2d.getFontRenderContext();
    int textWidth = (int) textFont.getStringBounds(text, frc).getWidth();
    int neededImageWidth = (topTextImage == null ? 0 : topTextImage.getWidth(this));
    int availableXWidth = imageSize.width - neededImageWidth;
    topTextOffsetX = (availableXWidth - textWidth) / 2 + neededImageWidth;
  }
  return topTextOffsetX;
}

代码示例来源:origin: magefree/mage

private int calculateOffset(Graphics2D g2d) {
  if (textOffsetX == -1) { // calculate once
    FontRenderContext frc = g2d.getFontRenderContext();
    int textWidth = (int) textFont.getStringBounds(text, frc).getWidth();
    if (textWidth > buttonSize.width) {
      g2d.setFont(textFontMini);
      useMiniFont = true;
      frc = g2d.getFontRenderContext();
      textWidth = (int) textFontMini.getStringBounds(text, frc).getWidth();
    }
    if (alignTextLeft) {
      textOffsetX = 0;
    } else {
      textOffsetX = (imageSize.width - textWidth) / 2;
    }
  }
  return textOffsetX;
}

代码示例来源:origin: nodebox/nodebox

public static void drawCenteredShadowText(Graphics2D g2, String s, int x, int y, Color shadowColor) {
  FontRenderContext frc = g2.getFontRenderContext();
  Rectangle2D bounds = g2.getFont().getStringBounds(s, frc);
  int leftX = (int) (x - (float) bounds.getWidth() / 2);
  drawShadowText(g2, s, leftX, y, shadowColor, 1);
}

代码示例来源:origin: geotools/geotools

final int length = message.length();
final Font font = graphics.getFont();
final FontRenderContext context = graphics.getFontRenderContext();
for (int i = 0; i < length; ) {
  int ir = message.indexOf('\r', i);
  final GlyphVector line = font.createGlyphVector(context, message.substring(i, irn));
  final Rectangle2D rect = line.getVisualBounds();
  final double w = rect.getWidth();
  if (w > width) width = w;
  height += rect.getHeight();

代码示例来源:origin: knowm/XChart

&& chart.getStyler().isYAxisTitleVisible()) {
FontRenderContext frc = g.getFontRenderContext();
TextLayout nonRotatedTextLayout =
  new TextLayout(yAxisTitle, chart.getStyler().getAxisTitleFont(), frc);
   (int)
     (yAxis.getAxisTick().getBounds().getX()
       + yAxis.getAxisTick().getBounds().getWidth()
       + nonRotatedRectangle.getHeight());
} else {
    ((yAxis.getBounds().getHeight() + nonRotatedRectangle.getWidth()) / 2.0
      + yAxis.getBounds().getY());
  new Rectangle2D.Double(
    xOffset - nonRotatedRectangle.getHeight(),
    yOffset - nonRotatedRectangle.getWidth(),
    nonRotatedRectangle.getHeight() + chart.getStyler().getAxisTitlePadding(),
    nonRotatedRectangle.getWidth());
 && chart.getStyler().isXAxisTitleVisible()) {
FontRenderContext frc = g.getFontRenderContext();
TextLayout textLayout =
  new TextLayout(chart.getXAxisTitle(), chart.getStyler().getAxisTitleFont(), frc);

代码示例来源:origin: knowm/XChart

FontRenderContext frc = g.getFontRenderContext();
TextLayout textLayout =
  new TextLayout(chart.getTitle(), chart.getStyler().getChartTitleFont(), frc);
 double chartTitleBoxWidth = chart.getPlot().getBounds().getWidth();
 double chartTitleBoxHeight =
   textBounds.getHeight() + 2 * chart.getStyler().getChartTitlePadding();
    + (chart.getPlot().getBounds().getWidth() - textBounds.getWidth()) / 2.0;
yOffset =
  chart.getStyler().getChartPadding()

代码示例来源:origin: knowm/XChart

: 0);
} else {
 double xWidth = yAxis.getAxisTitle().getBounds().getWidth();
 xOffset = yAxis.getAxisTitle().getBounds().getX() + xWidth;
   && flippedTickLocation > yOffset
  FontRenderContext frc = g.getFontRenderContext();
  TextLayout axisLabelTextLayout =
    new TextLayout(tickLabel, styler.getAxisTickLabelsFont(), frc);
  Rectangle2D tickLabelBounds = axisLabelTextLayout.getBounds();
  double boundWidth = tickLabelBounds.getWidth();
  if (boundWidth > maxTickLabelWidth) {
   maxTickLabelWidth = boundWidth;
 double boundWidth = tickLabelBounds.getWidth();
 double xPos;
 switch (styler.getYAxisLabelAlignment()) {
  FontRenderContext frc = g.getFontRenderContext();
  TextLayout textLayout = new TextLayout(tickLabel, styler.getAxisTickLabelsFont(), frc);
  FontRenderContext frc = g.getFontRenderContext();
  TextLayout textLayout = new TextLayout(tickLabel, styler.getAxisTickLabelsFont(), frc);

代码示例来源:origin: edu.toronto.cs.savant/savant-core

private boolean fontFits(Font font, double width, Graphics2D g2) {
  String baseChar = "G";
  Rectangle2D charRect = font.getStringBounds(baseChar, g2.getFontRenderContext());
  if (charRect.getWidth() > width) return false;
  else return true;
}

代码示例来源:origin: edu.toronto.cs.savant/savant-core

/**
   * Patterned off GraphPane.drawMessageHelper, draws a string centred in the given box.
   */
  public static void drawMessage(Graphics2D g2, String message, Rectangle2D box){
    FontMetrics metrics = g2.getFontMetrics();
    Rectangle2D stringBounds = g2.getFont().getStringBounds(message, g2.getFontRenderContext());
    float x = (float)(box.getX() + (box.getWidth() - stringBounds.getWidth()) / 2.0);
    float y = (float)(box.getY() + (box.getHeight() + metrics.getAscent() - metrics.getDescent()) / 2.0);

    g2.drawString(message, x, y);
  }
}

代码示例来源:origin: stackoverflow.com

private static Rectangle2D computeBoundsUsingNormalizedFont(
    String string, Graphics2D g) {
  Font normalizedFont = g.getFont().deriveFont(1f);
  Rectangle2D bounds = normalizedFont.getStringBounds(string, g.getFontRenderContext());

  float scale = g.getFont().getSize2D();
  return new Rectangle2D.Double(bounds.getX() * scale,
      bounds.getY() * scale,
      bounds.getWidth() * scale,
      bounds.getHeight() * scale);
}

代码示例来源:origin: edu.toronto.cs.medsavant/medsavant-client

/**
 * Draws a string centred in the given box.
 */
public static void drawCentred(Graphics2D g2, String message, Rectangle2D box) {
  FontMetrics metrics = g2.getFontMetrics();
  Rectangle2D stringBounds = g2.getFont().getStringBounds(message, g2.getFontRenderContext());
  float x = (float) (box.getX() + (box.getWidth() - stringBounds.getWidth()) / 2.0);
  float y = (float) (box.getY() + (box.getHeight() + metrics.getAscent() - metrics.getDescent()) / 2.0);
  g2.drawString(message, x, y);
}

代码示例来源:origin: ontopia/ontopia

protected int getWidth(String title) {
 if (g2d == null)
  return title.length()*8 + 30;
 
 Font font = g2d.getFont();
 int width = (int)
  font.getStringBounds(title, g2d.getFontRenderContext()).getWidth();
 return width + 30;
}

代码示例来源:origin: CognitiveJ/cognitivej

public static Font calculateMaxFontForString(Graphics2D graphics2D, Font font, int maxWidth, double maxHeight, String text) {
    float fontSize = font.getSize();
    FontRenderContext fontRenderContext = graphics2D.getFontRenderContext();
    Rectangle2D bounds = font.getStringBounds(text, fontRenderContext);
    while (bounds.getWidth() > maxWidth || bounds.getHeight()>maxHeight) {

      font = font.deriveFont((fontSize -= 2));
      bounds = font.getStringBounds(text, fontRenderContext);
    }
    return font;
  }
}

代码示例来源:origin: org.orbisgis/mapeditor

private void drawTextWithWhiteBackGround(Graphics2D g2, String text,
    Point2D p) {
    TextLayout tl = new TextLayout(text, g2.getFont(), g2.getFontRenderContext());
    g2.setColor(Color.WHITE);
    Rectangle2D textBounds = tl.getBounds();
    g2.fill(new Rectangle2D.Double(textBounds.getX() + p.getX(), textBounds.getY()
        + p.getY(), textBounds.getWidth(), textBounds.getHeight()));
    g2.setColor(Color.BLACK);
    tl.draw(g2, (float) p.getX(), (float) p.getY());
}

代码示例来源:origin: edu.toronto.cs.savant/savant-core

private void drawMessageHelper(Graphics2D g2, String message, Font font, int w, int h, int offset) {
  g2.setFont(font);
  FontMetrics metrics = g2.getFontMetrics();
  Rectangle2D stringBounds = font.getStringBounds(message, g2.getFontRenderContext());
  int preferredWidth = (int) stringBounds.getWidth() + metrics.getHeight();
  int preferredHeight = (int) stringBounds.getHeight() + metrics.getHeight();
  w = Math.min(preferredWidth, w);
  h = Math.min(preferredHeight, h);
  int x = (getWidth() - (int) stringBounds.getWidth()) / 2;
  int y = (getHeight() / 2) + ((metrics.getAscent() - metrics.getDescent()) / 2) + offset;
  g2.drawString(message, x, y);
}

代码示例来源:origin: uk.org.okapibarcode/okapibarcode

private static Font addTracking(Font baseFont, double maxTextWidth, String text, Graphics2D g2d) {
    FontRenderContext frc = g2d.getFontRenderContext();
    double originalWidth = baseFont.getStringBounds(text, frc).getWidth();
    double extraSpace = maxTextWidth - originalWidth;
    double extraSpacePerGap = extraSpace / (text.length() - 1);
    double tracking = extraSpacePerGap / baseFont.getSize2D();
    return baseFont.deriveFont(Collections.singletonMap(TextAttribute.TRACKING, tracking));
  }
}

代码示例来源:origin: net.imagej/ij

/** Returns a rectangle enclosing the pixels affected by drawString
 *  assuming it is drawn at (x=0, y=0). As drawString draws above the drawing location,
 *  the y coordinate of the rectangle is negative. */
public Rectangle getStringBounds(String s) {
  setupFontMetrics();
  GlyphVector gv = font.createGlyphVector(fmGraphics.getFontRenderContext(), s);
  Rectangle2D rect = gv.getPixelBounds(null, 0.f, -fontMetrics.getDescent());
  return new Rectangle((int)rect.getX(), (int)rect.getY(), (int)rect.getWidth(), (int)rect.getHeight());
}

代码示例来源:origin: stackoverflow.com

Graphics2D g = ...;
 Point2D loc = ...;
 Font font = Font.getFont("Helvetica-bold-italic");
 FontRenderContext frc = g.getFontRenderContext();
 TextLayout layout = new TextLayout("This is a string", font, frc);
 layout.draw(g, (float)loc.getX(), (float)loc.getY());
 Rectangle2D bounds = layout.getBounds();
 bounds.setRect(bounds.getX()+loc.getX(),
        bounds.getY()+loc.getY(),
        bounds.getWidth(),
        bounds.getHeight());
 g.draw(bounds);

相关文章

Graphics2D类方法