本文整理汇总了Java中java.awt.Font.getStringBounds方法的典型用法代码示例。如果您正苦于以下问题:Java Font.getStringBounds方法的具体用法?Java Font.getStringBounds怎么用?Java Font.getStringBounds使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.awt.Font
的用法示例。
在下文中一共展示了Font.getStringBounds方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: okGDIMetrics
import java.awt.Font; //导入方法依赖的package包/类
private boolean okGDIMetrics(String str, Font font,
FontRenderContext frc, double scaleX) {
Rectangle2D bds = font.getStringBounds(str, frc);
double jdkAdvance = bds.getWidth();
jdkAdvance = Math.round(jdkAdvance*scaleX);
int gdiAdvance = ((WPrinterJob)getPrinterJob()).getGDIAdvance(str);
if (jdkAdvance > 0 && gdiAdvance > 0) {
double diff = Math.abs(gdiAdvance-jdkAdvance);
double ratio = gdiAdvance/jdkAdvance;
if (ratio < 1) {
ratio = 1/ratio;
}
return diff <= 1 || ratio < 1.002;
}
return true;
}
示例2: getSizeForString
import java.awt.Font; //导入方法依赖的package包/类
/**
* Returns an <mxRectangle> with the size (width and height in pixels) of the given string.
*
* @param text String whose size should be returned.
* @param font Font to be used for the computation.
*/
public static mxRectangle getSizeForString(String text, Font font, double scale) {
FontRenderContext frc = new FontRenderContext(null, false, false);
font = font.deriveFont((float) (font.getSize2D() * scale));
FontMetrics metrics = null;
if (fontGraphics != null) {
metrics = fontGraphics.getFontMetrics(font);
}
double lineHeight = mxConstants.LINESPACING;
if (metrics != null) {
lineHeight += metrics.getHeight();
} else {
lineHeight += font.getSize2D() * 1.27;
}
String[] lines = text.split("\n");
Rectangle2D boundingBox = null;
if (lines.length == 0) {
boundingBox = font.getStringBounds("", frc);
} else {
for (int i = 0; i < lines.length; i++) {
Rectangle2D bounds = font.getStringBounds(lines[i], frc);
if (boundingBox == null) {
boundingBox = bounds;
} else {
boundingBox.setFrame(0, 0, Math.max(boundingBox.getWidth(), bounds.getWidth()),
boundingBox.getHeight() + lineHeight);
}
}
}
return new mxRectangle(boundingBox);
}
示例3: okGDIMetrics
import java.awt.Font; //导入方法依赖的package包/类
private boolean okGDIMetrics(String str, Font font,
FontRenderContext frc, double scaleX) {
Rectangle2D bds = font.getStringBounds(str, frc);
double jdkAdvance = bds.getWidth();
jdkAdvance = Math.round(jdkAdvance*scaleX);
int gdiAdvance = ((WPrinterJob)getPrinterJob()).getGDIAdvance(str);
if (jdkAdvance > 0 && gdiAdvance > 0) {
double diff = Math.abs(gdiAdvance-jdkAdvance);
double ratio = gdiAdvance/jdkAdvance;
if (ratio < 1) {
ratio = 1/ratio;
}
return diff <= 1 || ratio < 1.01;
}
return true;
}
示例4: test
import java.awt.Font; //导入方法依赖的package包/类
static void test(int sz) {
Font reg = new Font(name, Font.PLAIN, sz);
Font bold = new Font(name, Font.BOLD, sz);
//System.out.println("reg="+reg);
//System.out.println("bold="+bold);
FontRenderContext frc = new FontRenderContext(null, false, false);
if (reg.getFontName(Locale.ENGLISH).equals(name) &&
bold.getFontName(Locale.ENGLISH).equals(name)) {
Rectangle2D rb = reg.getStringBounds(" ", frc);
Rectangle2D bb = bold.getStringBounds(" ", frc);
if (bb.getWidth() > rb.getWidth() + 1.01f) {
System.err.println("reg="+reg+" bds = " + rb);
System.err.println("bold="+bold+" bds = " + bb);
throw new RuntimeException("Advance difference too great.");
}
} else {
System.out.println("Skipping test because fonts aren't as expected");
}
}
示例5: print
import java.awt.Font; //导入方法依赖的package包/类
public int print(Graphics g, PageFormat fmt, int pageNo) {
if( pageNo>0 ) return NO_SUCH_PAGE;
Graphics2D g2 = (Graphics2D)g;
Dimension dim = wwd.getPreferredSize();
Rectangle r = wwd.getBounds();
if(r.width>dim.width) r.width = dim.width;
if(r.height>dim.height) r.height = dim.height;
org.geomapapp.util.DateFmt df = new org.geomapapp.util.DateFmt();
int secs = (int)(System.currentTimeMillis()/1000L);
String date = df.format(secs);
Font font = new Font("SansSerif", Font.PLAIN, 8);
g.setFont( font );
Rectangle2D r2d = font.getStringBounds(date, g2.getFontRenderContext());
// g2.translate( r.getWidth()-20.-r2d.getWidth(), r.getHeight()+18. );
g2.setColor( Color.black);
// g.setClip( new Rectangle( 0, 0, r.width, r.height) );
double w = fmt.getImageableWidth();
double h = fmt.getImageableHeight();
double x = fmt.getImageableX();
double y = fmt.getImageableY();
g2.translate(x, y);
double scale = Math.min( w / r.getWidth(), h / r.getHeight());
int xd = (int)(scale*r.getWidth()-10.-r2d.getWidth());
int yd = (int)(scale*r.getHeight()+18.);
g2.drawString( date, xd, yd);
g2.translate( -r.getX()*scale, -r.getY()*scale );
g2.scale( scale, scale);
wwd.getContext().makeCurrent();
BufferedImage image = Screenshot.readToBufferedImage(r.width, r.height);
g2.drawImage(image, 0, 0, this);
return PAGE_EXISTS;
}
示例6: drawLegend
import java.awt.Font; //导入方法依赖的package包/类
/**
* drawing the legend
* @param ca colors of the legend
* @param sa comments respectivily
* @param f font
* @param x initial abscissa
* @param y initial ordinate
* @param g2d
* @return panel size
*/
private double drawLegend(Color[] ca, String[] sa, Font f, double x, double y, Graphics2D g2d) {
Rectangle2D[] ra = new Rectangle2D[ca.length];
Rectangle2D[] tba = new Rectangle2D[ca.length];
double maxw = 0.0, gap = 5.0;
String ts = "Legenda";
//backup
Color ctmp = g2d.getColor();
Font ftmp = g2d.getFont();
g2d.setFont(f);
Rectangle2D tr = f.getStringBounds(ts, g2d.getFontRenderContext());
maxw = tr.getWidth();
for (int i = 0; i < ca.length; i++) {
tba[i] = f.getStringBounds(sa[i], g2d.getFontRenderContext());
ra[i] = new Rectangle2D.Double(x + gap, y + gap + (tr.getHeight() + gap) * (i + 1), tr.getHeight(), tr.getHeight());
g2d.setColor(ca[i]);
g2d.fill(ra[i]);
g2d.setColor(Color.BLACK);
g2d.draw(ra[i]);
g2d.drawString(sa[i], (float) (x + gap * 2 + tr.getHeight()),
(float) (y + gap + (tr.getHeight() + gap) * (i + 1) + tr.getHeight() / 2.0 - tr.getY() / 2.0));
if (maxw < tba[i].getWidth()) {
maxw = tba[i].getWidth();
}
}
g2d.drawRect((int) x, (int) y, (int) (maxw + 3.0 * gap + tr.getHeight()), (int) (y + (tr.getHeight() + gap) * (ca.length + 1) + gap));
g2d.drawRect((int) x, (int) y, (int) (maxw + 3.0 * gap + tr.getHeight()), (int) (tr.getHeight() + gap));
g2d.fillRect((int) x, (int) y, (int) (maxw + 3.0 * gap + tr.getHeight()), (int) (tr.getHeight() + gap));
g2d.setColor(Color.WHITE);
g2d.drawString(ts, (float) (x + gap + (maxw - tr.getWidth() + tr.getHeight()) / 2.0), (float) (y + tr.getY() / 2.0 + tr.getHeight()));
//restore
g2d.setFont(ftmp);
g2d.setColor(ctmp);
return (maxw + 3.0 * gap + tr.getHeight());
}
示例7: drawLegend
import java.awt.Font; //导入方法依赖的package包/类
/**
* drawing the legend
*
* @param ca
* color of the legend
* @param sa
* Comments
* @param f
* font
* @param x
* initial abscissa
* @param y
* initial ordinate
* @param g2d
* @return panel length
*/
private double drawLegend(Color[] ca, String[] sa, Font f, double x, double y, Graphics2D g2d, boolean draw) {
Rectangle2D[] ra = new Rectangle2D[ca.length];
Rectangle2D[] tba = new Rectangle2D[ca.length];
double maxw = 0.0, gap = 5.0;
String ts = "Legenda";
// backup
Color ctmp = g2d.getColor();
Font ftmp = g2d.getFont();
g2d.setFont(f);
Rectangle2D tr = f.getStringBounds(ts, g2d.getFontRenderContext());
maxw = tr.getWidth();
for (int i = 0; i < ca.length; i++) {
tba[i] = f.getStringBounds(sa[i], g2d.getFontRenderContext());
ra[i] = new Rectangle2D.Double(x + gap, y + gap + (tr.getHeight() + gap) * (i + 1), tr.getHeight(), tr.getHeight());
if (draw) {
g2d.setColor(ca[i]);
g2d.fill(ra[i]);
g2d.setColor(Color.BLACK);
g2d.draw(ra[i]);
g2d.drawString(sa[i], (float) (x + gap * 2 + tr.getHeight()), (float) (y + gap + (tr.getHeight() + gap) * (i + 1) + tr.getHeight()
/ 2.0 - tr.getY() / 2.0));
}
if (maxw < tba[i].getWidth()) {
maxw = tba[i].getWidth();
}
}
if (draw) {
g2d.drawRect((int) x, (int) y, (int) (maxw + 3.0 * gap + tr.getHeight()), (int) (y + (tr.getHeight() + gap) * (ca.length + 1) + gap));
g2d.drawRect((int) x, (int) y, (int) (maxw + 3.0 * gap + tr.getHeight()), (int) (tr.getHeight() + gap));
g2d.fillRect((int) x, (int) y, (int) (maxw + 3.0 * gap + tr.getHeight()), (int) (tr.getHeight() + gap));
g2d.setColor(Color.WHITE);
g2d.drawString(ts, (float) (x + gap + (maxw - tr.getWidth() + tr.getHeight()) / 2.0), (float) (y + tr.getY() / 2.0 + tr.getHeight()));
}
// restore
g2d.setFont(ftmp);
g2d.setColor(ctmp);
return (maxw + 3.0 * gap + tr.getHeight());
}
示例8: drawLegend
import java.awt.Font; //导入方法依赖的package包/类
/**
* drawing the legend
*
* @param ca
* color of the legend
* @param sa
* Comments
* @param f
* font
* @param x
* initial abscissa
* @param y
* initial ordinate
* @param g2d
* @return panel lenght
*/
private double drawLegend(Color[] ca, String[] sa, Font f, double x, double y, Graphics2D g2d, boolean draw) {
Rectangle2D[] ra = new Rectangle2D[ca.length];
Rectangle2D[] tba = new Rectangle2D[ca.length];
double maxw = 0.0, gap = 5.0;
String ts = "Legenda";
// backup
Color ctmp = g2d.getColor();
Font ftmp = g2d.getFont();
g2d.setFont(f);
Rectangle2D tr = f.getStringBounds(ts, g2d.getFontRenderContext());
maxw = tr.getWidth();
for (int i = 0; i < ca.length; i++) {
tba[i] = f.getStringBounds(sa[i], g2d.getFontRenderContext());
ra[i] = new Rectangle2D.Double(x + gap, y + gap + (tr.getHeight() + gap) * (i + 1), tr.getHeight(), tr.getHeight());
if (draw) {
g2d.setColor(ca[i]);
g2d.fill(ra[i]);
g2d.setColor(Color.BLACK);
g2d.draw(ra[i]);
g2d.drawString(sa[i], (float) (x + gap * 2 + tr.getHeight()), (float) (y + gap + (tr.getHeight() + gap) * (i + 1) + tr.getHeight()
/ 2.0 - tr.getY() / 2.0));
}
if (maxw < tba[i].getWidth()) {
maxw = tba[i].getWidth();
}
}
if (draw) {
g2d.drawRect((int) x, (int) y, (int) (maxw + 3.0 * gap + tr.getHeight()), (int) (y + (tr.getHeight() + gap) * (ca.length + 1) + gap));
g2d.drawRect((int) x, (int) y, (int) (maxw + 3.0 * gap + tr.getHeight()), (int) (tr.getHeight() + gap));
g2d.fillRect((int) x, (int) y, (int) (maxw + 3.0 * gap + tr.getHeight()), (int) (tr.getHeight() + gap));
g2d.setColor(Color.WHITE);
g2d.drawString(ts, (float) (x + gap + (maxw - tr.getWidth() + tr.getHeight()) / 2.0), (float) (y + tr.getY() / 2.0 + tr.getHeight()));
}
// restore
g2d.setFont(ftmp);
g2d.setColor(ctmp);
return (maxw + 3.0 * gap + tr.getHeight());
}
示例9: getStringBounds
import java.awt.Font; //导入方法依赖的package包/类
protected Rectangle2D getStringBounds(String text, Font font) {
return font.getStringBounds(text, CONTEXT);
}
示例10: getSizeForString
import java.awt.Font; //导入方法依赖的package包/类
/**
* Returns an <mxRectangle> with the size (width and height in pixels) of
* the given string.
*
* @param text
* String whose size should be returned.
* @param font
* Font to be used for the computation.
*/
public static mxRectangle getSizeForString(String text, Font font,
double scale)
{
FontRenderContext frc = new FontRenderContext(null, false, false);
font = font.deriveFont((float) (font.getSize2D() * scale));
FontMetrics metrics = null;
if (fontGraphics != null)
{
metrics = fontGraphics.getFontMetrics(font);
}
double lineHeight = mxConstants.LINESPACING;
if (metrics != null)
{
lineHeight += metrics.getHeight();
}
else
{
lineHeight += font.getSize2D() * 1.27;
}
String[] lines = text.split("\n");
Rectangle2D boundingBox = null;
if (lines.length == 0)
{
boundingBox = font.getStringBounds("", frc);
}
else
{
for (int i = 0; i < lines.length; i++)
{
Rectangle2D bounds = font.getStringBounds(lines[i], frc);
if (boundingBox == null)
{
boundingBox = bounds;
}
else
{
boundingBox
.setFrame(
0,
0,
Math.max(boundingBox.getWidth(),
bounds.getWidth()),
boundingBox.getHeight() + lineHeight);
}
}
}
return new mxRectangle(boundingBox);
}
示例11: runTest
import java.awt.Font; //导入方法依赖的package包/类
private static void runTest() {
im = new BufferedImage(W, H, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = im.createGraphics();
g2d.setColor(Color.white);
g2d.fillRect(0, 0, W, H);
g2d.setColor(Color.black);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
char[] chs = "Sample Text.".toCharArray();
int len = chs.length;
int x = 50, y = 100;
FontRenderContext frc = g2d.getFontRenderContext();
Font plain = new Font("Serif", Font.PLAIN, 48);
GlyphVector pgv = plain.layoutGlyphVector(frc, chs, 0, len, 0);
g2d.setFont(plain);
g2d.drawChars(chs, 0, len, x, y); y +=50;
g2d.drawGlyphVector(pgv, x, y); y += 50;
Rectangle2D plainStrBounds = plain.getStringBounds(chs, 0, len, frc);
Rectangle2D plainGVBounds = pgv.getLogicalBounds();
Font bold = new Font("Serif", Font.BOLD, 48);
GlyphVector bgv = bold.layoutGlyphVector(frc, chs, 0, len, 0);
Rectangle2D boldStrBounds = bold.getStringBounds(chs, 0, len, frc);
Rectangle2D boldGVBounds = bgv.getLogicalBounds();
g2d.setFont(bold);
g2d.drawChars(chs, 0, len, x, y); y +=50;
g2d.drawGlyphVector(bgv, x, y);
System.out.println("Plain String Bounds = " + plainStrBounds);
System.out.println("Bold String Bounds = " + boldStrBounds);
System.out.println("Plain GlyphVector Bounds = " + plainGVBounds);
System.out.println("Bold GlyphVector Bounds = " + boldGVBounds);
if (!plainStrBounds.equals(boldStrBounds) &&
plainGVBounds.equals(boldGVBounds))
{
System.out.println("Test failed: Plain GV bounds same as Bold");
if (!interactive) {
throw new RuntimeException("Plain GV bounds same as Bold");
}
}
}
示例12: getStringBounds
import java.awt.Font; //导入方法依赖的package包/类
/**
* Create a bounding box for the visual representation of the given text.
*
* @param text
* The text.
* @param font
* The font in which to render the text.
* @return The bounding box.
*/
public static Rectangle2D getStringBounds(String text, Font font) {
return font.getStringBounds(text, frc);
}