本文整理了Java中java.awt.Graphics2D.getFontMetrics()
方法的一些代码示例,展示了Graphics2D.getFontMetrics()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Graphics2D.getFontMetrics()
方法的具体详情如下:
包路径:java.awt.Graphics2D
类名称:Graphics2D
方法名:getFontMetrics
暂无
代码示例来源:origin: pentaho/pentaho-kettle
public void drawText( String text, int x, int y ) {
int height = gc.getFontMetrics().getHeight();
String[] lines = text.split( "\n" );
for ( String line : lines ) {
gc.drawString( line, x + xOffset, y + height + yOffset );
y += height;
}
}
代码示例来源:origin: libgdx/libgdx
protected void paintComponent (Graphics graphics) {
super.paintComponent(graphics);
Graphics2D g = (Graphics2D)graphics;
int width = getWidth();
int height = getHeight();
g.setColor(bgColor);
g.fillRect(border, border, width - border * 2, height - border * 2);
int maxKnobX = width - border - KNOB_WIDTH;
int knobX = (int)((width - border * 2 - KNOB_WIDTH) * (value - sliderMin) / (sliderMax - sliderMin)) + border;
g.setColor(knobColor);
g.fillRect(Math.max(border, Math.min(maxKnobX, knobX)), 0, KNOB_WIDTH, height);
float displayValue = (int)(value * 10) / 10f;
String label = displayValue == (int)displayValue ? String.valueOf((int)displayValue) : String.valueOf(displayValue);
FontMetrics metrics = g.getFontMetrics();
int labelWidth = metrics.stringWidth(label);
g.setColor(Color.white);
g.drawString(label, width / 2 - labelWidth / 2, height / 2 + metrics.getAscent() / 2);
}
代码示例来源:origin: plantuml/plantuml
public int getAscent(Font font){
fakeGraphics.setFont(font);
FontMetrics metrics = fakeGraphics.getFontMetrics();
if(DEBUG) System.out.println("Ascent: "+metrics.getAscent());
return metrics.getAscent();
}
代码示例来源:origin: locationtech/jts
public static void drawStringAlignCenter(Graphics2D g2d, String s, int x, int y) {
int stringLen = (int) g2d.getFontMetrics().getStringBounds(s, g2d).getWidth();
g2d.drawString(s, x - stringLen /2, y);
}
}
代码示例来源:origin: runelite/runelite
private void drawMapIcons(BufferedImage image, int drawBaseX, int drawBaseY, Region region, int z)
{
int baseX = region.getBaseX();
int baseY = region.getBaseY();
Graphics2D graphics = image.createGraphics();
drawMapIcons(graphics, region, z, drawBaseX, drawBaseY);
if (labelRegions)
{
graphics.setColor(Color.WHITE);
String str = baseX + "," + baseY + " (" + region.getRegionX() + "," + region.getRegionY() + ")";
graphics.drawString(str, drawBaseX * MAP_SCALE, drawBaseY * MAP_SCALE + graphics.getFontMetrics().getHeight());
}
if (outlineRegions)
{
graphics.setColor(Color.WHITE);
graphics.drawRect(drawBaseX * MAP_SCALE, drawBaseY * MAP_SCALE, Region.X * MAP_SCALE, Region.Y * MAP_SCALE);
}
graphics.dispose();
}
代码示例来源:origin: stackoverflow.com
Graphics2D g2d = (Graphics2D) g;
FontMetrics fm = g2d.getFontMetrics();
Rectangle2D r = fm.getStringBounds(stringTime, g2d);
int x = (this.getWidth() - (int) r.getWidth()) / 2;
int y = (this.getHeight() - (int) r.getHeight()) / 2 + fm.getAscent();
g.drawString(stringTime, x, y);
代码示例来源:origin: runelite/runelite
final FontMetrics metrics = graphics.getFontMetrics();
final int progressTextX = barX + (width - metrics.stringWidth(textToWrite)) / 2;
final int progressTextY = barY + ((height - metrics.getHeight()) / 2) + metrics.getHeight();
final int progressFill = (int) (width * pc);
graphics.setColor(backgroundColor);
graphics.fillRect(barX, barY, width, height);
graphics.setColor(foregroundColor);
graphics.fillRect(barX, barY, progressFill, height);
代码示例来源:origin: runelite/runelite
@Override
public Dimension render(Graphics2D graphics)
{
if (!config.drawFps())
{
return null;
}
final String text = client.getFPS() + FPS_STRING;
final int textWidth = graphics.getFontMetrics().stringWidth(text);
final int textHeight = graphics.getFontMetrics().getAscent() - graphics.getFontMetrics().getDescent();
final int width = (int) client.getRealDimensions().getWidth();
final Point point = new Point(width - textWidth - VALUE_X_OFFSET, textHeight + Y_OFFSET);
OverlayUtil.renderTextLocation(graphics, point, text, getFpsValueColor());
return null;
}
}
代码示例来源:origin: plantuml/plantuml
final Color extended = mapper.getMappedColor(fontConfiguration.getExtendedColor());
if (extended != null) {
g2d.setColor(extended);
g2d.setBackground(extended);
g2d.fill(new Rectangle2D.Double(x, y - dimBack.getHeight() + 1.5, dimBack.getWidth(), dimBack
visible.ensureVisible(x + dimBack.getWidth(), y + 1.5);
g2d.setFont(font.getFont());
g2d.setColor(mapper.getMappedColor(fontConfiguration.getColor()));
final TextLayout t = new TextLayout(shape.getText(), font.getFont(), fontRenderContext);
g2d.translate(x, y);
final HtmlColor extended = fontConfiguration.getExtendedColor();
if (extended != null) {
g2d.setColor(mapper.getMappedColor(extended));
final FontMetrics fm = g2d.getFontMetrics(font.getFont());
final int ypos = (int) (y - fm.getDescent() - 0.5);
final HtmlColor extended = fontConfiguration.getExtendedColor();
代码示例来源:origin: kevin-wayne/algs4
/**
* Write the given text string in the current font, left-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
*/
public static void textLeft(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 hs = metrics.getDescent();
offscreen.drawString(text, (float) xs, (float) (ys + hs));
draw();
}
代码示例来源:origin: runelite/runelite
private void renderIconsAndCounters(Graphics2D graphics, int x, int y, Image image, String counterText, int counterPadding)
{
final int widthOfCounter = graphics.getFontMetrics().stringWidth(counterText);
final int centerText = (WIDTH - PADDING) / 2 - (widthOfCounter / 2);
if (config.enableCounter())
{
graphics.setFont(FontManager.getRunescapeSmallFont());
textComponent.setColor(Color.WHITE);
textComponent.setText(counterText);
textComponent.setPosition(new java.awt.Point(x + centerText + counterPadding, y + COUNTER_ICON_HEIGHT));
}
else
{
textComponent.setText("");
}
if (config.enableSkillIcon())
{
graphics.drawImage(image, x + ICON_AND_COUNTER_OFFSET_X + PADDING, y + ICON_AND_COUNTER_OFFSET_Y - image.getWidth(null), null);
textComponent.setPosition(new java.awt.Point(x + centerText + counterPadding, y + SKILL_ICON_HEIGHT));
}
textComponent.render(graphics);
}
}
代码示例来源:origin: apache/geode
g.draw(path);
g.fillArc(startX, y - CIRCLE_WIDTH / 2, CIRCLE_WIDTH, CIRCLE_WIDTH, 0, 360);
g.drawString(label, startX + LABEL_OFFSET, y - LABEL_OFFSET);
} else {
int startX = x1;
path.lineTo(endX + ARROW_WIDTH, y + ARROW_WIDTH);
g.draw(path);
int labelWidth = g.getFontMetrics().stringWidth(label);
g.fillArc(startX - CIRCLE_WIDTH / 2, y - CIRCLE_WIDTH / 2, CIRCLE_WIDTH, CIRCLE_WIDTH, 0,
360);
g.drawString(label, startX - LABEL_OFFSET - labelWidth, y - LABEL_OFFSET);
代码示例来源:origin: libgdx/libgdx
protected void paintComponent (Graphics graphics) {
super.paintComponent(graphics);
Graphics2D g = (Graphics2D)graphics;
int width = getWidth();
int height = getHeight();
g.setColor(bgColor);
g.fillRect(border, border, width - border * 2, height - border * 2);
int maxKnobX = width - border - KNOB_WIDTH;
int knobX = (int)((width - border * 2 - KNOB_WIDTH) * (value - sliderMin) / (sliderMax - sliderMin)) + border;
g.setColor(knobColor);
g.fillRect(Math.max(border, Math.min(maxKnobX, knobX)), 0, KNOB_WIDTH, height);
float displayValue = (int)(value * 10) / 10f;
String label = displayValue == (int)displayValue ? String.valueOf((int)displayValue) : String.valueOf(displayValue);
FontMetrics metrics = g.getFontMetrics();
int labelWidth = metrics.stringWidth(label);
g.setColor(Color.white);
g.drawString(label, width / 2 - labelWidth / 2, height / 2 + metrics.getAscent() / 2);
}
代码示例来源:origin: org.netbeans.modules/org-netbeans-lib-profiler-ui
protected void drawVerticalAxisLegendItem(Graphics2D g2, int y, String string) {
int legendWidth = (int) g2.getFontMetrics().getStringBounds(string, g2).getWidth();
int legendHeight = vertLegendHeight;
int legendX = vertAxisWidth - legendWidth - 5;
int legendY = Math.max((y + (legendHeight / 2)) - 2, (2 * legendHeight) + 3);
g2.drawString(string, legendX, legendY);
}
代码示例来源:origin: pentaho/pentaho-kettle
public Point textExtent( String text ) {
String[] lines = text.split( Const.CR );
int maxWidth = 0;
for ( String line : lines ) {
Rectangle2D bounds = TextUtilities.getTextBounds( line, gc, gc.getFontMetrics() );
if ( bounds.getWidth() > maxWidth ) {
maxWidth = (int) bounds.getWidth();
}
}
int height = gc.getFontMetrics().getHeight() * lines.length;
return new Point( maxWidth, height );
}
代码示例来源:origin: runelite/runelite
final FontMetrics metrics = graphics.getFontMetrics();
final int textDescent = metrics.getDescent();
final int textHeight = metrics.getHeight();
backgroundComponent.setRectangle(tooltipBackground);
backgroundComponent.render(graphics);
graphics.setColor(Color.WHITE);
textComponent.render(graphics);
lineX += metrics.stringWidth(text);
textComponent.render(graphics);
lineX += metrics.stringWidth(text);
代码示例来源:origin: runelite/runelite
@Override
public Dimension render(Graphics2D graphics)
{
graphics.translate(preferredLocation.x, preferredLocation.y);
final FontMetrics metrics = graphics.getFontMetrics();
final TextComponent titleComponent = new TextComponent();
titleComponent.setText(text);
titleComponent.setColor(color);
titleComponent.setPosition(new Point((preferredSize.width - metrics.stringWidth(text)) / 2, metrics.getHeight()));
final Dimension dimension = titleComponent.render(graphics);
graphics.translate(-preferredLocation.x, -preferredLocation.y);
return new Dimension(preferredSize.width, dimension.height);
}
}
代码示例来源:origin: kevin-wayne/algs4
/**
* Writes the given text string in the current font, left-aligned at (x, y).
*
* @param x the x-coordinate of the text
* @param y the y-coordinate of the text
* @param s the text
*/
public void textLeft(double x, double y, String s) {
offscreen.setFont(font);
FontMetrics metrics = offscreen.getFontMetrics();
double xs = scaleX(x);
double ys = scaleY(y);
// int ws = metrics.stringWidth(s);
int hs = metrics.getDescent();
offscreen.drawString(s, (float) xs, (float) (ys + hs));
draw();
}
代码示例来源:origin: runelite/runelite
graphics.setFont(getSize() < DEFAULT_SIZE ? FontManager.getRunescapeSmallFont() : FontManager.getRunescapeFont());
graphics.translate(preferredLocation.x, preferredLocation.y);
final FontMetrics metrics = graphics.getFontMetrics();
final int size = getSize();
final Rectangle bounds = new Rectangle(size, size);
textComponent.setColor(color);
textComponent.setText(text);
textComponent.setPosition(new Point(((size - metrics.stringWidth(text)) / 2), size - SEPARATOR));
textComponent.render(graphics);
代码示例来源:origin: stanfordnlp/CoreNLP
protected FontMetrics pickFont(Graphics2D g2, Tree tree, Dimension space) {
Font font = g2.getFont();
String fontName = font.getName();
int style = font.getStyle();
for (int size = maxFontSize; size > minFontSize; size--) {
font = new Font(fontName, style, size);
g2.setFont(font);
FontMetrics fontMetrics = g2.getFontMetrics();
if (height(tree, fontMetrics) > space.getHeight()) {
continue;
}
if (width(tree, fontMetrics) > space.getWidth()) {
continue;
}
//System.out.println("Chose: "+size+" for space: "+space.getWidth());
return fontMetrics;
}
font = new Font(fontName, style, minFontSize);
g2.setFont(font);
return g2.getFontMetrics();
}
内容来源于网络,如有侵权,请联系作者删除!