本文整理汇总了Java中java.awt.Graphics.getFontMetrics方法的典型用法代码示例。如果您正苦于以下问题:Java Graphics.getFontMetrics方法的具体用法?Java Graphics.getFontMetrics怎么用?Java Graphics.getFontMetrics使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.awt.Graphics
的用法示例。
在下文中一共展示了Graphics.getFontMetrics方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: drawString
import java.awt.Graphics; //导入方法依赖的package包/类
public void drawString(Graphics g, String string, int x, int y, int width) {
FontMetrics fontMetrics = g.getFontMetrics();
int lineHeight = fontMetrics.getHeight();
int curX = x;
int curY = y;
String[] words = string.split(" ");
for (String word : words) {
// Find out thw width of the word.
int wordWidth = fontMetrics.stringWidth(word + " ");
// If text exceeds the width, then move to next line.
if (curX + wordWidth >= x + width) {
curY += lineHeight;
curX = x;
}
g.drawString(word, curX, curY);
// Move over to the right for next word.
curX += wordWidth;
}
}
示例2: computePreferredSize
import java.awt.Graphics; //导入方法依赖的package包/类
private void computePreferredSize() {
int inputs = table.getInputColumnCount();
int outputs = table.getOutputColumnCount();
if (inputs == 0 && outputs == 0) {
setPreferredSize(new Dimension(0, 0));
return;
}
Graphics g = getGraphics();
if (g == null) {
cellHeight = 16;
cellWidth = 24;
} else {
FontMetrics fm = g.getFontMetrics(HEAD_FONT);
cellHeight = fm.getHeight();
cellWidth = 24;
if (inputs == 0 || outputs == 0) {
cellWidth = Math.max(cellWidth, fm.stringWidth(Strings.get("tableNullHeader")));
}
for (int i = 0; i < inputs + outputs; i++) {
String header = i < inputs ? table.getInputHeader(i) : table.getOutputHeader(i - inputs);
cellWidth = Math.max(cellWidth, fm.stringWidth(header));
}
}
if (inputs == 0)
inputs = 1;
if (outputs == 0)
outputs = 1;
tableWidth = (cellWidth + COLUMN_SEP) * (inputs + outputs) - COLUMN_SEP;
tableHeight = cellHeight * (1 + table.getRowCount()) + HEADER_SEP;
setPreferredSize(new Dimension(tableWidth, tableHeight));
revalidate();
repaint();
}
示例3: getWidth
import java.awt.Graphics; //导入方法依赖的package包/类
public int getWidth() {
if (shortName == null || shortName.length() <= 1) {
return Figure.SLOT_WIDTH;
} else {
BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setFont(figure.getDiagram().getSlotFont().deriveFont(Font.BOLD));
FontMetrics metrics = g.getFontMetrics();
return Math.max(Figure.SLOT_WIDTH, metrics.stringWidth(shortName) + 6);
}
}
示例4: setExpression
import java.awt.Graphics; //导入方法依赖的package包/类
public void setExpression(Expression expr) {
ExpressionData exprData = new ExpressionData(expr);
Graphics g = getGraphics();
FontMetrics fm = g == null ? null : g.getFontMetrics();
renderData = new RenderData(exprData, getWidth(), fm);
setPreferredSize(renderData.getPreferredSize());
revalidate();
repaint();
}
示例5: paintText
import java.awt.Graphics; //导入方法依赖的package包/类
/**
* Renders the text.
*/
private void paintText(Graphics g, int w, int h) {
g.setFont(getFont());
String text = getHeapSizeText();
FontMetrics fm = g.getFontMetrics();
int textWidth = fm.stringWidth(text);
g.drawString(text, (w - maxTextWidth) / 2 + (maxTextWidth - textWidth),
h / 2 + fm.getAscent() / 2 - 2);
}
示例6: trialDrawY
import java.awt.Graphics; //导入方法依赖的package包/类
/**
* probne rysowanie - policznie wymiarow w dół
*
* @param g - kontekst graficzny
*/
public void trialDrawY(Graphics g) {
if (itemsToDraw == null) {
return;
}
// policzenie wymiarow
FontMetrics metrics = g.getFontMetrics();
fontHeight = metrics.getHeight();
int pos = fontHeight;
int length = 0;
for (String text : itemsToDraw) {
length = Math.max(length, metrics.stringWidth(text));
pos += fontHeight;
}
dimension.width = length + 4;
dimension.height = pos - fontHeight + 4;
// policzenie obrysu z dziecmi
bound.width = 0;
bound.height = 0;
for (NodeDrawer node : nodes) {
node.trialDrawY(g);
bound.width += node.getBound().width + X_DISTANCE;
bound.height = Math.max(node.getBound().height, bound.height);
}
bound.width = Math.max(dimension.width, bound.width - X_DISTANCE);
bound.height = dimension.height + Y_DISTANCE + bound.height;
// korekta na odstep
if (nodes.isEmpty()) {
bound.height -= Y_DISTANCE;
}
}
示例7: createPullButton
import java.awt.Graphics; //导入方法依赖的package包/类
/**
* Create "Pull" button.
*
* @return the "Pull" button.
*/
private ToolbarButton createPullButton() {
return new ToolbarButton(createPullAction(), false) {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
String str = "";
if (pullsBehind > 0) {
str = "" + pullsBehind;
}
if (pullsBehind > 9) {
pullButton.setHorizontalAlignment(SwingConstants.LEFT);
} else {
pullButton.setHorizontalAlignment(SwingConstants.CENTER);
}
g.setFont(g.getFont().deriveFont(Font.BOLD, 8.5f));
FontMetrics fontMetrics = g.getFontMetrics(g.getFont());
int stringWidth = fontMetrics.stringWidth(str);
int stringHeight = fontMetrics.getHeight();
g.setColor(new Color(255, 255, 255, 100));
g.fillRect(pullButton.getWidth() - stringWidth, 0, stringWidth, stringHeight);
g.setColor(Color.BLACK);
g.drawString(str, pullButton.getWidth() - stringWidth,
fontMetrics.getHeight() - fontMetrics.getDescent() - fontMetrics.getLeading());
}
};
}
示例8: getPreferredSize
import java.awt.Graphics; //导入方法依赖的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);
}
示例9: getFontMetrics
import java.awt.Graphics; //导入方法依赖的package包/类
/** Get the font-metrics for the given font.
* @param font font for which the metrics is being retrieved.
* @param g graphics that is used to retrieve the metrics in case it's
* not yet in the cache.
*/
public static synchronized FontMetrics getFontMetrics(Font f, Graphics g) {
Object fm = font2FM.get(f);
if (fm == null) {
fm = g.getFontMetrics(f);
font2FM.put(f, fm);
}
return (FontMetrics)fm;
}
示例10: getTextBounds
import java.awt.Graphics; //导入方法依赖的package包/类
static public Rectangle getTextBounds(Graphics g, String text, int x, int y, int halign, int valign) {
if (g == null)
return new Rectangle(x, y, 0, 0);
FontMetrics mets = g.getFontMetrics();
int width = mets.stringWidth(text);
int ascent = mets.getAscent();
int descent = mets.getDescent();
int height = ascent + descent;
Rectangle ret = new Rectangle(x, y, width, height);
switch (halign) {
case H_CENTER:
ret.translate(-(width / 2), 0);
break;
case H_RIGHT:
ret.translate(-width, 0);
break;
default:
;
}
switch (valign) {
case V_TOP:
break;
case V_CENTER:
ret.translate(0, -(ascent / 2));
break;
case V_CENTER_OVERALL:
ret.translate(0, -(height / 2));
break;
case V_BASELINE:
ret.translate(0, -ascent);
break;
case V_BOTTOM:
ret.translate(0, -height);
break;
default:
;
}
return ret;
}
示例11: paint
import java.awt.Graphics; //导入方法依赖的package包/类
@Override
public void paint(Graphics g) {
int fontSize = g.getFont().getSize();
int x = 0, y = fontSize, space;
int red = (int) (50 * Math.random());
int green = (int) (50 * Math.random());
int blue = (int) (256 * Math.random());
Dimension d = getSize();
g.setColor(Color.black);
FontMetrics fm = g.getFontMetrics();
space = fm.stringWidth(" ");
for (StringTokenizer t = new StringTokenizer(labelString);
t.hasMoreTokens();) {
String word = t.nextToken();
int w = fm.stringWidth(word) + space;
if (x + w > d.width) {
x = 0;
y += fontSize; //move word to next line if it doesn't fit
}
if (Math.random() < 0.5) {
g.setColor(new java.awt.Color((red + y * 30) % 256,
(green + x / 3) % 256, blue));
} else {
g.setColor(getBackground());
}
g.drawString(word, x, y);
x += w; //shift to the right to draw the next word
}
}
示例12: calcRowHeight
import java.awt.Graphics; //导入方法依赖的package包/类
/**
* Calculate the height of rows based on the current font. This is done
* when the first paint occurs, to ensure that a valid Graphics object is
* available.
*
* @since 1.25
*/
private void calcRowHeight(Graphics g) {
Font f = getFont();
FontMetrics fm = g.getFontMetrics(f);
// As icons are displayed use maximum from font and icon height
int rowHeight = Math.max(fm.getHeight(), 16) + 4;
needCalcRowHeight = false;
setRowHeight(rowHeight);
}
示例13: paint
import java.awt.Graphics; //导入方法依赖的package包/类
@Override public void paint(Graphics graphics) {
graphics.setColor(Color.BLACK);
graphics.setFont(graphics.getFont().deriveFont(8f));
int top = size.height/4, height=graphics.getFontMetrics().getAscent()/2;
graphics.drawRect(5, 0, size.width-10, size.height);
graphics.drawString("A", 10, top+height);
graphics.drawString("B", 10, top*3+height);
graphics.drawString("S", size.width-15, top+height);
graphics.drawString("C", size.width-15, top*3+height);
graphics.setFont(graphics.getFont().deriveFont(Font.BOLD, 15f));
FontMetrics metrics = graphics.getFontMetrics();
graphics.drawString("HA", size.width/2-metrics.stringWidth("HA")/2,
size.height/2+metrics.getAscent()/3);
ContactUtilities.paintSolderingJoints(graphics, contacts);
}
示例14: checkLinesWidth
import java.awt.Graphics; //导入方法依赖的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;
}
示例15: getNumberWidthY
import java.awt.Graphics; //导入方法依赖的package包/类
/**
* Calculates max number width for y-axis numeration.
*
* @param g
* The {@link Graphics} object.
* @return Maximum number width.
*/
private int getNumberWidthY(Graphics g) {
FontMetrics fm = g.getFontMetrics();
int dimension = 0;
int yValueStart = chart.getyMin();
int yValueEnd = chart.getyMax();
int yValue = Math.max(Math.abs(yValueEnd), Math.abs(yValueStart));
int width = fm.stringWidth(Integer.toString(yValue));
dimension = Math.max(width, dimension);
return dimension;
}