本文整理汇总了Java中java.awt.FontMetrics.getHeight方法的典型用法代码示例。如果您正苦于以下问题:Java FontMetrics.getHeight方法的具体用法?Java FontMetrics.getHeight怎么用?Java FontMetrics.getHeight使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.awt.FontMetrics
的用法示例。
在下文中一共展示了FontMetrics.getHeight方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: drawCenteredText
import java.awt.FontMetrics; //导入方法依赖的package包/类
private void drawCenteredText(Graphics g, String message, boolean alignCenter) {
String[] lines = message.split("\n");
FontMetrics fm = g.getFontMetrics();
int height = lines.length * fm.getHeight();
int width = 0;
if (!alignCenter) {
for (String line : lines) {
if (fm.stringWidth(line) > width) {
width = fm.stringWidth(line);
}
}
}
for (int i = 0; i < lines.length; i++) {
g.drawString(
lines[i],
(getWidth() - (alignCenter ? fm.stringWidth(lines[i]) : width)) / 2,
(getHeight() - height) / 2 + i*fm.getHeight()
);
}
}
示例2: paint
import java.awt.FontMetrics; //导入方法依赖的package包/类
@Override
public void paint(Graphics g) {
super.paint(g);
if( underline && isEnabled() ) {
Font f = getFont();
FontMetrics fm = getFontMetrics(f);
int iconWidth = 0;
if( null != getIcon() ) {
iconWidth = getIcon().getIconWidth()+getIconTextGap();
}
int x1 = iconWidth;
int y1 = fm.getHeight();
int x2 = fm.stringWidth(getText()) + iconWidth;
if( getText().length() > 0 )
g.drawLine(x1, y1, x2, y1);
}
}
示例3: mouseReleased
import java.awt.FontMetrics; //导入方法依赖的package包/类
/**
* Called when a mouse button is released.
*/
public void mouseReleased(MouseEvent e) {
if (e.getComponent() == this
&& (e.getModifiers() & MouseEvent.BUTTON1_MASK) != 0) {
int y = e.getY();
Font font = fileWindow.textArea.getFont();
FontMetrics metrics = getFontMetrics(font);
int h = metrics.getHeight();
int line = y/h;
if (line == pressLine) {
fileWindow.toggleBreakPoint(line + 1);
} else {
pressLine = -1;
}
}
}
示例4: resizePane
import java.awt.FontMetrics; //导入方法依赖的package包/类
@SuppressWarnings("empty-statement")
private int resizePane (String text, JList list, int lastWidth) {
if (text == null) {
text = ""; //NOI18N
}
int width = summaryView.getMaster().getComponent().getWidth();
if (width > 0 && width != lastWidth) {
String[] rows = text.split("\n"); //NOI18N
FontMetrics fm = list.getFontMetrics(list.getFont());
int lines = 0;
for (String row : rows) {
Rectangle2D rect = fm.getStringBounds(row, revisionCell.getGraphics());
lines += (int) (rect.getWidth() / (width - 80) + 1);
}
int ph = fm.getHeight() * (lines + 1) + 4;
revisionCell.setPreferredSize(new Dimension(width - 50 - ICON_COLLAPSED.getIconWidth(), ph));
setPreferredSize(revisionCell.getPreferredSize());
}
return width;
}
示例5: getLargestHeight
import java.awt.FontMetrics; //导入方法依赖的package包/类
public static int getLargestHeight(Graphics g, String text, int font, int size, int wrapWidth) {
FontMetrics fm = null;
BitmapFont bmfont = null;
if (font == Theme.FONT_SMALL) {
bmfont = BitmapFont.SMALL;
} else if (font == Theme.FONT_TEXT) {
bmfont = BitmapFont.MAIN;
} else {
fm = g.getFontMetrics(MainGui.gui.getTheme().getFont(font));
}
String[] lines = text.split(TextFormatting.NEW_LINE);
int h = 0;
for (String line : lines) {
if (bmfont == null) {
if (fm.stringWidth(line) > wrapWidth)
h += fm.getHeight();
h += fm.getHeight();
} else {
if(bmfont.getStringWidth(line, size) > wrapWidth)
h += (bmfont.getCharHeight() + bmfont.getNewLineSpacing()) * size;
h += (bmfont.getCharHeight() + bmfont.getNewLineSpacing()) * size;
}
}
return h;
}
示例6: paintTransparentFrame
import java.awt.FontMetrics; //导入方法依赖的package包/类
protected void paintTransparentFrame() {
Dimension size = component.getComponent().getSize();
if (component.getComponent() instanceof JComponent) {
JComponent jc = (JComponent) component.getComponent();
jc.paintImmediately(0, 0, size.width, size.height);
}
if (disposed) {
return;
}
size = component.getSize();
Point location = component.getLocation();
graphics.setColor(BG);
graphics.fillRect(0 + location.x, 0 + location.y, size.width, size.height);
String name = component.getRComponentName();
graphics.setColor(Color.WHITE);
Font font = new Font(graphics.getFont().getName(), Font.ITALIC | Font.BOLD, 12);
FontMetrics metrics = graphics.getFontMetrics(font);
int stringWidth = metrics.stringWidth(name);
int stringHeight = metrics.getHeight() / 2;
if (stringWidth < size.width && stringHeight < size.height) {
graphics.setFont(font);
graphics.drawString(name, (size.width - stringWidth) / 2 + location.x, (size.height + stringHeight) / 2 + location.y);
} else if (stringWidth >= size.width || stringHeight >= size.height) {
graphics.setFont(new Font(graphics.getFont().getName(), Font.ITALIC, 9));
graphics.drawString(name, 0 + location.x, (size.height + stringHeight) / 2 + location.y);
} else {
logger.warning("Not drawing");
}
}
示例7: renderHtml
import java.awt.FontMetrics; //导入方法依赖的package包/类
private static void renderHtml(ClipboardHistoryElement f, Graphics g, Font defaultFont, Color defaultColor,
int width, int height, boolean selected) {
int textEnd = width - AFTER_ICON_GAP - AFTER_TEXT_GAP;
FontMetrics fm = g.getFontMetrics(defaultFont);
int textY = (height - fm.getHeight())/2 + fm.getHeight() - fm.getDescent();
HtmlRenderer.renderHTML(f.getNumber() + " " + StringEscapeUtils.escapeHtml(f.getShortenText()), g, 1, textY, textEnd, textY, //NOI18N
defaultFont, defaultColor, HtmlRenderer.STYLE_TRUNCATE, true);//, selected);
}
示例8: mousePressed
import java.awt.FontMetrics; //导入方法依赖的package包/类
/**
* Called when a mouse button is pressed.
*/
public void mousePressed(MouseEvent e) {
Font font = fileWindow.textArea.getFont();
FontMetrics metrics = getFontMetrics(font);
int h = metrics.getHeight();
pressLine = e.getY() / h;
}
示例9: drawString
import java.awt.FontMetrics; //导入方法依赖的package包/类
private void drawString(Graphics2D g2, int w, int h, boolean compressed) {
if (progressBar.isStringPainted()) {
// need to reduce font size to fit available space.
// DO NOT CALL THIS EVERY TIME AS IT'S TREMENDOUSLY EXPENSIVE!!!
if (compressed && progressBar.getFont().getSize() != 11) {
progressBar.setFont(progressBar.getFont().deriveFont(11f));
}
FontMetrics fontSizer = progressBar.getFontMetrics(progressBar.getFont());
String displayString = progressBar.getString();
if (displayString == null || displayString.trim().isEmpty()) {
return;
}
int stringHeight = fontSizer.getHeight();
int stringWidth = fontSizer.stringWidth(displayString);
// if string is too wide, cut beginning off until it fits
while (stringWidth > w * 2) {
displayString = displayString.substring(0, (int) (displayString.length() * 0.9));
stringWidth = fontSizer.stringWidth(displayString);
}
g2.setColor(Colors.TEXT_FOREGROUND);
if (compressed) {
g2.drawString(displayString, (int) Math.max(0, w / 0.33 - w - stringWidth - 5), h - (h - stringHeight) / 2
- 1);
} else {
g2.drawString(displayString, w - stringWidth, h + stringHeight - 1);
}
}
}
示例10: createImageFromString
import java.awt.FontMetrics; //导入方法依赖的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: getPreferredSize
import java.awt.FontMetrics; //导入方法依赖的package包/类
public Dimension getPreferredSize(JComponent c) {
int prefHeight = 28;
Graphics g = BasicScrollingTabDisplayerUI.getOffscreenGraphics();
if (g != null) {
FontMetrics fm = g.getFontMetrics(displayer.getFont());
Insets ins = getTabAreaInsets();
prefHeight = fm.getHeight() + ins.top + ins.bottom + (isGenericUI ? 5 : 6);
}
return new Dimension(displayer.getWidth(), prefHeight);
}
示例12: getFontImage
import java.awt.FontMetrics; //导入方法依赖的package包/类
private BufferedImage getFontImage(final char ch, final boolean antiAlias)
{
final BufferedImage tempfontImage =
new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
final Graphics2D g = (Graphics2D)tempfontImage.getGraphics();
if(antiAlias)
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
else
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
g.setFont(font);
final FontMetrics fontMetrics = g.getFontMetrics();
int charwidth = fontMetrics.charWidth(ch) + 8;
if(charwidth <= 0)
charwidth = 7;
int charheight = fontMetrics.getHeight() + 3;
if(charheight <= 0)
charheight = font.getSize();
final BufferedImage fontImage = new BufferedImage(charwidth, charheight,
BufferedImage.TYPE_INT_ARGB);
final Graphics2D gt = (Graphics2D)fontImage.getGraphics();
if(antiAlias)
gt.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
else
gt.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
gt.setFont(font);
gt.setColor(Color.WHITE);
final int charx = 3;
final int chary = 1;
gt.drawString(String.valueOf(ch), charx,
chary + fontMetrics.getAscent());
return fontImage;
}
示例13: paintBorder
import java.awt.FontMetrics; //导入方法依赖的package包/类
/**
* Paints the border for the specified component with the
* specified position and size.
* @param c the component for which this border is being painted
* @param g the paint graphics
* @param x the x position of the painted border
* @param y the y position of the painted border
* @param width the width of the painted border
* @param height the height of the painted border
*/
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
if (!(c instanceof JPopupMenu)) {
return;
}
Font origFont = g.getFont();
Color origColor = g.getColor();
JPopupMenu popup = (JPopupMenu)c;
String title = popup.getLabel();
if (title == null) {
return;
}
g.setFont(font);
FontMetrics fm = SwingUtilities2.getFontMetrics(popup, g, font);
int fontHeight = fm.getHeight();
int descent = fm.getDescent();
int ascent = fm.getAscent();
Point textLoc = new Point();
int stringWidth = SwingUtilities2.stringWidth(popup, fm,
title);
textLoc.y = y + ascent + TEXT_SPACING;
textLoc.x = x + ((width - stringWidth) / 2);
g.setColor(background);
g.fillRect(textLoc.x - TEXT_SPACING, textLoc.y - (fontHeight-descent),
stringWidth + (2 * TEXT_SPACING), fontHeight - descent);
g.setColor(foreground);
SwingUtilities2.drawString(popup, g, title, textLoc.x, textLoc.y);
MotifGraphicsUtils.drawGroove(g, x, textLoc.y + TEXT_SPACING,
width, GROOVE_HEIGHT,
shadowColor, highlightColor);
g.setFont(origFont);
g.setColor(origColor);
}
示例14: canSafelyRunPixelTests
import java.awt.FontMetrics; //导入方法依赖的package包/类
/** Determine if the graphics environment is 24 bit and has standard fonts,
* so pixel position and color tests will not erroneously fail. This method is
* thread safe.
*/
public static boolean canSafelyRunPixelTests() {
if (graphicsTestsSafe != null) {
return graphicsTestsSafe.booleanValue();
}
try {
boolean result = false;
if (GraphicsEnvironment.getLocalGraphicsEnvironment().isHeadless()) {
System.err.println("Cannot run test in a headless environment");
graphicsTestsSafe = Boolean.FALSE;
return false;
}
DisplayMode dm =
GraphicsEnvironment.getLocalGraphicsEnvironment().
getDefaultScreenDevice().getDisplayMode();
int i = dm.getBitDepth();
if (i == dm.BIT_DEPTH_MULTI || i >= 16) {
result = true;
Font f = UIManager.getFont("controlFont");
if (f == null) {
f = new JTable().getFont();
}
Graphics g = GraphicsEnvironment.getLocalGraphicsEnvironment().
getDefaultScreenDevice().getDefaultConfiguration().
createCompatibleImage(10,10).getGraphics();
FontMetrics fm = g.getFontMetrics(f);
if (fm.getHeight() != 16) {
System.err.println("Cannot run this test - default font size is not " + 16 + " pixels in height - could lead to false fails");
System.err.println("Basic font size is " + fm.getHeight());
//Some environments, such as Mandrake linux or Windows with
//large fonts will supply fonts bigger than the error icon,
//causing the pixel tests to fail due to icon positioning
//differences
result = false;
}
}
if (result) {
result = tryPrototypePixelTest();
}
return result;
} catch (Exception e) {
graphicsTestsSafe = Boolean.FALSE;
e.printStackTrace();
return false;
}
}
示例15: setFontHeightWidth
import java.awt.FontMetrics; //导入方法依赖的package包/类
private void setFontHeightWidth(Font font) {
FontMetrics metrics=getFontMetrics(font);
fontHeight=metrics.getHeight();
charWidth=metrics.charWidth('m');
}