下面列出了java.awt.FontMetrics#stringWidth ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void renderHumanAction(StandardEntity entity, Color colour, String s) {
Pair<Integer, Integer> location = entity.getLocation(world);
int x = t.xToScreen(location.first()) - SIZE / 2;
int y = t.yToScreen(location.second()) - SIZE / 2;
Shape shape = new Ellipse2D.Double(x, y, SIZE, SIZE);
g.setColor(colour);
g.fill(shape);
if (s != null) {
g.setColor(Color.BLACK);
FontMetrics metrics = g.getFontMetrics();
int width = metrics.stringWidth(s);
int height = metrics.getHeight();
x = t.xToScreen(location.first());
y = t.yToScreen(location.second());
g.drawString(s, x - (width / 2), y + (height / 2));
}
}
private void drawLabelWithLineBreaks(Graphics2D g, String label, float x0, float y0, boolean vertical) {
g.setFont(labelFont);
FontMetrics fm = g.getFontMetrics();
int stringHeight = fm.getHeight();
String[] lines = label.split("-linebreak-");
int half = lines.length / 2;
int h = lines.length % 2 == 0 ? -(half - 1) * stringHeight : -half * stringHeight - (fm.getAscent() + fm.getDescent()) / 2 + fm.getAscent();
float x1;
for (String line : lines) {
x1 = x0 - fm.stringWidth(line) / 2;
g.setColor(getContrastColor(Math.round(x1), Math.round(y0 + h)));
if (vertical) {
g.rotate(Math.PI * 0.5, x0, y0);
g.drawString(line, x1, y0 + h);
g.rotate(-Math.PI * 0.5, x0, y0);
} else {
g.drawString(line, x1, y0 + h);
}
h += stringHeight;
}
}
/**
* Estimates the maximum width of the tick labels, assuming the specified
* tick unit is used.
* <P>
* Rather than computing the string bounds of every tick on the axis, we
* just look at two values: the lower bound and the upper bound for the
* axis. These two values will usually be representative.
*
* @param g2 the graphics device.
* @param unit the tick unit to use for calculation.
*
* @return The estimated maximum width of the tick labels.
*/
private double estimateMaximumTickLabelHeight(Graphics2D g2,
DateTickUnit unit) {
RectangleInsets tickLabelInsets = getTickLabelInsets();
double result = tickLabelInsets.getTop() + tickLabelInsets.getBottom();
Font tickLabelFont = getTickLabelFont();
FontRenderContext frc = g2.getFontRenderContext();
LineMetrics lm = tickLabelFont.getLineMetrics("ABCxyz", frc);
if (!isVerticalTickLabels()) {
// all tick labels have the same width (equal to the height of
// the font)...
result += lm.getHeight();
}
else {
// look at lower and upper bounds...
DateRange range = (DateRange) getRange();
Date lower = range.getLowerDate();
Date upper = range.getUpperDate();
String lowerStr, upperStr;
DateFormat formatter = getDateFormatOverride();
if (formatter != null) {
lowerStr = formatter.format(lower);
upperStr = formatter.format(upper);
}
else {
lowerStr = unit.dateToString(lower);
upperStr = unit.dateToString(upper);
}
FontMetrics fm = g2.getFontMetrics(tickLabelFont);
double w1 = fm.stringWidth(lowerStr);
double w2 = fm.stringWidth(upperStr);
result += Math.max(w1, w2);
}
return result;
}
private static JComboBox<Integer> createFontSizeComboBox() {
JComboBox<Integer> c = new JComboBox<Integer>(FONT_SIZE);
c.setToolTipText("Font size");
FontMetrics fm = c.getFontMetrics(c.getFont());
int w = fm.stringWidth(FONT_SIZE[FONT_SIZE.length - 1].toString()) + 40;
int h = fm.getHeight() + 8;
c.setPreferredSize(new Dimension(w, h));
c.setEditable(false);
c.setRequestFocusEnabled(false);
return c;
}
private void medidaV(Graphics2D g, int l, int t) {
FontMetrics fm = g.getFontMetrics();
String vl = dono.FormateUnidadeMedida(height);
int traco = width;
int xIni = l;// + (traco) / 2;
int xFim = xIni + traco;
int yIni = t;
int yFim = t + height;
int xLin = l + (width / 2);
g.drawLine(xIni, yIni, xFim, yIni);
g.drawLine(xIni, yFim, xFim, yFim);
g.drawLine(xLin, yIni, xLin, yFim);
int degrees = isInvertido() ? 90 : -90;
int desse = isInvertido() ? 0 : fm.stringWidth(vl);
//int centra = fm.getHeight() / 2 - fm.getDescent();
int centra = fm.getHeight() - fm.getDescent();
centra = isInvertido() ? -centra : centra;
AffineTransform at = AffineTransform.getRotateInstance(Math.toRadians(degrees));
Font f = new Font(g.getFont().getName(), Font.BOLD, g.getFont().getSize());
Font f2 = g.getFont();
g.setFont(f.deriveFont(at));
yIni = yIni + (height - fm.stringWidth(vl)) / 2 + desse;
g.drawString(vl, xLin + centra, yIni);
g.setFont(f2);
}
@CalledOnlyBy(AmidstThread.EDT)
private void initializeIfNecessary(FontMetrics fontMetrics) {
if (!isInitialized) {
isInitialized = true;
for (Biome biome : biomeList.iterable()) {
biomes.add(biome);
int width = fontMetrics.stringWidth(biome.getName());
maxNameWidth = Math.max(width, maxNameWidth);
}
biomeListHeight = biomes.size() * 16;
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
g.setColor(foreground);
int width = image.getWidth();
int height = image.getHeight();
String sizes = "Dimensions: " + width + " x " + height;
g.drawString(sizes, (int) (this.getWidth() * 0.05), this.getHeight() - stringGapSize);
// adapt image width and height to the size of Navigator window
double widthRatio = ((double) image.getWidth()) / (((double) this.getWidth()) * 0.9);
double heightRatio = ((double) image.getHeight()) / (((double) this.getHeight()) * 0.9 - stringGapSize - 20);
if (widthRatio > 1 || heightRatio > 1) {
double ratio = widthRatio > heightRatio ? widthRatio : heightRatio;
width = (int) (((double) image.getWidth()) / ratio);
height = (int) (((double) image.getHeight()) / ratio);
}
g.drawImage(image, (this.getWidth() - width) / 2, (this.getHeight() - height) / 2, width, height, this);
} else {
g.setColor(Color.RED);
FontMetrics fm = this.getFontMetrics(g.getFont()) ;
String errMessage = NbBundle.getMessage(ImagePreviewPanel.class, "ERR_Thumbnail");
int stringWidth = fm.stringWidth(errMessage);
g.drawString(errMessage, (this.getWidth() - stringWidth) / 2, this.getHeight() / 2);
}
}
private void computeDimensions(Graphics g, Font font, FontMetrics fm) {
String s = text;
FontRenderContext frc = ((Graphics2D) g).getFontRenderContext();
width = fm.stringWidth(s);
ascent = fm.getAscent();
descent = fm.getDescent();
int[] xs = new int[s.length()];
int[] ys = new int[s.length()];
for (int i = 0; i < xs.length; i++) {
xs[i] = fm.stringWidth(s.substring(0, i + 1));
TextLayout lay = new TextLayout(s.substring(i, i + 1), font, frc);
Rectangle2D rect = lay.getBounds();
int asc = (int) Math.ceil(-rect.getMinY());
int desc = (int) Math.ceil(rect.getMaxY());
if (asc < 0)
asc = 0;
if (asc > 0xFFFF)
asc = 0xFFFF;
if (desc < 0)
desc = 0;
if (desc > 0xFFFF)
desc = 0xFFFF;
ys[i] = (asc << 16) | desc;
}
charX = xs;
charY = ys;
dimsKnown = true;
}
private void drawStringWithLineBreaks(Graphics2D g, String text, TextBox t) {
int x = convertPointToPixelX(t.getX());
int y = getHeight() - convertPointToPixelY(t.getY());
FontMetrics fm = g.getFontMetrics();
int stringHeight = fm.getHeight();
int stringWidth = 0;
int w = 0;
int h = 0;
for (String line : text.split("\n")) {
h += stringHeight;
g.drawString(line, x, y + h);
stringWidth = fm.stringWidth(line);
if (stringWidth > w)
w = stringWidth;
}
Rectangle2D.Float r = (Rectangle2D.Float) t.getShape();
r.x = x - 8;
r.y = y - 2;
r.width = w + 16;
r.height = h + 10;
if (t.hasBorder()) {
g.setStroke(moderateStroke);
g.drawRoundRect((int) r.x, (int) r.y, (int) r.width, (int) r.height, 10, 10);
}
if (t.isSelected()) {
g.setStroke(dashed);
g.drawRoundRect((int) (r.x - 5), (int) (r.y - 5), (int) (r.width + 10), (int) (r.height + 10), 15, 15);
}
r.x = convertPixelToPointX((int) r.x);
r.y = convertPixelToPointX((int) r.y);
r.width = convertPixelToPointX((int) r.width);
r.height = convertPixelToPointX((int) r.height);
}
/** Overrides default preferredSize impl.
* @return Standard method returned preferredSize
* (depends on font size only).
*/
@Override
public Dimension getPreferredSize () {
try {
FontMetrics fontMetrics = this.getFontMetrics(this.getFont());
return new Dimension (
fontMetrics.stringWidth (names [index]) + 30,
fontMetrics.getHeight () + 4
);
} catch (NullPointerException e) {
return new Dimension (10, 10);
}
}
/**
* Finds widest segment of text based on its rendering
* @param test
*/
private int getWidestStringText(String[] test, FontMetrics metrics) {
int ret = 0;
for (String curLine : test) {
int curWidth = metrics.stringWidth(curLine);
ret = curWidth > ret ? curWidth : ret;
}
return ret;
}
private void paintText(Graphics g, int w, int h) {
g.setFont(getFont());
String text = getString();
FontMetrics fm = g.getFontMetrics();
int textWidth = fm.stringWidth(text);
g.drawString(text, (w - textWidth) / 2,
h - fm.getDescent() - ((h - fm.getHeight()) / 2));
}
@Override
public void paintTo(Graphics2D g1, int x, int y,
int width, int height, boolean down, String text) {
BufferedImage lookCache = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = lookCache.createGraphics();
g2.drawImage(topLeft, x, y, null);
g2.drawImage(topRight, x + width - topRight.getWidth(), y, null);
g2.drawImage(bottomLeft, x, y + height - bottomLeft.getHeight(), null);
g2.drawImage(bottomRight, x + width - bottomRight.getWidth(), y + height - bottomRight.getHeight(), null);
Paint save = g2.getPaint();
g2.setPaint(new TexturePaint(topCenter, new Rectangle(x, y, topCenter.getWidth(), topCenter.getHeight())));
g2.fillRect(x + topLeft.getWidth(), y, width - topLeft.getWidth() - topRight.getWidth(), topCenter.getHeight());
g2.setPaint(new TexturePaint(bottomCenter, new Rectangle(x, y + height - bottomLeft.getHeight(), bottomCenter.getWidth(), bottomCenter.getHeight())));
g2.fillRect(x + bottomLeft.getWidth(), y + height - bottomLeft.getHeight(),
width - bottomLeft.getWidth() - bottomRight.getWidth(), bottomCenter.getHeight());
g2.setPaint(new TexturePaint(leftMiddle, new Rectangle(x, y + topLeft.getHeight(), leftMiddle.getWidth(), leftMiddle.getHeight())));
g2.fillRect(x, y + topLeft.getHeight(), leftMiddle.getWidth(), height - topLeft.getHeight() - bottomLeft.getHeight());
g2.setPaint(new TexturePaint(rightMiddle, new Rectangle(x + width - rightMiddle.getWidth(), y + topLeft.getHeight(), rightMiddle.getWidth(), rightMiddle.getHeight())));
g2.fillRect(x + width - rightMiddle.getWidth(), y + topRight.getHeight(), rightMiddle.getWidth(), height - topRight.getHeight() - bottomRight.getHeight());
int dx = down ? 0 : -1;
GradientPaint grad = new GradientPaint(
new Point(x + leftMiddle.getWidth(), y + topLeft.getHeight() + dx),
new Color(0xFF4C7098),
new Point(x + leftMiddle.getWidth(), y + height - bottomLeft.getHeight()),
new Color(0xFF805B71)
);
g2.setPaint(grad);
g2.fillRect(x + leftMiddle.getWidth() + dx, y + topLeft.getHeight() + dx, width - leftMiddle.getWidth() - rightMiddle.getWidth() - 2 * dx, height - topCenter.getHeight() - bottomCenter.getHeight() - dx);
g2.setPaint(save);
g2.dispose();
g1.drawImage(lookCache, x, y, null);
FontMetrics fm = g1.getFontMetrics();
int tw = fm.stringWidth(text);
int th = fm.getHeight();
int tx = x + (width - tw) / 2 + dx;
int ty = y + (height - th) / 2 + dx;
Color textColor = g1.getColor();
g1.setColor(new Color(0x509090));
g1.drawString(text, tx + 1, ty + 1 + fm.getAscent());
g1.setColor(textColor);
g1.drawString(text, tx, ty + fm.getAscent());
if (down) {
g1.setColor(new Color(0, 0, 0, 92));
g1.fillRect(x + 3, y + 3, width - 5, 4);
g1.fillRect(x + 3, y + 7, 4, height - 9);
}
}
/**
* Selects a tick size that is appropriate for drawing the axis from
* {@code pt0} to {@code pt1}.
*
* @param g2 the graphics target ({@code null} not permitted).
* @param pt0 the starting point for the axis.
* @param pt1 the ending point for the axis.
* @param opposingPt a point on the opposite side of the line from where
* the labels should be drawn.
*/
@Override
public double selectTick(Graphics2D g2, Point2D pt0, Point2D pt1,
Point2D opposingPt) {
if (this.tickSelector == null) {
return this.tickSize;
}
g2.setFont(getTickLabelFont());
FontMetrics fm = g2.getFontMetrics(getTickLabelFont());
double length = pt0.distance(pt1);
LabelOrientation orientation = getTickLabelOrientation();
if (orientation.equals(LabelOrientation.PERPENDICULAR)) {
// based on the font height, we can determine roughly how many tick
// labels will fit in the length available
double height = fm.getHeight();
// the tickLabelFactor allows some control over how dense the labels
// will be
int maxTicks = (int) (length / (height * getTickLabelFactor()));
if (maxTicks > 2 && this.tickSelector != null) {
double rangeLength = getRange().getLength();
this.tickSelector.select(rangeLength / 2.0);
// step through until we have too many ticks OR we run out of
// tick sizes
int tickCount = (int) (rangeLength
/ this.tickSelector.getCurrentTickSize());
while (tickCount < maxTicks) {
this.tickSelector.previous();
tickCount = (int) (rangeLength
/ this.tickSelector.getCurrentTickSize());
}
this.tickSelector.next();
this.tickSize = this.tickSelector.getCurrentTickSize();
// TFE, 20180911: don't overwrite any formatter explicitly set
if (DEFAULT_TICK_LABEL_FORMATTER.equals(this.tickLabelFormatter)) {
this.tickLabelFormatter
= this.tickSelector.getCurrentTickLabelFormat();
}
} else {
this.tickSize = Double.NaN;
}
} else if (orientation.equals(LabelOrientation.PARALLEL)) {
// choose a unit that is at least as large as the length of the axis
this.tickSelector.select(getRange().getLength());
boolean done = false;
while (!done) {
if (this.tickSelector.previous()) {
// estimate the label widths, and do they overlap?
Format f = this.tickSelector.getCurrentTickLabelFormat();
String s0 = f.format(this.range.getMin());
String s1 = f.format(this.range.getMax());
double w0 = fm.stringWidth(s0);
double w1 = fm.stringWidth(s1);
double w = Math.max(w0, w1);
int n = (int) (length / (w * this.getTickLabelFactor()));
if (n < getRange().getLength()
/ tickSelector.getCurrentTickSize()) {
tickSelector.next();
done = true;
}
} else {
done = true;
}
}
this.tickSize = this.tickSelector.getCurrentTickSize();
// TFE, 20180911: don't overwrite any formatter explicitly set
if (DEFAULT_TICK_LABEL_FORMATTER.equals(this.tickLabelFormatter)) {
this.tickLabelFormatter
= this.tickSelector.getCurrentTickLabelFormat();
}
}
return this.tickSize;
}
protected String adjustString(String s, FontMetrics metrics) {
while (metrics.stringWidth(s) > ICON_NAME_MAX_LENGTH) {
s = s.substring(0, s.length() - 1);
}
return s;
}
@Override
public ProfileImage getProfileAvatarInitials(String userUuid) {
ProfileImage image = null;
if(cache.containsKey(userUuid)) {
image = (ProfileImage)cache.get(userUuid);
if (image == null) {
this.cacheManager.evictFromCache(this.cache, userUuid);
}
}
if (image == null) {
image = new ProfileImage();
BufferedImage bufferedImage = new BufferedImage(ProfileConstants.PROFILE_AVATAR_WIDTH, ProfileConstants.PROFILE_AVATAR_HEIGHT, BufferedImage.TYPE_INT_ARGB);
String displayName = sakaiProxy.getUserDisplayName(userUuid);
String[] names = displayName.split(" ");
String initials = "";
int fontSize;
int profileInitialsSize = Integer.parseInt(sakaiProxy.getServerConfigurationParameter("profile2.avatar.initials.size", "2"));
switch(profileInitialsSize){
case 1:
initials = Character.toString(names[0].charAt(0));
fontSize = Integer.parseInt(sakaiProxy.getServerConfigurationParameter("profile2.avatar.initials.font.size", ProfileConstants.DFLT_PROFILE_AVATAR_FONT_SIZE_1_CHAR));
break;
case 2:
default:
for (int i=0; i < names.length;i++) {
if (i > 1) break;
initials += Character.toString(names[i].charAt(0));
}
fontSize = Integer.parseInt(sakaiProxy.getServerConfigurationParameter("profile2.avatar.initials.font.size", ProfileConstants.DFLT_PROFILE_AVATAR_FONT_SIZE_2_CHAR));
break;
}
initials = initials.toUpperCase();
Graphics2D background = bufferedImage.createGraphics();
background.setPaint(Color.decode(this.getAvatarInitialsColor(displayName)));
background.fillRect(0, 0, ProfileConstants.PROFILE_AVATAR_WIDTH, ProfileConstants.PROFILE_AVATAR_HEIGHT);
String fontFamily = sakaiProxy.getServerConfigurationParameter("profile2.avatar.initials.font", ProfileConstants.DFLT_PROFILE_AVATAR_FONT_FAMILY);
Graphics2D initialsg2d = bufferedImage.createGraphics();
initialsg2d.setPaint(Color.WHITE);
initialsg2d.setFont(new Font(fontFamily, Font.PLAIN, fontSize));
FontMetrics fm = initialsg2d.getFontMetrics();
int x = (ProfileConstants.PROFILE_AVATAR_WIDTH/2) - fm.stringWidth(initials) / 2;
int y = (ProfileConstants.PROFILE_AVATAR_HEIGHT - fm.getHeight()) / 2 + fm.getAscent();
initialsg2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
initialsg2d.drawString(initials, x, y);
initialsg2d.dispose();
byte[] bytes = null;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()){
ImageIO.write(bufferedImage, "png", baos);
bytes = baos.toByteArray();
} catch (IOException ex) {
log.error("Cannot generate profile avatar for the user {}", userUuid);
}
if(bytes != null) {
image.setUploadedImage(bytes);
} else {
image.setExternalImageUrl(getUnavailableImageURL());
image.setDefault(true);
}
cache.put(userUuid, image);
}
return image;
}
/**
* Presents the Exceptions if the chart cannot be displayed properly.
*
* @param g2d
* @param ex
*/
private final void showException( Graphics2D g2d, Exception ex )
{
String sWrappedException = ex.getClass( ).getName( );
Throwable th = ex;
while ( ex.getCause( ) != null )
{
ex = (Exception) ex.getCause( );
}
String sException = ex.getClass( ).getName( );
if ( sWrappedException.equals( sException ) )
{
sWrappedException = null;
}
String sMessage = null;
if ( th instanceof BirtException )
{
sMessage = ( (BirtException) th ).getLocalizedMessage( );
}
else
{
sMessage = ex.getMessage( );
}
if ( sMessage == null )
{
sMessage = "<null>";//$NON-NLS-1$
}
StackTraceElement[] stea = ex.getStackTrace( );
Dimension d = getSize( );
Font fo = new Font( "Monospaced", Font.BOLD, 14 );//$NON-NLS-1$
g2d.setFont( fo );
FontMetrics fm = g2d.getFontMetrics( );
g2d.setColor( Color.WHITE );
g2d.fillRect( 20, 20, d.width - 40, d.height - 40 );
g2d.setColor( Color.BLACK );
g2d.drawRect( 20, 20, d.width - 40, d.height - 40 );
g2d.setClip( 20, 20, d.width - 40, d.height - 40 );
int x = 25, y = 20 + fm.getHeight( );
g2d.drawString( "Exception:", x, y );//$NON-NLS-1$
x += fm.stringWidth( "Exception:" ) + 5;//$NON-NLS-1$
g2d.setColor( Color.RED );
g2d.drawString( sException, x, y );
x = 25;
y += fm.getHeight( );
if ( sWrappedException != null )
{
g2d.setColor( Color.BLACK );
g2d.drawString( "Wrapped In:", x, y );//$NON-NLS-1$
x += fm.stringWidth( "Wrapped In:" ) + 5;//$NON-NLS-1$
g2d.setColor( Color.RED );
g2d.drawString( sWrappedException, x, y );
x = 25;
y += fm.getHeight( );
}
g2d.setColor( Color.BLACK );
y += 10;
g2d.drawString( "Message:", x, y );//$NON-NLS-1$
x += fm.stringWidth( "Message:" ) + 5;//$NON-NLS-1$
g2d.setColor( Color.BLUE );
g2d.drawString( sMessage, x, y );
x = 25;
y += fm.getHeight( );
g2d.setColor( Color.BLACK );
y += 10;
g2d.drawString( "Trace:", x, y );//$NON-NLS-1$
x = 40;
y += fm.getHeight( );
g2d.setColor( Color.GREEN.darker( ) );
for ( int i = 0; i < stea.length; i++ )
{
g2d.drawString( stea[i].getClassName( ) + ":"//$NON-NLS-1$
+ stea[i].getMethodName( ) + "(...):"//$NON-NLS-1$
+ stea[i].getLineNumber( ), x, y );
x = 40;
y += fm.getHeight( );
}
}
private void drawBreakSymbol(ColorBreak aCB, Rectangle rect, boolean selected, Graphics2D g) {
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
float aSize;
PointF aP = new PointF(0, 0);
float width, height;
aP.X = rect.x + rect.width / 2;
aP.Y = rect.y + rect.height / 2;
//Draw selected back color
if (selected) {
g.setColor(Color.lightGray);
g.fill(new Rectangle(_symbolWidth, rect.y, _valueWidth + _labelWidth, rect.height));
}
//Draw symbol
switch (aCB.getBreakType()) {
case PointBreak:
PointBreak aPB = (PointBreak) aCB;
aSize = aPB.getSize();
if (aPB.isDrawShape()) {
if (aPB.getMarkerType() == MarkerType.Character) {
Draw.drawPoint(aP, aPB, g);
} else {
Draw.drawPoint(aP, aPB, g);
}
}
break;
case PolylineBreak:
PolylineBreak aPLB = (PolylineBreak) aCB;
aSize = aPLB.getWidth();
width = rect.width / 3 * 2;
height = rect.height / 3 * 2;
Draw.drawPolylineSymbol(aP, width, height, aPLB, g);
break;
case PolygonBreak:
PolygonBreak aPGB = (PolygonBreak) aCB;
width = rect.width / 3 * 2;
height = rect.height / 5 * 4;
if (aPGB.isDrawShape()) {
Draw.drawPolygonSymbol(aP, width, height, aPGB, g);
}
break;
case ColorBreak:
width = rect.width / 3 * 2;
height = rect.height / 3 * 2;
Draw.drawPolygonSymbol(aP, aCB.getColor(), Color.black, width,
height, true, true, g);
break;
}
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
//Draw value and label
int sX = _symbolWidth;
Font aFont = new Font("Simsun", Font.PLAIN, 13);
String str = aCB.getCaption();
FontMetrics metrics = g.getFontMetrics(aFont);
Dimension size = new Dimension(metrics.stringWidth(str), metrics.getHeight());
aP.X = sX;
aP.Y = rect.y + rect.height * 3 / 4;
g.setFont(aFont);
g.setColor(Color.black);
if (_legendScheme.getLegendType() == LegendType.SingleSymbol) {
g.drawString(str, aP.X, aP.Y);
} else {
//Label
aP.X += _valueWidth;
g.drawString(str, aP.X, aP.Y);
//Value
if (String.valueOf(aCB.getStartValue()).equals(
String.valueOf(aCB.getEndValue()))) {
str = String.valueOf(aCB.getStartValue());
} else {
str = String.valueOf(aCB.getStartValue()) + " - " + String.valueOf(aCB.getEndValue());
}
//size = new Dimension(metrics.stringWidth(str), metrics.getHeight());
aP.X = sX;
Rectangle clip = g.getClipBounds();
g.clipRect(sX, rect.y, this._valueWidth - 5, rect.height);
g.drawString(str, aP.X, aP.Y);
g.setClip(clip.x, clip.y, clip.width, clip.height);
}
}
protected void drawCenteredString(String s, int x, int y, int w, int h, Graphics g) {
FontMetrics fm = g.getFontMetrics();
x += (w - fm.stringWidth(s)) / 2;
y += (fm.getAscent() + (h - (fm.getAscent() + fm.getDescent())) / 2);
g.drawString(s, x, y);
}
/**
* Returns the bounds for the specified text when it is drawn with the
* left-baseline aligned to the point <code>(x, y)</code>.
*
* @param text the text (<code>null</code> not permitted).
* @param x the x-coordinate.
* @param y the y-coordinate.
* @param fm the font metrics (<code>null</code> not permitted).
*
* @return The bounding rectangle (never <code>null</code>).
*/
public static Rectangle2D getTextBounds(String text, double x, double y,
FontMetrics fm) {
ParamChecks.nullNotPermitted(text, "text");
ParamChecks.nullNotPermitted(fm, "fm");
double width = fm.stringWidth(text);
double height = fm.getHeight();
return new Rectangle2D.Double(x, y - fm.getAscent(), width, height);
}