本文整理汇总了Java中sun.font.FontDesignMetrics.getMetrics方法的典型用法代码示例。如果您正苦于以下问题:Java FontDesignMetrics.getMetrics方法的具体用法?Java FontDesignMetrics.getMetrics怎么用?Java FontDesignMetrics.getMetrics使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sun.font.FontDesignMetrics
的用法示例。
在下文中一共展示了FontDesignMetrics.getMetrics方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getFontMetrics
import sun.font.FontDesignMetrics; //导入方法依赖的package包/类
public FontMetrics getFontMetrics() {
if (this.fontMetrics != null) {
return this.fontMetrics;
}
/* NB the constructor and the setter disallow "font" being null */
return this.fontMetrics =
FontDesignMetrics.getMetrics(font, getFontRenderContext());
}
示例2: getFontMetrics
import sun.font.FontDesignMetrics; //导入方法依赖的package包/类
public static FontMetrics getFontMetrics(JComponent c, Font font) {
FontRenderContext frc = getFRCProperty(c);
if (frc == null) {
frc = DEFAULT_FRC;
}
return FontDesignMetrics.getMetrics(font, frc);
}
示例3: getStringBounds
import sun.font.FontDesignMetrics; //导入方法依赖的package包/类
/**
* Returns the logical bounds of the specified array of characters
* in the specified <code>FontRenderContext</code>. The logical
* bounds contains the origin, ascent, advance, and height, which
* includes the leading. The logical bounds does not always enclose
* all the text. For example, in some languages and in some fonts,
* accent marks can be positioned above the ascent or below the
* descent. To obtain a visual bounding box, which encloses all the
* text, use the {@link TextLayout#getBounds() getBounds} method of
* <code>TextLayout</code>.
* <p>Note: The returned bounds is in baseline-relative coordinates
* (see {@link java.awt.Font class notes}).
* @param chars an array of characters
* @param beginIndex the initial offset in the array of
* characters
* @param limit the end offset in the array of characters
* @param frc the specified <code>FontRenderContext</code>
* @return a <code>Rectangle2D</code> that is the bounding box of the
* specified array of characters in the specified
* <code>FontRenderContext</code>.
* @throws IndexOutOfBoundsException if <code>beginIndex</code> is
* less than zero, or <code>limit</code> is greater than the
* length of <code>chars</code>, or <code>beginIndex</code>
* is greater than <code>limit</code>.
* @see FontRenderContext
* @see Font#createGlyphVector
* @since 1.2
*/
public Rectangle2D getStringBounds(char [] chars,
int beginIndex, int limit,
FontRenderContext frc) {
if (beginIndex < 0) {
throw new IndexOutOfBoundsException("beginIndex: " + beginIndex);
}
if (limit > chars.length) {
throw new IndexOutOfBoundsException("limit: " + limit);
}
if (beginIndex > limit) {
throw new IndexOutOfBoundsException("range length: " +
(limit - beginIndex));
}
// this code should be in textlayout
// quick check for simple text, assume GV ok to use if simple
boolean simple = values == null ||
(values.getKerning() == 0 && values.getLigatures() == 0 &&
values.getBaselineTransform() == null);
if (simple) {
simple = ! FontUtilities.isComplexText(chars, beginIndex, limit);
}
if (simple) {
FontDesignMetrics metrics = FontDesignMetrics.getMetrics(this, frc);
return metrics.getSimpleBounds(chars, beginIndex, limit-beginIndex);
} else {
// need char array constructor on textlayout
String str = new String(chars, beginIndex, limit - beginIndex);
TextLayout tl = new TextLayout(str, this, frc);
return new Rectangle2D.Float(0, -tl.getAscent(), tl.getAdvance(),
tl.getAscent() + tl.getDescent() +
tl.getLeading());
}
}
示例4: size
import sun.font.FontDesignMetrics; //导入方法依赖的package包/类
@Override
public Dimension size(Component component) {
if (!(component instanceof Text))
throw new IllegalArgumentException("Text layout must only be used on texts.");
int width = 0;
int height = 0;
Text text = (Text) component;
Insets insets = text.getPadding();
if (insets != null) {
width += insets.left + insets.right;
height += insets.top + insets.bottom;
}
DropShadow shadow = text.getDropShadow();
if (shadow != null) {
width += shadow.getOffsetX();
height += shadow.getOffsetY();
}
Font font = text.getFont();
if (font != null) {
String string = text.getText();
if (string != null && string.length() > 0) {
FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
if (metrics != null) {
Graphics graphics = null;
BufferedImage cache = text.getCachedImage();
if (cache != null)
graphics = cache.getGraphics();
Rectangle2D rect = metrics.getStringBounds(string, graphics);
width += rect.getWidth();
height += rect.getHeight();
}
}
}
return new Dimension(width, height);
}
示例5: getFontMetrics
import sun.font.FontDesignMetrics; //导入方法依赖的package包/类
@SuppressWarnings("deprecation")
public FontMetrics getFontMetrics(Font font) {
return FontDesignMetrics.getMetrics(font);
}
示例6: getFontMetrics
import sun.font.FontDesignMetrics; //导入方法依赖的package包/类
@Override
@SuppressWarnings("deprecation")
public FontMetrics getFontMetrics(Font font) {
return FontDesignMetrics.getMetrics(font);
}
示例7: paintComponent
import sun.font.FontDesignMetrics; //导入方法依赖的package包/类
@Override
public void paintComponent(Component component, Graphics graphics, int x, int y, int width, int height) {
super.paintComponent(component, graphics, x, y, width, height);
if (!(component instanceof Text))
throw new IllegalArgumentException("Text image builders must only be used on texts.");
if (width == 0 || height == 0)
return;
Text text = (Text) component;
Font font = text.getFont();
if (font == null)
return;
String string = text.getText();
if (string == null || string.length() == 0)
return;
Color color = text.getParent() instanceof Button ? (text.isPressed() ? text.getPressedColor() : text.isHovered() ? text.getRolloverColor() : text.getColor()) : text.getColor();
if (color == null)
return;
int tw = text.getWidth();
int th = text.getHeight();
int dx = 0;
int dy = 0;
int dw = 0;
int dh = 0;
int dhh = 0;
Insets insets = text.getPadding();
if (insets != null) {
dx += insets.left;
dy += insets.top;
tw -= insets.left + insets.right;
th -= insets.top + insets.bottom;
}
FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
if (metrics != null) {
Rectangle2D rect = metrics.getStringBounds(string, graphics);
dw += rect.getWidth();
dh += rect.getHeight();
dhh += metrics.getAscent() - 1;
}
Set<Position> picturePosition = text.getPosition();
if (picturePosition.contains(Position.LEFT)) {
dx += 0;
} else if (picturePosition.contains(Position.RIGHT)) {
dx += tw - dw;
} else if (picturePosition.contains(Position.CENTER)) {
dx += (int) Math.round((tw - dw) / 2.0d);
}
if (picturePosition.contains(Position.TOP)) {
dy += 0;
} else if (picturePosition.contains(Position.BOTTOM)) {
dy += th - dh;
} else if (picturePosition.contains(Position.CENTER)) {
dy += (int) Math.round((th - dh) / 2.0d);
}
if (!new Rectangle(x, y, width, height).contains(new Rectangle(dx, dy, dw, dh)))
return;
Color oldColor = graphics.getColor();
Font oldFont = graphics.getFont();
graphics.setFont(font);
Object aliasHint = ((Graphics2D) graphics).getRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING);
DropShadow shadow = text.getDropShadow();
if (shadow != null) {
Color shadowColor = shadow.getColor();
if (shadowColor != null) {
graphics.setColor(shadowColor);
graphics.drawString(string, dx + shadow.getOffsetX(), dy + dhh + shadow.getOffsetY());
}
}
((Graphics2D) graphics).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphics.setColor(color);
graphics.drawString(string, dx, dy + dhh);
((Graphics2D) graphics).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, aliasHint);
graphics.setColor(oldColor);
graphics.setFont(oldFont);
}
示例8: getFontMetrics
import sun.font.FontDesignMetrics; //导入方法依赖的package包/类
public FontMetrics getFontMetrics(Font font) {
return FontDesignMetrics.getMetrics(font);
}
示例9: createSVGGridLabels
import sun.font.FontDesignMetrics; //导入方法依赖的package包/类
/**
* Creates the SVG element corresponding to the labels of the grid.
*/
private void createSVGGridLabels(final SVGDocument document, final SVGElement elt, final String prefix, final double minX,
final double maxX, final double minY, final double maxY, final double tlx, final double tly,
final double xStep, final double yStep, final double gridWidth, final double absStep) {
final int gridLabelsSize = shape.getLabelsSize();
final boolean isXLabelSouth = shape.isXLabelSouth();
final boolean isYLabelWest = shape.isYLabelWest();
final double originX = shape.getOriginX();
final double originY = shape.getOriginY();
final Color gridLabelsColor = shape.getGridLabelsColour();
final FontMetrics fontMetrics = FontDesignMetrics.getMetrics(new Font(null, Font.PLAIN, shape.getLabelsSize()));
final float labelHeight = fontMetrics.getAscent();
final float labelWidth = fontMetrics.stringWidth(String.valueOf((int)maxX));
final double xorigin = xStep*originX;
final double yorigin = isXLabelSouth ? yStep*originY+labelHeight : yStep*originY-2;
final double width=gridWidth/2., tmp = isXLabelSouth ? width : -width;
final SVGElement texts = new SVGGElement(document);
SVGElement text;
String label;
double i, j;
texts.setAttribute(SVGAttributes.SVG_FONT_SIZE, String.valueOf(shape.getLabelsSize()));
texts.setAttribute(SVGAttributes.SVG_STROKE, CSSColors.INSTANCE.getColorName(gridLabelsColor, true));
texts.setAttribute(prefix+LNamespace.XML_TYPE, LNamespace.XML_TYPE_TEXT);
for(i=tlx + (isYLabelWest ? width+gridLabelsSize/4. : -width-labelWidth-gridLabelsSize/4.), j=minX; j<=maxX; i+=absStep, j++) {
text = new SVGTextElement(document);
text.setAttribute(SVGAttributes.SVG_X, String.valueOf((int)i));
text.setAttribute(SVGAttributes.SVG_Y, String.valueOf((int)(yorigin+tmp)));
text.setTextContent(String.valueOf((int)j));
texts.appendChild(text);
}
if(isYLabelWest)
for(i=tly + (isXLabelSouth ? -width-gridLabelsSize/4. : width+labelHeight), j=maxY ; j>=minY; i+=absStep, j--) {
label = String.valueOf((int)j);
text = new SVGTextElement(document);
text.setAttribute(SVGAttributes.SVG_X, String.valueOf((int)(xorigin-fontMetrics.stringWidth(label)-gridLabelsSize/4.-width)));
text.setAttribute(SVGAttributes.SVG_Y, String.valueOf((int)i));
text.setTextContent(label);
texts.appendChild(text);
}
else
for(i=tly + (isXLabelSouth ? -width-gridLabelsSize/4. : width+labelHeight), j=maxY; j>=minY; i+=absStep, j--) {
label = String.valueOf((int)j);
text = new SVGTextElement(document);
text.setAttribute(SVGAttributes.SVG_X, String.valueOf((int)(xorigin+gridLabelsSize/4.+width)));
text.setAttribute(SVGAttributes.SVG_Y, String.valueOf((int)i));
text.setTextContent(label);
texts.appendChild(text);
}
elt.appendChild(texts);
}
示例10: updateFonts
import sun.font.FontDesignMetrics; //导入方法依赖的package包/类
/**
* Updates the font and the fontMetrics.
*/
protected void updateFonts() {
fontMetrics = FontDesignMetrics.getMetrics(new Font(null, Font.PLAIN, shape.getLabelsSize()));
}