本文整理汇总了Java中java.awt.Graphics2D.getFontMetrics方法的典型用法代码示例。如果您正苦于以下问题:Java Graphics2D.getFontMetrics方法的具体用法?Java Graphics2D.getFontMetrics怎么用?Java Graphics2D.getFontMetrics使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.awt.Graphics2D
的用法示例。
在下文中一共展示了Graphics2D.getFontMetrics方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getLabelEnclosure
import java.awt.Graphics2D; //导入方法依赖的package包/类
/**
* Returns a rectangle that encloses the axis label. This is typically
* used for layout purposes (it gives the maximum dimensions of the label).
*
* @param g2 the graphics device.
* @param edge the edge of the plot area along which the axis is measuring.
*
* @return The enclosing rectangle.
*/
protected Rectangle2D getLabelEnclosure(Graphics2D g2, RectangleEdge edge) {
Rectangle2D result = new Rectangle2D.Double();
String axisLabel = getLabel();
if (axisLabel != null && !axisLabel.equals("")) {
FontMetrics fm = g2.getFontMetrics(getLabelFont());
Rectangle2D bounds = TextUtilities.getTextBounds(axisLabel, g2, fm);
RectangleInsets insets = getLabelInsets();
bounds = insets.createOutsetRectangle(bounds);
double angle = getLabelAngle();
if (edge == RectangleEdge.LEFT || edge == RectangleEdge.RIGHT) {
angle = angle - Math.PI / 2.0;
}
double x = bounds.getCenterX();
double y = bounds.getCenterY();
AffineTransform transformer
= AffineTransform.getRotateInstance(angle, x, y);
Shape labelBounds = transformer.createTransformedShape(bounds);
result = labelBounds.getBounds2D();
}
return result;
}
示例2: medidaH
import java.awt.Graphics2D; //导入方法依赖的package包/类
private void medidaH(Graphics2D g, int l, int t) {
FontMetrics fm = g.getFontMetrics();
String vl = dono.FormateUnidadeMedida(width);
int xini = l;
int pre_y = t;
int xfim = l + width;
int yfim = t + height / 2;
int traco = height;
int ytraco = pre_y;// - (traco/2);
g.drawLine(xini, ytraco, xini, ytraco + traco);
g.drawLine(xfim, ytraco, xfim, ytraco + traco);
g.drawLine(xini, yfim, xfim, yfim);
xini = xini + (width - fm.stringWidth(vl)) / 2;
int yini = invertido ? yfim + (fm.getHeight() - fm.getDescent()) : yfim - fm.getDescent();// yfim + (fm.getHeight()) / 2 - fm.getDescent();
g.drawString(vl, xini, yini);
}
示例3: drawTitleBar
import java.awt.Graphics2D; //导入方法依赖的package包/类
private void drawTitleBar(Graphics2D g) {
Graphics2D g2 = (Graphics2D) g.create();
RoundRectangle2D titleBar = createTitleBarShape();
double h = SF * (TITLE_BAR_HEIGHT + CORNER_RADIUS) + 1;
g2.setPaint(new GradientPaint(0, 0, grayColor(GRAY_TOP),
0, (float) h, grayColor(GRAY_BOTTOM)));
g2.fill(titleBar);
g2.setColor(Color.DARK_GRAY);
g2.draw(titleBar);
g2.setColor(Color.BLACK);
int scaledSize = (int) Math.round(SF * TITLE_FONT_SIZE);
g2.setFont(Font.decode(TITLE_FONT_FAMILY + "-" + scaledSize));
FontMetrics fm = g2.getFontMetrics();
int x = (int) Math.round((sw - fm.stringWidth(title)) / 2);
int y = (int) Math.round(SF * (TITLE_BAR_HEIGHT / 2 + TITLE_DY));
g2.drawString(title, x, y);
drawBall(g2, RED_BALL, 0);
drawBall(g2, AMBER_BALL, 1);
drawBall(g2, GREEN_BALL, 2);
g2.dispose();
}
示例4: drawTileName
import java.awt.Graphics2D; //导入方法依赖的package包/类
private static BufferedImage drawTileName(BufferedImage image, String name) {
BufferedImage i2 = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = i2.createGraphics();
g2.drawImage(image, null, 0, 0);
g2.setColor(Color.red);
g2.setStroke(new BasicStroke(4));
g2.drawRect(8, 8, image.getWidth() - 8, image.getHeight() - 8);
g2.setColor(Color.RED);
g2.setFont(g2.getFont().deriveFont(Font.BOLD).deriveFont(13f));
FontMetrics fm = g2.getFontMetrics();
String[] parts = name.split("/");
for (int i = 0; i < parts.length; i++) {
String s = i == parts.length - 1 ? parts[i] : parts[i] + "/";
int x = 40 + i * 5;
int y = 40 + i * (fm.getHeight() + 5);
Rectangle2D rect = fm.getStringBounds(s, g2);
g2.setColor(Color.white);
g2.fillRect(x, y - fm.getAscent(), (int)rect.getWidth(), fm.getHeight());
g2.setColor(Color.red);
g2.drawString(s, x, y);
}
return i2;
}
示例5: calc
import java.awt.Graphics2D; //导入方法依赖的package包/类
/**
* Allocates the nonbold and bold fonts, then calculates the max ascent and
* descent.
*/
private static void calc() {
if (cachedMaxDescent >= 0)
return; // already done
BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
cachedGraphics = (Graphics2D) (image.getGraphics());
cachedPlainMetrics = cachedGraphics.getFontMetrics(cachedPlainFont = new Font(fontName, Font.PLAIN, fontSize));
cachedBoldMetrics = cachedGraphics.getFontMetrics(cachedBoldFont = new Font(fontName, Font.BOLD, fontSize));
cachedGraphics.setFont(cachedPlainFont);
cachedMaxAscent = cachedPlainMetrics.getMaxAscent();
cachedMaxDescent = cachedPlainMetrics.getMaxDescent();
}
示例6: refreshFontMetrics
import java.awt.Graphics2D; //导入方法依赖的package包/类
void refreshFontMetrics(Graphics2D g2d) {
// It is assumed that any rendering hints are already applied to g2d.
for (int i=0; i<styles.length; i++) {
Style s = styles[i];
if (s!=null) {
s.fontMetrics = s.font==null ? null :
g2d.getFontMetrics(s.font);
}
}
}
示例7: getTextToRender
import java.awt.Graphics2D; //导入方法依赖的package包/类
public String getTextToRender(final Graphics2D g) {
if (this.getText() == null) {
return "";
}
final FontMetrics fm = g.getFontMetrics();
String newText = this.getText();
while (newText.length() > 1 && fm.stringWidth(newText) >= this.getWidth() - this.getTextXMargin()) {
newText = newText.substring(1, newText.length());
}
return newText;
}
示例8: renderText
import java.awt.Graphics2D; //导入方法依赖的package包/类
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));
}
}
示例9: findMaximumTickLabelWidth
import java.awt.Graphics2D; //导入方法依赖的package包/类
/**
* A utility method for determining the width of the widest tick label.
*
* @param ticks the ticks.
* @param g2 the graphics device.
* @param drawArea the area within which the plot and axes should be drawn.
* @param vertical a flag that indicates whether or not the tick labels
* are 'vertical'.
*
* @return The width of the tallest tick label.
*/
protected double findMaximumTickLabelWidth(List ticks,
Graphics2D g2,
Rectangle2D drawArea,
boolean vertical) {
RectangleInsets insets = getTickLabelInsets();
Font font = getTickLabelFont();
double maxWidth = 0.0;
if (!vertical) {
FontMetrics fm = g2.getFontMetrics(font);
Iterator iterator = ticks.iterator();
while (iterator.hasNext()) {
Tick tick = (Tick) iterator.next();
Rectangle2D labelBounds = TextUtilities.getTextBounds(
tick.getText(), g2, fm);
if (labelBounds.getWidth() + insets.getLeft()
+ insets.getRight() > maxWidth) {
maxWidth = labelBounds.getWidth()
+ insets.getLeft() + insets.getRight();
}
}
}
else {
LineMetrics metrics = font.getLineMetrics("ABCxyz",
g2.getFontRenderContext());
maxWidth = metrics.getHeight()
+ insets.getTop() + insets.getBottom();
}
return maxWidth;
}
示例10: createImageFromString
import java.awt.Graphics2D; //导入方法依赖的package包/类
/**
* Creates an {@link Image} which displays the given {@link String}.
*
* @param text
* this text will be displayed in the image
* @return the image, never {@code null}
*/
private Image createImageFromString(String text) {
// to know bounds of desired text we need Graphics context so create fake one
Graphics2D g2 = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB).createGraphics();
Font font = new Font("Arial", Font.PLAIN, 24);
g2.setFont(font);
FontMetrics fm = g2.getFontMetrics();
// set intermediate width and height so we don't lose original height of background image
// while loading and/or in error case
loadingW = fm.stringWidth(text);
loadingH = fm.getHeight();
errorW = loadingW;
errorH = loadingH;
g2.dispose();
// create actual image now that text bounds are known
BufferedImage img = new BufferedImage(loadingW, loadingH, BufferedImage.TYPE_INT_ARGB);
g2 = img.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2.setFont(font);
fm = g2.getFontMetrics();
g2.setColor(Colors.TEXT_FOREGROUND);
g2.drawString(text, 0, fm.getAscent());
g2.dispose();
return img;
}
示例11: createChip
import java.awt.Graphics2D; //导入方法依赖的package包/类
/**
* 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;
}
示例12: findMaximumTickLabelWidth
import java.awt.Graphics2D; //导入方法依赖的package包/类
/**
* A utility method for determining the width of the widest tick label.
*
* @param ticks the ticks.
* @param g2 the graphics device.
* @param drawArea the area within which the plot and axes should be drawn.
* @param vertical a flag that indicates whether or not the tick labels are 'vertical'.
*
* @return the width of the tallest tick label.
*/
protected double findMaximumTickLabelWidth(List ticks,
Graphics2D g2,
Rectangle2D drawArea,
boolean vertical) {
Insets insets = getTickLabelInsets();
Font font = getTickLabelFont();
double maxWidth = 0.0;
if (!vertical) {
FontMetrics fm = g2.getFontMetrics(font);
Iterator iterator = ticks.iterator();
while (iterator.hasNext()) {
Tick tick = (Tick) iterator.next();
Rectangle2D labelBounds = TextUtilities.getTextBounds(tick.getText(), g2, fm);
if (labelBounds.getWidth() + insets.left + insets.right > maxWidth) {
maxWidth = labelBounds.getWidth() + insets.left + insets.right;
}
}
}
else {
LineMetrics metrics = font.getLineMetrics("ABCxyz", g2.getFontRenderContext());
maxWidth = metrics.getHeight() + insets.top + insets.bottom;
}
return maxWidth;
}
示例13: getLabelEnclosure
import java.awt.Graphics2D; //导入方法依赖的package包/类
/**
* Returns a rectangle that encloses the axis label. This is typically used for layout
* purposes (it gives the maximum dimensions of the label).
*
* @param g2 the graphics device.
* @param edge the edge of the plot area along which the axis is measuring.
*
* @return The enclosing rectangle.
*/
protected Rectangle2D getLabelEnclosure(Graphics2D g2, RectangleEdge edge) {
// calculate the width of the axis label...
Rectangle2D result = new Rectangle2D.Double();
String axisLabel = getLabel();
if (axisLabel != null) {
FontMetrics fm = g2.getFontMetrics(getLabelFont());
Rectangle2D bounds = TextUtilities.getTextBounds(axisLabel, g2, fm);
Insets insets = getLabelInsets();
bounds.setRect(bounds.getX(), bounds.getY(),
bounds.getWidth() + insets.left + insets.right,
bounds.getHeight() + insets.top + insets.bottom);
double angle = getLabelAngle();
if (edge == RectangleEdge.LEFT || edge == RectangleEdge.RIGHT) {
angle = angle - Math.PI / 2.0;
}
double x = bounds.getCenterX();
double y = bounds.getCenterY();
AffineTransform transformer = AffineTransform.getRotateInstance(angle, x, y);
Shape labelBounds = transformer.createTransformedShape(bounds);
result = labelBounds.getBounds2D();
}
return result;
}
示例14: reserveSpace
import java.awt.Graphics2D; //导入方法依赖的package包/类
/**
* Reserve some space on each axis side because we draw a centered label at each extremity.
*
* @param g2 the graphics device.
* @param plot the plot.
* @param plotArea the plot area.
* @param edge the edge.
* @param space the space already reserved.
*
* @return the reserved space.
*/
public AxisSpace reserveSpace(Graphics2D g2,
Plot plot,
Rectangle2D plotArea,
RectangleEdge edge,
AxisSpace space) {
this.internalMarkerCycleBoundTick = null;
AxisSpace ret = super.reserveSpace(g2, plot, plotArea, edge, space);
if (this.internalMarkerCycleBoundTick == null) {
return ret;
}
FontMetrics fm = g2.getFontMetrics(getTickLabelFont());
Rectangle2D r = TextUtilities.getTextBounds(
this.internalMarkerCycleBoundTick.getText(), g2, fm
);
if (RectangleEdge.isTopOrBottom(edge)) {
if (isVerticalTickLabels()) {
space.add(r.getHeight() / 2, RectangleEdge.RIGHT);
}
else {
space.add(r.getWidth() / 2, RectangleEdge.RIGHT);
}
}
else if (RectangleEdge.isLeftOrRight(edge)) {
if (isVerticalTickLabels()) {
space.add(r.getWidth() / 2, RectangleEdge.TOP);
}
else {
space.add(r.getHeight() / 2, RectangleEdge.TOP);
}
}
return ret;
}
示例15: findMaximumTickLabelHeight
import java.awt.Graphics2D; //导入方法依赖的package包/类
/**
* A utility method for determining the height of the tallest tick label.
*
* @param ticks the ticks.
* @param g2 the graphics device.
* @param drawArea the area within which the plot and axes should be drawn.
* @param vertical a flag that indicates whether or not the tick labels
* are 'vertical'.
*
* @return The height of the tallest tick label.
*/
protected double findMaximumTickLabelHeight(List ticks,
Graphics2D g2,
Rectangle2D drawArea,
boolean vertical) {
RectangleInsets insets = getTickLabelInsets();
Font font = getTickLabelFont();
double maxHeight = 0.0;
if (vertical) {
FontMetrics fm = g2.getFontMetrics(font);
Iterator iterator = ticks.iterator();
while (iterator.hasNext()) {
Tick tick = (Tick) iterator.next();
Rectangle2D labelBounds = TextUtilities.getTextBounds(
tick.getText(), g2, fm);
if (labelBounds.getWidth() + insets.getTop()
+ insets.getBottom() > maxHeight) {
maxHeight = labelBounds.getWidth()
+ insets.getTop() + insets.getBottom();
}
}
}
else {
LineMetrics metrics = font.getLineMetrics("ABCxyz",
g2.getFontRenderContext());
maxHeight = metrics.getHeight()
+ insets.getTop() + insets.getBottom();
}
return maxHeight;
}