本文整理汇总了Java中java.awt.FontMetrics类的典型用法代码示例。如果您正苦于以下问题:Java FontMetrics类的具体用法?Java FontMetrics怎么用?Java FontMetrics使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
FontMetrics类属于java.awt包,在下文中一共展示了FontMetrics类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: paintComponent
import java.awt.FontMetrics; //导入依赖的package包/类
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setFont(new Font("SansSerif", Font.BOLD, 12));
FontMetrics metrics = g.getFontMetrics();
g.drawString("Current font: " + g.getFont(), 10, 30);
g.drawString("Ascent: " + metrics.getAscent(), 10, 45);
g.drawString("Descent: " + metrics.getDescent(), 10, 60);
g.drawString("Height: " + metrics.getHeight(), 10, 75);
g.drawString("Leading: " + metrics.getLeading(), 10, 90);
Font font = new Font("Serif", Font.ITALIC, 14);
metrics = g.getFontMetrics(font);
g.setFont(font);
g.drawString("Current font: " + font, 10, 120);
g.drawString("Ascent: " + metrics.getAscent(), 10, 135);
g.drawString("Descent: " + metrics.getDescent(), 10, 150);
g.drawString("Height: " + metrics.getHeight(), 10, 165);
g.drawString("Leading: " + metrics.getLeading(), 10, 180);
}
示例2: layout
import java.awt.FontMetrics; //导入依赖的package包/类
private String layout(JLabel label, FontMetrics fm,
int width, int height) {
Insets insets = label.getInsets(null);
String text = label.getText();
Icon icon = (label.isEnabled()) ? label.getIcon() :
label.getDisabledIcon();
Rectangle paintViewR = new Rectangle();
paintViewR.x = insets.left;
paintViewR.y = insets.top;
paintViewR.width = width - (insets.left + insets.right);
paintViewR.height = height - (insets.top + insets.bottom);
paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;
return layoutCL(label, fm, text, icon, paintViewR, paintIconR,
paintTextR);
}
示例3: checkNullArgumentsGetStringWidth
import java.awt.FontMetrics; //导入依赖的package包/类
private static void checkNullArgumentsGetStringWidth(JComponent comp,
String text) {
FontMetrics fontMetrics = comp.getFontMetrics(comp.getFont());
BasicGraphicsUtils.getStringWidth(null, fontMetrics, text);
float result = BasicGraphicsUtils.getStringWidth(comp, fontMetrics, null);
if (result != 0) {
throw new RuntimeException("The string length is not 0");
}
try {
BasicGraphicsUtils.getStringWidth(comp, null, text);
} catch (NullPointerException e) {
return;
}
throw new RuntimeException("NPE is not thrown");
}
示例4: renderHtml
import java.awt.FontMetrics; //导入依赖的package包/类
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);
}
}
示例5: leak
import java.awt.FontMetrics; //导入依赖的package包/类
private static void leak() {
Map<TextAttribute, Object> textAttributes = new HashMap<>();
textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
textAttributes.put(TextAttribute.SIZE, 12);
textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
Font font = Font.getFont(textAttributes);
JLabel label = new JLabel();
int dummy = 0;
for (int i = 0; i < 500; i++) {
if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
for (int j = 0; j <1000; j++) {
FontMetrics fm = label.getFontMetrics(font);
dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
}
}
System.out.println("done " + dummy);
}
示例6: renderChar
import java.awt.FontMetrics; //导入依赖的package包/类
/**
* Performs binary dilation.
*/
public void renderChar(FontMetrics fontMetrics, BufferedImage image, char c, int safetyMargin) {
BufferedImage copy = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
copy.getGraphics().drawImage(image, 0, 0, null);
Graphics2D g = (Graphics2D) image.getGraphics();
g.setColor(maskColor);
for(int x=maskSize/2; x < image.getWidth()-maskSize/2; x++) {
for(int y=maskSize/2; y < image.getHeight()-maskSize/2; y++) {
if(PixelReplacer.INT_ARGBhasColor(copy.getRGB(x, y))) {
for(int j=0;j<maskSize;j++) {
for(int k=0;k<maskSize;k++) {
g.drawLine(x+j-maskSize/2, y+k-maskSize/2, x+j-maskSize/2, y+k-maskSize/2);
}
}
}
}
}
}
示例7: paintTitle
import java.awt.FontMetrics; //导入依赖的package包/类
protected void paintTitle(final Graphics2D g2d, final Font font, final FontMetrics metrics, final Rectangle textRect, final int tabIndex, final String title) {
final View v = getTextViewForTab(tabIndex);
if (v != null) {
v.paint(g2d, textRect);
return;
}
if (title == null) return;
final Color color = tabPane.getForegroundAt(tabIndex);
if (color instanceof UIResource) {
g2d.setColor(getNonSelectedTabTitleColor());
if (tabPane.getSelectedIndex() == tabIndex) {
boolean pressed = isPressedAt(tabIndex);
boolean enabled = tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex);
Color textColor = getSelectedTabTitleColor(enabled, pressed);
Color shadowColor = getSelectedTabTitleShadowColor(enabled);
AquaUtils.paintDropShadowText(g2d, tabPane, font, metrics, textRect.x, textRect.y, 0, 1, textColor, shadowColor, title);
return;
}
} else {
g2d.setColor(color);
}
g2d.setFont(font);
SwingUtilities2.drawString(tabPane, g2d, title, textRect.x, textRect.y + metrics.getAscent());
}
示例8: paint
import java.awt.FontMetrics; //导入依赖的package包/类
@Override
public void paint(Graphics g1)
{
Graphics2D g = (Graphics2D)g1;
Dimension size = getSize();
g.setColor(getBackground());
g.fillRect(0, 0, size.width, size.height);
g.setColor(new Color(40,40,40));
//FontMetrics tm = g.getFontMetrics(topFont);
FontMetrics bm = g.getFontMetrics(bottomFont);
if (topLine != null)
{
g.setFont(topFont);
g.drawString(topLine, 5, size.height/2 - 2);
}
if (bottomLine != null)
{
g.setFont(bottomFont);
g.drawString(bottomLine, 5, size.height/2 + bm.getHeight() - 2);
}
}
示例9: drawTextImpl
import java.awt.FontMetrics; //导入依赖的package包/类
/**
* The implementation of
* {@link #drawText(Vec, String, TextOptions, boolean)}.
*
* @param pos
* same as in base method
* @param text
* same as in base method
* @param options
* same as in base method
*/
private void drawTextImpl(Vec pos, String text, TextOptions options) {
// Set color to red and set font
RGBAColor in = options.getColor();
RGBAColor col = (!options.getUseFilter() || this.filter == null || !this.filter.isApplyPixel()) ? in : this.filter.apply(in);
this.graphics.setColor(this.calcRGBA(col));
Font font = new Font(options.getFont(), options.getFontOptions(), options.getHeight());
this.graphics.setFont(font);
FontMetrics metrics = this.graphics.getFontMetrics(font);
float x = pos.x;
float y = pos.y;
float xAlign = options.getAlignment().x;
float yAlign = options.getAlignment().y;
for (String line : text.split("\n")) {
Dimension offset = this.getTextOffset(line, metrics);
this.graphics.drawString(line, //
(x + xAlign * offset.width), //
(y += metrics.getHeight()) + yAlign * offset.height);
}
}
示例10: drawStringInRect
import java.awt.FontMetrics; //导入依赖的package包/类
/**
* A utility method that draws a string inside a rectangle.
*
* @param g2 the graphics device.
* @param bounds the rectangle.
* @param font the font.
* @param text the text.
*/
private void drawStringInRect(Graphics2D g2, Rectangle2D bounds, Font font,
String text) {
g2.setFont(font);
FontMetrics fm = g2.getFontMetrics(font);
Rectangle2D r = TextUtilities.getTextBounds(text, g2, fm);
double x = bounds.getX();
if (r.getWidth() < bounds.getWidth()) {
x = x + (bounds.getWidth() - r.getWidth()) / 2;
}
LineMetrics metrics = font.getLineMetrics(
text, g2.getFontRenderContext()
);
g2.drawString(
text, (float) x, (float) (bounds.getMaxY()
- this.bottomInnerGap - metrics.getDescent())
);
}
示例11: computeFitText
import java.awt.FontMetrics; //导入依赖的package包/类
private static String computeFitText(JLabel label) {
String text = label.getText();
if(text == null) text = "";
if (text.length() <= VISIBLE_START_CHARS + 3) return text;
Icon icon = label.getIcon();
int iconWidth = icon != null ? icon.getIconWidth() : 0;
FontMetrics fm = label.getFontMetrics(label.getFont());
int width = label.getSize().width - iconWidth;
String sufix = "..."; // NOI18N
int sufixLength = fm.stringWidth(sufix);
int desired = width - sufixLength;
if (desired <= 0) return text;
for (int i = 0; i <= text.length() - 1; i++) {
String prefix = text.substring(0, i);
int swidth = fm.stringWidth(prefix);
if (swidth >= desired) {
return prefix.length() > 0 ? prefix + sufix: text;
}
}
return text;
}
示例12: testStringWidth
import java.awt.FontMetrics; //导入依赖的package包/类
private static void testStringWidth() {
String str = "12345678910\u036F";
JComponent comp = createComponent(str);
Font font = comp.getFont();
FontMetrics fontMetrics = comp.getFontMetrics(font);
float stringWidth = BasicGraphicsUtils.getStringWidth(comp, fontMetrics, str);
if (stringWidth == fontMetrics.stringWidth(str)) {
throw new RuntimeException("Numeric shaper is not used!");
}
if (stringWidth != getLayoutWidth(str, font, NUMERIC_SHAPER)) {
throw new RuntimeException("Wrong text width!");
}
}
示例13: paintValue
import java.awt.FontMetrics; //导入依赖的package包/类
/** Paints the current value. Implements <code>ProepertyEditor</code> interface. */
public void paintValue(Graphics g, Rectangle rectangle) {
int px;
((Graphics2D)g).setRenderingHints (getHints ());
if (this.superColor != null) {
Color color = g.getColor();
g.drawRect(rectangle.x, rectangle.y + rectangle.height / 2 - 5 , 10, 10);
g.setColor(this.superColor);
g.fillRect(rectangle.x + 1, rectangle.y + rectangle.height / 2 - 4 , 9, 9);
g.setColor(color);
px = 18;
}
else px = 0;
FontMetrics fm = g.getFontMetrics();
g.drawString(getAsText(), rectangle.x + px, rectangle.y +
(rectangle.height - fm.getHeight()) / 2 + fm.getAscent());
}
示例14: getTableCellRendererComponent
import java.awt.FontMetrics; //导入依赖的package包/类
@Override
public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column ) {
super.getTableCellRendererComponent( table, value, isSelected, hasFocus, row, column );
int availableWidth = table.getColumnModel().getColumn( column ).getWidth();
availableWidth -= table.getIntercellSpacing().getWidth();
Insets borderInsets = getBorder().getBorderInsets( (Component)this );
availableWidth -= (borderInsets.left + borderInsets.right);
String cellText = getText();
FontMetrics fm = getFontMetrics( getFont() );
if( fm.stringWidth(cellText) > availableWidth ) {
setToolTipText( cellText );
} else {
setToolTipText( null );
}
return this;
}
示例15: paintText
import java.awt.FontMetrics; //导入依赖的package包/类
@Override
protected void paintText(Graphics g, int tabPlacement, Font font, FontMetrics metrics, int tabIndex, String title,
Rectangle textRect, boolean isSelected)
{
g.setFont(font);
View v = getTextViewForTab(tabIndex);
if( v != null )
{
// html
v.paint(g, textRect);
}
else
{
// plain text
int mnemIndex = tabPane.getDisplayedMnemonicIndexAt(tabIndex);
if( tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex) )
{
g.setColor(isSelected ? Color.white : Color.black);
BasicGraphicsUtils.drawStringUnderlineCharAt(g, title, mnemIndex, textRect.x,
textRect.y + metrics.getAscent());
}
else
{ // tab disabled
g.setColor(tabPane.getBackgroundAt(tabIndex).brighter());
BasicGraphicsUtils.drawStringUnderlineCharAt(g, title, mnemIndex, textRect.x,
textRect.y + metrics.getAscent());
g.setColor(tabPane.getBackgroundAt(tabIndex).darker());
BasicGraphicsUtils.drawStringUnderlineCharAt(g, title, mnemIndex, textRect.x,
textRect.y + metrics.getAscent());
}
}
}