下面列出了怎么用java.awt.font.GlyphMetrics的API类实例代码及写法,或者点击链接到github查看源代码。
public GlyphMetrics getGlyphMetrics(int ix) {
if (ix < 0 || ix >= glyphs.length) {
throw new IndexOutOfBoundsException("ix = " + ix);
}
Rectangle2D vb = getGlyphVisualBounds(ix).getBounds2D();
Point2D pt = getGlyphPosition(ix);
vb.setRect(vb.getMinX() - pt.getX(),
vb.getMinY() - pt.getY(),
vb.getWidth(),
vb.getHeight());
Point2D.Float adv =
getGlyphStrike(ix).strike.getGlyphMetrics(glyphs[ix]);
GlyphMetrics gm = new GlyphMetrics(true, adv.x, adv.y,
vb,
GlyphMetrics.STANDARD);
return gm;
}
public GlyphMetrics getGlyphMetrics(int ix) {
if (ix < 0 || ix >= glyphs.length) {
throw new IndexOutOfBoundsException("ix = " + ix);
}
Rectangle2D vb = getGlyphVisualBounds(ix).getBounds2D();
Point2D pt = getGlyphPosition(ix);
vb.setRect(vb.getMinX() - pt.getX(),
vb.getMinY() - pt.getY(),
vb.getWidth(),
vb.getHeight());
Point2D.Float adv =
getGlyphStrike(ix).strike.getGlyphMetrics(glyphs[ix]);
GlyphMetrics gm = new GlyphMetrics(true, adv.x, adv.y,
vb,
GlyphMetrics.STANDARD);
return gm;
}
public GlyphMetrics getGlyphMetrics(int ix) {
if (ix < 0 || ix >= glyphs.length) {
throw new IndexOutOfBoundsException("ix = " + ix);
}
Rectangle2D vb = getGlyphVisualBounds(ix).getBounds2D();
Point2D pt = getGlyphPosition(ix);
vb.setRect(vb.getMinX() - pt.getX(),
vb.getMinY() - pt.getY(),
vb.getWidth(),
vb.getHeight());
Point2D.Float adv =
getGlyphStrike(ix).strike.getGlyphMetrics(glyphs[ix]);
GlyphMetrics gm = new GlyphMetrics(true, adv.x, adv.y,
vb,
GlyphMetrics.STANDARD);
return gm;
}
public GlyphMetrics getGlyphMetrics(int ix) {
if (ix < 0 || ix >= glyphs.length) {
throw new IndexOutOfBoundsException("ix = " + ix);
}
Rectangle2D vb = getGlyphVisualBounds(ix).getBounds2D();
Point2D pt = getGlyphPosition(ix);
vb.setRect(vb.getMinX() - pt.getX(),
vb.getMinY() - pt.getY(),
vb.getWidth(),
vb.getHeight());
Point2D.Float adv =
getGlyphStrike(ix).strike.getGlyphMetrics(glyphs[ix]);
GlyphMetrics gm = new GlyphMetrics(true, adv.x, adv.y,
vb,
GlyphMetrics.STANDARD);
return gm;
}
public GlyphMetrics getGlyphMetrics(int ix) {
if (ix < 0 || ix >= glyphs.length) {
throw new IndexOutOfBoundsException("ix = " + ix);
}
Rectangle2D vb = getGlyphVisualBounds(ix).getBounds2D();
Point2D pt = getGlyphPosition(ix);
vb.setRect(vb.getMinX() - pt.getX(),
vb.getMinY() - pt.getY(),
vb.getWidth(),
vb.getHeight());
Point2D.Float adv =
getGlyphStrike(ix).strike.getGlyphMetrics(glyphs[ix]);
GlyphMetrics gm = new GlyphMetrics(true, adv.x, adv.y,
vb,
GlyphMetrics.STANDARD);
return gm;
}
public GlyphMetrics getGlyphMetrics(int ix) {
if (ix < 0 || ix >= glyphs.length) {
throw new IndexOutOfBoundsException("ix = " + ix);
}
Rectangle2D vb = getGlyphVisualBounds(ix).getBounds2D();
Point2D pt = getGlyphPosition(ix);
vb.setRect(vb.getMinX() - pt.getX(),
vb.getMinY() - pt.getY(),
vb.getWidth(),
vb.getHeight());
Point2D.Float adv =
getGlyphStrike(ix).strike.getGlyphMetrics(glyphs[ix]);
GlyphMetrics gm = new GlyphMetrics(true, adv.x, adv.y,
vb,
GlyphMetrics.STANDARD);
return gm;
}
public GlyphMetrics getGlyphMetrics(int ix) {
if (ix < 0 || ix >= glyphs.length) {
throw new IndexOutOfBoundsException("ix = " + ix);
}
Rectangle2D vb = getGlyphVisualBounds(ix).getBounds2D();
Point2D pt = getGlyphPosition(ix);
vb.setRect(vb.getMinX() - pt.getX(),
vb.getMinY() - pt.getY(),
vb.getWidth(),
vb.getHeight());
Point2D.Float adv =
getGlyphStrike(ix).strike.getGlyphMetrics(glyphs[ix]);
GlyphMetrics gm = new GlyphMetrics(true, adv.x, adv.y,
vb,
GlyphMetrics.STANDARD);
return gm;
}
public GlyphMetrics getGlyphMetrics(int ix) {
if (ix < 0 || ix >= glyphs.length) {
throw new IndexOutOfBoundsException("ix = " + ix);
}
Rectangle2D vb = getGlyphVisualBounds(ix).getBounds2D();
Point2D pt = getGlyphPosition(ix);
vb.setRect(vb.getMinX() - pt.getX(),
vb.getMinY() - pt.getY(),
vb.getWidth(),
vb.getHeight());
Point2D.Float adv =
getGlyphStrike(ix).strike.getGlyphMetrics(glyphs[ix]);
GlyphMetrics gm = new GlyphMetrics(true, adv.x, adv.y,
vb,
GlyphMetrics.STANDARD);
return gm;
}
public GlyphMetrics getGlyphMetrics(int ix) {
if (ix < 0 || ix >= glyphs.length) {
throw new IndexOutOfBoundsException("ix = " + ix);
}
Rectangle2D vb = getGlyphVisualBounds(ix).getBounds2D();
Point2D pt = getGlyphPosition(ix);
vb.setRect(vb.getMinX() - pt.getX(),
vb.getMinY() - pt.getY(),
vb.getWidth(),
vb.getHeight());
Point2D.Float adv =
getGlyphStrike(ix).strike.getGlyphMetrics(glyphs[ix]);
GlyphMetrics gm = new GlyphMetrics(true, adv.x, adv.y,
vb,
GlyphMetrics.STANDARD);
return gm;
}
public GlyphMetrics getGlyphMetrics(int ix) {
if (ix < 0 || ix >= glyphs.length) {
throw new IndexOutOfBoundsException("ix = " + ix);
}
Rectangle2D vb = getGlyphVisualBounds(ix).getBounds2D();
Point2D pt = getGlyphPosition(ix);
vb.setRect(vb.getMinX() - pt.getX(),
vb.getMinY() - pt.getY(),
vb.getWidth(),
vb.getHeight());
Point2D.Float adv =
getGlyphStrike(ix).strike.getGlyphMetrics(glyphs[ix]);
GlyphMetrics gm = new GlyphMetrics(true, adv.x, adv.y,
vb,
GlyphMetrics.STANDARD);
return gm;
}
public GlyphMetrics getGlyphMetrics(int ix) {
if (ix < 0 || ix >= glyphs.length) {
throw new IndexOutOfBoundsException("ix = " + ix);
}
Rectangle2D vb = getGlyphVisualBounds(ix).getBounds2D();
Point2D pt = getGlyphPosition(ix);
vb.setRect(vb.getMinX() - pt.getX(),
vb.getMinY() - pt.getY(),
vb.getWidth(),
vb.getHeight());
Point2D.Float adv =
getGlyphStrike(ix).strike.getGlyphMetrics(glyphs[ix]);
GlyphMetrics gm = new GlyphMetrics(true, adv.x, adv.y,
vb,
GlyphMetrics.STANDARD);
return gm;
}
public GlyphMetrics getGlyphMetrics(int ix) {
if (ix < 0 || ix >= glyphs.length) {
throw new IndexOutOfBoundsException("ix = " + ix);
}
Rectangle2D vb = getGlyphVisualBounds(ix).getBounds2D();
Point2D pt = getGlyphPosition(ix);
vb.setRect(vb.getMinX() - pt.getX(),
vb.getMinY() - pt.getY(),
vb.getWidth(),
vb.getHeight());
Point2D.Float adv =
getGlyphStrike(ix).strike.getGlyphMetrics(glyphs[ix]);
GlyphMetrics gm = new GlyphMetrics(true, adv.x, adv.y,
vb,
GlyphMetrics.STANDARD);
return gm;
}
public GlyphMetrics getGlyphMetrics(int ix) {
if (ix < 0 || ix >= glyphs.length) {
throw new IndexOutOfBoundsException("ix = " + ix);
}
Rectangle2D vb = getGlyphVisualBounds(ix).getBounds2D();
Point2D pt = getGlyphPosition(ix);
vb.setRect(vb.getMinX() - pt.getX(),
vb.getMinY() - pt.getY(),
vb.getWidth(),
vb.getHeight());
Point2D.Float adv =
getGlyphStrike(ix).strike.getGlyphMetrics(glyphs[ix]);
GlyphMetrics gm = new GlyphMetrics(true, adv.x, adv.y,
vb,
GlyphMetrics.STANDARD);
return gm;
}
public GlyphMetrics getGlyphMetrics(int ix) {
if (ix < 0 || ix >= glyphs.length) {
throw new IndexOutOfBoundsException("ix = " + ix);
}
Rectangle2D vb = getGlyphVisualBounds(ix).getBounds2D();
Point2D pt = getGlyphPosition(ix);
vb.setRect(vb.getMinX() - pt.getX(),
vb.getMinY() - pt.getY(),
vb.getWidth(),
vb.getHeight());
Point2D.Float adv =
getGlyphStrike(ix).strike.getGlyphMetrics(glyphs[ix]);
GlyphMetrics gm = new GlyphMetrics(true, adv.x, adv.y,
vb,
GlyphMetrics.STANDARD);
return gm;
}
private static GlyphVector getWrappedGlyphVector(String str, double width, Font font, FontRenderContext frc) {
Point2D gmPos = new Point2D.Float();
GlyphVector gv = font.createGlyphVector(frc, str);
float lineHeight = (float) gv.getLogicalBounds().getHeight();
float pos = 0f;
int lineCount = 0;
GlyphMetrics gm;
for (int i = 0; i < gv.getNumGlyphs(); i++) {
gm = gv.getGlyphMetrics(i);
float advance = gm.getAdvance();
if (pos < width && width <= pos + advance) {
lineCount++;
pos = 0f;
}
gmPos.setLocation(pos, lineHeight * lineCount);
gv.setGlyphPosition(i, gmPos);
pos += advance;
}
return gv;
}
private static GlyphVector getWrappedGlyphVector(String str, double width, Font font, FontRenderContext frc) {
Point2D gmPos = new Point2D.Float();
GlyphVector gv = font.createGlyphVector(frc, str);
float lineHeight = (float) gv.getLogicalBounds().getHeight();
float pos = 0f;
int lineCount = 0;
GlyphMetrics gm;
for (int i = 0; i < gv.getNumGlyphs(); i++) {
gm = gv.getGlyphMetrics(i);
float advance = gm.getAdvance();
if (pos < width && width <= pos + advance) {
lineCount++;
pos = 0f;
}
gmPos.setLocation(pos, lineHeight * lineCount);
gv.setGlyphPosition(i, gmPos);
pos += advance;
}
return gv;
}
private static GlyphVector makeJustifiedGlyphVector(GlyphVector gv, int width) {
Rectangle2D r = gv.getVisualBounds();
float jw = (float) width;
float vw = (float) r.getWidth();
if (jw > vw) {
int num = gv.getNumGlyphs();
float xx = (jw - vw) / (num - 1f);
float pos = num == 1 ? (jw - vw) * .5f : 0f;
Point2D gmPos = new Point2D.Float();
for (int i = 0; i < num; i++) {
GlyphMetrics gm = gv.getGlyphMetrics(i);
gmPos.setLocation(pos, 0f);
gv.setGlyphPosition(i, gmPos);
pos += gm.getAdvance() + xx;
}
}
return gv;
}
protected MarqueePanel() {
super();
addHierarchyListener(e -> {
if ((e.getChangeFlags() & HierarchyEvent.DISPLAYABILITY_CHANGED) != 0 && !e.getComponent().isDisplayable()) {
animator.stop();
}
});
String text = "abcdefghijklmnopqrstuvwxyz";
Font font = new Font(Font.SERIF, Font.PLAIN, 100);
FontRenderContext frc = new FontRenderContext(null, true, true);
gv = font.createGlyphVector(frc, text);
lm = font.getLineMetrics(text, frc);
GlyphMetrics xgm = gv.getGlyphMetrics(23);
corpusSize = (float) xgm.getBounds2D().getHeight();
animator.start();
}
private static GlyphVector getWrappedGlyphVector(String str, double width, Font font, FontRenderContext frc) {
Point2D gmPos = new Point2D.Float();
GlyphVector gv = font.createGlyphVector(frc, str);
float lineHeight = (float) gv.getLogicalBounds().getHeight();
float pos = 0f;
int lineCount = 0;
GlyphMetrics gm;
for (int i = 0; i < gv.getNumGlyphs(); i++) {
// TEST: gv.setGlyphTransform(i, at);
gm = gv.getGlyphMetrics(i);
float advance = gm.getAdvance();
if (pos < width && width <= pos + advance) {
lineCount++;
pos = 0f;
}
gmPos.setLocation(pos, lineHeight * lineCount);
gv.setGlyphPosition(i, gmPos);
pos += advance;
}
return gv;
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
GlyphMetrics gm;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
gm = gv.getGlyphMetrics(i);
}
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
GlyphMetrics gm;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
gm = gv.getGlyphMetrics(i);
}
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
GlyphMetrics gm;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
gm = gv.getGlyphMetrics(i);
}
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
GlyphMetrics gm;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
gm = gv.getGlyphMetrics(i);
}
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
GlyphMetrics gm;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
gm = gv.getGlyphMetrics(i);
}
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
GlyphMetrics gm;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
gm = gv.getGlyphMetrics(i);
}
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
GlyphMetrics gm;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
gm = gv.getGlyphMetrics(i);
}
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
GlyphMetrics gm;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
gm = gv.getGlyphMetrics(i);
}
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
GlyphMetrics gm;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
gm = gv.getGlyphMetrics(i);
}
} while (--numReps >= 0);
}
public void runTest(Object ctx, int numReps) {
GVContext gvctx = (GVContext)ctx;
GlyphVector gv = gvctx.gv;
GlyphMetrics gm;
do {
for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
gm = gv.getGlyphMetrics(i);
}
} while (--numReps >= 0);
}
@Override
public SerializationWrapper<?> filter(Object object) {
if (object instanceof GlyphMetrics) {
GlyphMetrics gm = (GlyphMetrics) object;
return new GlyphMetricsSerializationWrapper(gm);
}
return null;
}