本文整理汇总了Java中java.awt.FontMetrics.getStringBounds方法的典型用法代码示例。如果您正苦于以下问题:Java FontMetrics.getStringBounds方法的具体用法?Java FontMetrics.getStringBounds怎么用?Java FontMetrics.getStringBounds使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.awt.FontMetrics
的用法示例。
在下文中一共展示了FontMetrics.getStringBounds方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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;
}
示例2: drawTileName
import java.awt.FontMetrics; //导入方法依赖的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;
}
示例3: drawTileName
import java.awt.FontMetrics; //导入方法依赖的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;
}
示例4: drawString
import java.awt.FontMetrics; //导入方法依赖的package包/类
/**
* 绘制文字
*/
private void drawString(Point st, String str, Color c, Graphics g, int size) {
g.setFont(new Font("宋体", Font.PLAIN, size));
g.setColor(c);
FontMetrics fm = g.getFontMetrics();
Rectangle2D r2d = fm.getStringBounds(str, g);
g.drawString(str, st.x, st.y + (int) r2d.getHeight());
}
示例5: checkLinesWidth
import java.awt.FontMetrics; //导入方法依赖的package包/类
private boolean checkLinesWidth(Graphics g) {
FontMetrics fm = g.getFontMetrics(getLinesFont());
Rectangle2D rect = fm.getStringBounds(Integer.toString(linesCount), g);
linesWidth = (int) rect.getWidth() + LINES_BORDER_WIDTH * 2;
if (linesWidth != oldLinesWidth) {
oldLinesWidth = linesWidth;
revalidate();
repaint();
return true;
}
return false;
}
示例6: paintSelectedValue
import java.awt.FontMetrics; //导入方法依赖的package包/类
/**
* Draws the selected value
* @param selectedValue the selected value to draw
* @param g the graphics object
* @param width the total chart width
* @param height the total chart height
* @param maxx the maximum X value (simulation time)
* @param maxy the maximum Y value
*/
private void paintSelectedValue(MeasureValue selectedValue, Graphics g, int width, int height, double maxx, double maxy) {
FontMetrics metric = g.getFontMetrics();
String x_str = simulationTimeFormat.format(selectedValue.getSimTime());
String m_str = formatNumber(selectedValue.getMeanValue());
String i_str = formatNumber(selectedValue.getLastIntervalAvgValue());
Dimension bounds = composeVerticalBounds(g, metric, POPUP_FULL_X_PREFIX + x_str, POPUP_FULL_X_PREFIX + m_str, POPUP_FULL_X_PREFIX + i_str);
int selectedValueX = getX(selectedValue.getSimTime());
int textX = (int)(selectedValueX - bounds.getWidth() / 2);
// Fix value out of chart for label
if (textX < 2) {
textX = 2;
} else if (textX + bounds.getWidth() + POPUP_MARGIN > width) {
textX = width - (int)bounds.getWidth() - POPUP_MARGIN;
}
int textY = getY(maxy / 2) + (int)bounds.getHeight() / 2;
g.setColor(COLOR_POPUP);
g.drawLine(selectedValueX, getY(0), selectedValueX, getY(selectedValue.getLastIntervalAvgValue()));
g.setColor(COLOR_POPUP_BG);
g.fillRoundRect(textX - POPUP_MARGIN, textY - (int)bounds.getHeight(), (int)bounds.getWidth() + POPUP_MARGIN * 2, (int)bounds.getHeight() + POPUP_MARGIN, 4, 4);
g.setColor(COLOR_POPUP);
g.drawRoundRect(textX - POPUP_MARGIN, textY - (int)bounds.getHeight(), (int)bounds.getWidth() + POPUP_MARGIN * 2, (int)bounds.getHeight() + POPUP_MARGIN, 4, 4);
// Draw squares
Rectangle2D prefixBounds = metric.getStringBounds(POPUP_X_PREFIX, g);
g.setColor(COLOR_DRAW);
g.fillRect(textX, textY - (int)prefixBounds.getHeight() - bounds.height / 3, (int)prefixBounds.getWidth(), (int)prefixBounds.getHeight());
g.setColor(COLOR_LAST_INTERVAL);
g.fillRect(textX, textY - (int)prefixBounds.getHeight(), (int)prefixBounds.getWidth(), (int)prefixBounds.getHeight());
// Draws texts
g.setColor(COLOR_AXIS);
g.drawString(POPUP_FULL_X_PREFIX + x_str, textX, textY - bounds.height * 2 / 3);
g.drawString(POPUP_MIDDLE + m_str, textX + (int)prefixBounds.getWidth(), textY - bounds.height / 3);
g.drawString(POPUP_MIDDLE + i_str, textX + (int)prefixBounds.getWidth(), textY);
}
示例7: composeVerticalBounds
import java.awt.FontMetrics; //导入方法依赖的package包/类
private Dimension composeVerticalBounds(Graphics g, FontMetrics metric, String...strings) {
double xBounds = 0.0, yBounds = 0.0;
for (int i=0; i<strings.length; i++) {
Rectangle2D bounds = metric.getStringBounds(strings[i], g);
if (xBounds < bounds.getWidth()) {
xBounds = bounds.getWidth();
}
yBounds = yBounds + bounds.getHeight();
if (i > 0) {
yBounds = yBounds + POPUP_VSPACING;
}
}
return new Dimension((int)xBounds, (int)yBounds);
}
示例8: getToolTipLocation
import java.awt.FontMetrics; //导入方法依赖的package包/类
@Override
public Point getToolTipLocation(MouseEvent event) {
final String txt = getToolTipText(event);
if (txt != null) {
FontMetrics fm = this.getFontMetrics(getFont());
Rectangle2D r = fm.getStringBounds(txt, getGraphics());
return new Point(event.getX() - (int)r.getWidth()/2, -22);
}
return null;
}
示例9: generateFontImage
import java.awt.FontMetrics; //导入方法依赖的package包/类
protected BufferedImage generateFontImage(Font font, boolean antiAlias, boolean fractionalMetrics, CharData[] chars)
{
int imgSize = (int) this.imgSize;
BufferedImage bufferedImage = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
g.setFont(font);
g.setColor(new Color(255, 255, 255, 0));
g.fillRect(0, 0, imgSize, imgSize);
g.setColor(Color.WHITE);
g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, fractionalMetrics ? RenderingHints.VALUE_FRACTIONALMETRICS_ON : RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, antiAlias ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antiAlias ? RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF);
FontMetrics fontMetrics = g.getFontMetrics();
int charHeight = 0;
int positionX = 0;
int positionY = 1;
for (int i = 0; i < chars.length; i++)
{
char ch = (char) i;
CharData charData = new CharData();
Rectangle2D dimensions = fontMetrics.getStringBounds(String.valueOf(ch), g);
charData.width = (dimensions.getBounds().width + 8);
charData.height = dimensions.getBounds().height;
if (positionX + charData.width >= imgSize)
{
positionX = 0;
positionY += charHeight;
charHeight = 0;
}
if (charData.height > charHeight)
{
charHeight = charData.height;
}
charData.storedX = positionX;
charData.storedY = positionY;
if (charData.height > this.fontHeight)
{
this.fontHeight = charData.height;
}
chars[i] = charData;
g.drawString(String.valueOf(ch), positionX + 2, positionY + fontMetrics.getAscent());
positionX += charData.width;
}
return bufferedImage;
}