本文整理汇总了Java中java.awt.Graphics.getFont方法的典型用法代码示例。如果您正苦于以下问题:Java Graphics.getFont方法的具体用法?Java Graphics.getFont怎么用?Java Graphics.getFont使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.awt.Graphics
的用法示例。
在下文中一共展示了Graphics.getFont方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: drawCircledText
import java.awt.Graphics; //导入方法依赖的package包/类
private void drawCircledText(Graphics g, String text, int centreX, int topY)
{
// Make some coordinate calculations
Rectangle2D textSize = g.getFontMetrics().getStringBounds(text, g);
int halfDiamondY = OVAL_HEIGHT / 2;
int textStartX = centreX - (int) (textSize.getWidth() / 2);
int textStartY = topY + halfDiamondY + (int) (textSize.getHeight() / 2);
int ovalStartX = textStartX - PADDING_SIZE;
int ovalWidth = (int) textSize.getWidth() + (PADDING_SIZE * 2);
// Draw the oval around the text
g.setColor(Color.BLACK);
g.fillOval(ovalStartX, topY, ovalWidth, OVAL_HEIGHT);
// Draw our text
Font backup = g.getFont();
g.setFont(new Font(backup.getName(), Font.BOLD, backup.getSize()));
g.setColor(Color.WHITE);
g.drawString(text, textStartX - 3, textStartY - 3);
g.setFont(backup);
}
示例2: _resetFont
import java.awt.Graphics; //导入方法依赖的package包/类
private static void _resetFont(Graphics g)
{
Font font = g.getFont();
// We set the font to be the same font family/style as
// the current font, but a different size (1pt).
g.setFont(new Font(font.getName(), font.getStyle(), 1));
// Then, get the font metrics, this seems to reset the
// graphics state.
g.getFontMetrics();
// Finally, reset the original font. Now, if we get the
// font metrics again or paint some text, the right font/
// font metrics will be used.
g.setFont(font);
}
示例3: printNumbersY
import java.awt.Graphics; //导入方法依赖的package包/类
/**
* Draws numbers on y-axis.
*
* @param g
* The {@link Graphics} object.
*/
private void printNumbersY(Graphics g) {
FontMetrics fm = g.getFontMetrics();
g.setColor(Color.black);
Font currentFont = g.getFont();
g.setFont(new Font(currentFont.toString(), Font.BOLD, currentFont
.getSize()));
for (int i = 0; i < hLines; i++) {
String text = Integer.toString(chart.getyMin() + i
* chart.getyStep());
g.drawString(text, xPoint - fm.stringWidth(text) - SPACE,
rect.height - yPoint - i * hStep + (textHeight / 4));
}
g.setFont(currentFont);
}
示例4: printNumbersX
import java.awt.Graphics; //导入方法依赖的package包/类
/**
* Draws numbers on y-axis.
*
* @param g
* The {@link Graphics} object.
*/
private void printNumbersX(Graphics g) {
g.setColor(Color.black);
Font currentFont = g.getFont();
g.setFont(new Font(currentFont.toString(), Font.BOLD, currentFont
.getSize()));
for (int i = 0; i < vLines; i++) {
XYValue value = chart.getValues().get(i);
String text = Integer.toString(value.getX());
int x = xPoint + vStep / 2 - getTextWidth(text, g) / 2;
g.drawString(text, x + i * vStep, rect.height - yPoint + SPACE * 2);
}
g.setFont(currentFont);
}
示例5: paintSyntaxLine
import java.awt.Graphics; //导入方法依赖的package包/类
/**
* Paints the specified line onto the graphics context. Note that this method munges the offset
* and count values of the segment.
*
* @param line
* The line segment
* @param tokens
* The token list for the line
* @param styles
* The syntax style list
* @param expander
* The tab expander used to determine tab stops. May be null
* @param gfx
* The graphics context
* @param x
* The x co-ordinate
* @param y
* The y co-ordinate
* @return The x co-ordinate, plus the width of the painted string
*/
public static int paintSyntaxLine(Segment line, Token tokens, SyntaxStyle[] styles, TabExpander expander, Graphics gfx,
int x, int y) {
Font defaultFont = gfx.getFont();
Color defaultColor = gfx.getColor();
int offset = 0;
for (;;) {
byte id = tokens.id;
if (id == Token.END) {
break;
}
int length = tokens.length;
if (id == Token.NULL) {
if (!defaultColor.equals(gfx.getColor())) {
gfx.setColor(defaultColor);
}
if (!defaultFont.equals(gfx.getFont())) {
gfx.setFont(defaultFont);
}
} else {
styles[id].setGraphicsFlags(gfx, defaultFont);
}
line.count = length;
x = Utilities.drawTabbedText(line, x, y, gfx, expander, 0);
line.offset += length;
offset += length;
tokens = tokens.next;
}
return x;
}
示例6: _drawLegend
import java.awt.Graphics; //导入方法依赖的package包/类
private int _drawLegend(Graphics graphics, int urx, int ury) {
// Ignore if there is no graphics object to draw on.
if (graphics == null) return 0;
// FIXME: consolidate all these for efficiency
Font previousFont = graphics.getFont();
graphics.setFont(_labelFont);
int spacing = _labelFontMetrics.getHeight();
Enumeration v = _legendStrings.elements();
Enumeration i = _legendDatasets.elements();
int ypos = ury + spacing;
int maxwidth = 0;
while (v.hasMoreElements()) {
String legend = (String) v.nextElement();
// NOTE: relies on _legendDatasets having the same num. of entries.
int dataset = ((Integer) i.nextElement()).intValue();
if (dataset >= 0) {
if (_usecolor) {
// Points are only distinguished up to the number of colors
int color = dataset % _colors.length;
graphics.setColor(_colors[color]);
}
_drawPoint(graphics, dataset, urx-3, ypos-3, false);
graphics.setColor(_foreground);
int width = _labelFontMetrics.stringWidth(legend);
if (width > maxwidth) maxwidth = width;
graphics.drawString(legend, urx - 15 - width, ypos);
ypos += spacing;
}
}
graphics.setFont(previousFont);
return 22 + maxwidth; // NOTE: subjective spacing parameter.
}
示例7: drawCenteredString
import java.awt.Graphics; //导入方法依赖的package包/类
/**
* Place a string in the center of a bounding rectangle (eg a plot), with an optional white box
* behind the text
* @param g
* @param text
* @param rect
* @param font
* @param xScale
* @param yScale
* @param whiteBox
*/
public static void drawCenteredString(Graphics g, String text, Rectangle2D rect, Font font, double xScale,
double yScale, boolean whiteBox) {
// Get the original font
Font oldFont = g.getFont();
// Get the FontMetrics
FontMetrics metrics = g.getFontMetrics(font);
// Determine the X coordinate for the text
int x = (int) ((rect.getWidth()* xScale - metrics.stringWidth(text)) / 2);
// Determine the Y coordinate for the text (note we add the ascent, as in java 2d 0 is top of the screen)
int y = (int) ((rect.getHeight() * yScale - metrics.getHeight()) / 2) + metrics.getAscent();
// Set the font
Graphics2D g2 = (Graphics2D) g;
g2.setFont(font);
if (whiteBox) {
//draw a white box behind the text
int textWidth = (int) font.getStringBounds(text, g2.getFontRenderContext()).getWidth() + 2;
int textHeight = metrics.getHeight() + 2;
Color oldColor = g2.getColor();
g2.setColor(Color.white);
g2.fillRect(x-1, y-textHeight+4, textWidth, textHeight);
g2.setColor(oldColor);
}
// Draw the String
g.drawString(text, x , y);
// Reset font
g.setFont(oldFont);
}
示例8: drawLowerLeftString
import java.awt.Graphics; //导入方法依赖的package包/类
/**
* Place a string in the lower left of a bounding rectangle (eg a plot)
* @param g
* @param text
* @param rect
* @param font
* @param xScale
* @param yScale
*/
public static void drawLowerLeftString(Graphics g, String text, Rectangle2D rect, Font font, double xScale, double yScale, boolean whiteBox) {
// Get the original font
Font oldFont = g.getFont();
// Get the FontMetrics
FontMetrics metrics = g.getFontMetrics(font);
// Determine the X coordinate for the text
int x = 10;
// Determine the Y coordinate for the text (note we add the ascent, as in java 2d 0 is top of the screen)
int y = (int) ((rect.getHeight() * yScale - metrics.getHeight()) ) + metrics.getAscent() - 5;
// Set the font
Graphics2D g2 = (Graphics2D) g;
g2.setFont(font);
if (whiteBox) {
//draw a white box behind the text
int textWidth = (int) font.getStringBounds(text, g2.getFontRenderContext()).getWidth() + 2;
int textHeight = metrics.getHeight() + 2;
Color oldColor = g2.getColor();
g2.setColor(Color.white);
g2.fillRect(x-1, y-textHeight+4, textWidth, textHeight);
g2.setColor(oldColor);
}
// Draw the String
g2.drawString(text, x , y);
// Reset font
g2.setFont(oldFont);
}
示例9: paintIconRectangular
import java.awt.Graphics; //导入方法依赖的package包/类
@Override
public void paintIconRectangular(InstancePainter painter) {
Graphics g = painter.getGraphics();
g.setColor(Color.black);
g.drawRect(1, 2, 16, 16);
Font old = g.getFont();
g.setFont(old.deriveFont(9.0f));
GraphicsUtil.drawCenteredText(g, "2k", 9, 6);
GraphicsUtil.drawCenteredText(g, "+1", 9, 13);
g.setFont(old);
}
示例10: drawText
import java.awt.Graphics; //导入方法依赖的package包/类
static public void drawText(Graphics g, Font font, String text, int x, int y, int halign, int valign) {
Font oldfont = g.getFont();
if (font != null)
g.setFont(font);
drawText(g, text, x, y, halign, valign);
if (font != null)
g.setFont(oldfont);
}
示例11: getTextBounds
import java.awt.Graphics; //导入方法依赖的package包/类
static public Rectangle getTextBounds(Graphics g, Font font, String text, int x, int y, int halign, int valign) {
if (g == null)
return new Rectangle(x, y, 0, 0);
Font oldfont = g.getFont();
if (font != null)
g.setFont(font);
Rectangle ret = getTextBounds(g, text, x, y, halign, valign);
if (font != null)
g.setFont(oldfont);
return ret;
}
示例12: paintString
import java.awt.Graphics; //导入方法依赖的package包/类
private void paintString(Graphics g, String msg) {
Font old = g.getFont();
g.setFont(old.deriveFont(Font.BOLD).deriveFont(18.0f));
FontMetrics fm = g.getFontMetrics();
int x = (getWidth() - fm.stringWidth(msg)) / 2;
if (x < 0)
x = 0;
g.drawString(msg, x, getHeight() - 23);
g.setFont(old);
return;
}
示例13: paintVertex
import java.awt.Graphics; //导入方法依赖的package包/类
public void paintVertex(Graphics g, Vertex v, int x, int y) {
String label = null;
if (getLabel() != null) {
// label = (String) v.getUserDatum(getLabel());
label = StringLabeller.getLabeller((Graph) v.getGraph()).getLabel(v);
}
if (label == null) {
label = v.toString();
}
if (label.length() > 15) {
label = label.substring(0, 14);
}
int nodeSize = mDefaultNodeSize;
if (mSizeKey != null) {
Number decoratedNodeSize = (Number) v.getUserDatum(mSizeKey);
nodeSize =
(int) Math.ceil(
decoratedNodeSize.doubleValue() * mNodeSizeScale);
}
if (isPicked(v))
g.setColor(Color.ORANGE);
else
g.setColor(Color.RED);
int labelSize = g.getFontMetrics().stringWidth(label);
nodeSize = Math.max(nodeSize, 10);
nodeSize = Math.min(nodeSize, 150);
g.fillOval(x - nodeSize / 2, y - nodeSize / 2, nodeSize, nodeSize);
g.setColor(Color.GRAY);
g.drawOval(x - nodeSize / 2, y - nodeSize / 2, nodeSize, nodeSize);
g.setColor(Color.BLACK);
Font font = new Font("Arial", Font.PLAIN, 12);
Font f = g.getFont();
g.setFont(font);
if (nodeSize > labelSize) {
g.drawString(label, x - labelSize / 2, y + 4);
} else {
g.drawString(label, x - labelSize / 2 + 20, y + 15);
}
g.setFont(f);
}
示例14: drawStringInRect
import java.awt.Graphics; //导入方法依赖的package包/类
static void drawStringInRect(JComponent c, Graphics g, String aString,
int x, int y, int width, int height,
int justification) {
FontMetrics fontMetrics;
int drawWidth, startX, startY, delta;
if (g.getFont() == null) {
// throw new InconsistencyException("No font set");
return;
}
fontMetrics = SwingUtilities2.getFontMetrics(c, g);
if (fontMetrics == null) {
// throw new InconsistencyException("No metrics for Font " + font());
return;
}
if (justification == CENTER) {
drawWidth = SwingUtilities2.stringWidth(c, fontMetrics, aString);
if (drawWidth > width) {
drawWidth = width;
}
startX = x + (width - drawWidth) / 2;
} else if (justification == RIGHT) {
drawWidth = SwingUtilities2.stringWidth(c, fontMetrics, aString);
if (drawWidth > width) {
drawWidth = width;
}
startX = x + width - drawWidth;
} else {
startX = x;
}
delta = (height - fontMetrics.getAscent() - fontMetrics.getDescent()) / 2;
if (delta < 0) {
delta = 0;
}
startY = y + height - delta - fontMetrics.getDescent();
SwingUtilities2.drawString(c, g, aString, startX, startY);
}
示例15: paintBorder
import java.awt.Graphics; //导入方法依赖的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);
}