求人不如求己,我已经搞定: public void DrawString( Graphics2D g, Rectangle rc, String s, Color bkcolor, Color textcolor, String FontName, int FontSize, boolean FontBold, boolean FontItalic, boolean FontUnderline, int hAlign, int vAlign, boolean MultiLine) { if (s.trim().equals("")) return; if ( rc.width<=0 || rc.height<=0 ) return ;
//背景颜色 g.setColor(bkcolor); g.fillRect(rc.x, rc.y, rc.width, rc.height); g.setColor(textcolor); //文字颜色 LineBreakMeasurer lineMeasurer; //The LineBreakMeasurer used to line-break the paragraph. int paragraphStart; // The index in the LineBreakMeasurer of the first character in the paragraph. int paragraphEnd; // The index in the LineBreakMeasurer of the first character after the end of the paragraph. Dimension size = rc.getSize(); //保存旧的剪裁区 Rectangle oc = g.getClipBounds(); //设置新得剪裁区 g.setClip(rc.x, rc.y, rc.width, rc.height); Hashtable map = new Hashtable(); map.put(TextAttribute.FAMILY, FontName); map.put(TextAttribute.SIZE, new Float(FontSize)); if (FontUnderline) map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); if (FontBold) map.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD); if (FontItalic) map.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE); AttributedString ss = new AttributedString(s, map); AttributedCharacterIterator text = ss.getIterator(); FontRenderContext frc = new FontRenderContext(g.getTransform(), false, false); paragraphStart = text.getBeginIndex(); paragraphEnd = text.getEndIndex(); // Create a new LineBreakMeasurer from the paragraph. lineMeasurer = new LineBreakMeasurer(text, frc); // Set formatting width to width of Component. float formatWidth = (float) size.width; float drawPosY = rc.y; // 多行时,计算文本总高度 if (MultiLine && vAlign != Alignment.Top) { double gg = 0; lineMeasurer.setPosition(paragraphStart); while (lineMeasurer.getPosition() < paragraphEnd) { TextLayout layout = lineMeasurer.nextLayout(formatWidth); gg += layout.getDescent() + layout.getLeading() + layout.getAscent(); } if (gg < rc.height) { if (vAlign == Alignment.Center) drawPosY += (rc.height - gg) / 2; if (vAlign == Alignment.Bottom) drawPosY += (rc.height - gg); } } // Get lines from lineMeasurer until the entire // paragraph has been displayed. lineMeasurer.setPosition(paragraphStart); while (lineMeasurer.getPosition() < paragraphEnd) { // Retrieve next layout. TextLayout layout = lineMeasurer.nextLayout(formatWidth); // Move y-coordinate by the ascent of the layout. switch (vAlign) { case Alignment.Bottom : //底边 if (!MultiLine) { drawPosY += (rc.height - layout.getDescent() - layout.getLeading()); break; } case Alignment.Center : //居中 if (!MultiLine) { drawPosY += layout.getAscent() + (rc.height - layout.getAscent() - layout.getDescent() - layout.getLeading()) / 2; break; } default : drawPosY += layout.getAscent(); break; } float drawPosX = rc.x; switch (hAlign) { case Alignment.Right : //右对齐 drawPosX = rc.x + formatWidth - layout.getAdvance(); break; case Alignment.Center : // 居中对齐 drawPosX = rc.x + (formatWidth - layout.getAdvance()) / 2; break; default : drawPosX = rc.x; break; } layout.draw(g, drawPosX, drawPosY); // Move y-coordinate in preparation for next layout. drawPosY += layout.getDescent() + layout.getLeading(); //如果多行文本超出了显示区域,那么退出 if (drawPosY >= rc.y + rc.height) break; if (!MultiLine) //如果不是多行,那么退出 break; } //恢复旧的裁减区域 g.setClip(oc.x, oc.y, oc.width, oc.height); }
public void DrawString(
Graphics2D g,
Rectangle rc,
String s,
Color bkcolor,
Color textcolor,
String FontName,
int FontSize,
boolean FontBold,
boolean FontItalic,
boolean FontUnderline,
int hAlign,
int vAlign,
boolean MultiLine)
{
if (s.trim().equals(""))
return; if ( rc.width<=0 || rc.height<=0 ) return ;
//背景颜色
g.setColor(bkcolor);
g.fillRect(rc.x, rc.y, rc.width, rc.height);
g.setColor(textcolor); //文字颜色 LineBreakMeasurer lineMeasurer; //The LineBreakMeasurer used to line-break the paragraph.
int paragraphStart; // The index in the LineBreakMeasurer of the first character in the paragraph.
int paragraphEnd; // The index in the LineBreakMeasurer of the first character after the end of the paragraph. Dimension size = rc.getSize(); //保存旧的剪裁区
Rectangle oc = g.getClipBounds();
//设置新得剪裁区
g.setClip(rc.x, rc.y, rc.width, rc.height);
Hashtable map = new Hashtable();
map.put(TextAttribute.FAMILY, FontName);
map.put(TextAttribute.SIZE, new Float(FontSize));
if (FontUnderline)
map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
if (FontBold)
map.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
if (FontItalic)
map.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE); AttributedString ss = new AttributedString(s, map);
AttributedCharacterIterator text = ss.getIterator();
FontRenderContext frc = new FontRenderContext(g.getTransform(), false, false); paragraphStart = text.getBeginIndex();
paragraphEnd = text.getEndIndex(); // Create a new LineBreakMeasurer from the paragraph.
lineMeasurer = new LineBreakMeasurer(text, frc);
// Set formatting width to width of Component. float formatWidth = (float) size.width;
float drawPosY = rc.y; // 多行时,计算文本总高度
if (MultiLine && vAlign != Alignment.Top)
{
double gg = 0;
lineMeasurer.setPosition(paragraphStart);
while (lineMeasurer.getPosition() < paragraphEnd)
{
TextLayout layout = lineMeasurer.nextLayout(formatWidth);
gg += layout.getDescent() + layout.getLeading() + layout.getAscent();
} if (gg < rc.height)
{
if (vAlign == Alignment.Center)
drawPosY += (rc.height - gg) / 2;
if (vAlign == Alignment.Bottom)
drawPosY += (rc.height - gg);
} }
// Get lines from lineMeasurer until the entire
// paragraph has been displayed.
lineMeasurer.setPosition(paragraphStart);
while (lineMeasurer.getPosition() < paragraphEnd)
{
// Retrieve next layout.
TextLayout layout = lineMeasurer.nextLayout(formatWidth);
// Move y-coordinate by the ascent of the layout.
switch (vAlign)
{
case Alignment.Bottom : //底边
if (!MultiLine)
{
drawPosY += (rc.height - layout.getDescent() - layout.getLeading());
break;
}
case Alignment.Center : //居中
if (!MultiLine)
{
drawPosY += layout.getAscent() + (rc.height - layout.getAscent() - layout.getDescent() - layout.getLeading()) / 2;
break;
}
default :
drawPosY += layout.getAscent();
break;
} float drawPosX = rc.x;
switch (hAlign)
{
case Alignment.Right : //右对齐
drawPosX = rc.x + formatWidth - layout.getAdvance();
break;
case Alignment.Center : // 居中对齐
drawPosX = rc.x + (formatWidth - layout.getAdvance()) / 2;
break;
default :
drawPosX = rc.x;
break;
} layout.draw(g, drawPosX, drawPosY);
// Move y-coordinate in preparation for next layout.
drawPosY += layout.getDescent() + layout.getLeading();
//如果多行文本超出了显示区域,那么退出
if (drawPosY >= rc.y + rc.height)
break; if (!MultiLine) //如果不是多行,那么退出
break;
}
//恢复旧的裁减区域
g.setClip(oc.x, oc.y, oc.width, oc.height); }