本文整理匯總了Java中java.awt.font.LineBreakMeasurer.nextLayout方法的典型用法代碼示例。如果您正苦於以下問題:Java LineBreakMeasurer.nextLayout方法的具體用法?Java LineBreakMeasurer.nextLayout怎麽用?Java LineBreakMeasurer.nextLayout使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.awt.font.LineBreakMeasurer
的用法示例。
在下文中一共展示了LineBreakMeasurer.nextLayout方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getPreferredSize
import java.awt.font.LineBreakMeasurer; //導入方法依賴的package包/類
@Override
public Dimension getPreferredSize(JComponent c) {
String tipText = ((JToolTip)c).getTipText();
if (tipText == null || tipText.isEmpty()) {
return new Dimension(0, 0);
}
float x = 0f;
float y = 0f;
for (String line : lineBreak.split(tipText)) {
if (line.isEmpty()) {
y += LEADING;
continue;
}
AttributedCharacterIterator styledText
= new AttributedString(line).getIterator();
LineBreakMeasurer measurer = new LineBreakMeasurer(styledText, frc);
while (measurer.getPosition() < styledText.getEndIndex()) {
TextLayout layout = measurer.nextLayout(maximumWidth);
x = Math.max(x, layout.getVisibleAdvance());
y += layout.getAscent() + layout.getDescent() + layout.getLeading();
}
}
return new Dimension((int) (x + 2 * margin),
(int) (y + 2 * margin));
}
示例2: drawTextInBoundedArea
import java.awt.font.LineBreakMeasurer; //導入方法依賴的package包/類
private static void drawTextInBoundedArea(Graphics2D g2d, int x1, int y1,
int x2, int y2, String text) {
float interline = 1;
float width = x2 - x1;
AttributedString as = new AttributedString(text);
as.addAttribute(TextAttribute.FOREGROUND, g2d.getPaint());
as.addAttribute(TextAttribute.FONT, g2d.getFont());
AttributedCharacterIterator aci = as.getIterator();
FontRenderContext frc = new FontRenderContext(null, true, false);
LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
while (lbm.getPosition() < text.length()) {
TextLayout tl = lbm.nextLayout(width);
y1 += tl.getAscent();
tl.draw(g2d, x1, y1);
y1 += tl.getDescent() + tl.getLeading() + (interline - 1.0f) * tl.
getAscent();
if (y1 > y2) {
break;
}
}
}
示例3: pixelDataFromString
import java.awt.font.LineBreakMeasurer; //導入方法依賴的package包/類
private final static void pixelDataFromString(int width, int height, String str, java.awt.Font font, int[] pixels, LineBreakMeasurer measurer) {
measurer.setPosition(0);
g2d.clearRect(0, 0, width, height);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
float wrapping_width = width;
float y = 0;
int length = str.length();
while (measurer.getPosition() < length) {
TextLayout layout = measurer.nextLayout(wrapping_width);
y += (layout.getAscent());
float x = layout.isLeftToRight() ? 0 : (wrapping_width - layout.getAdvance());
layout.draw(g2d, x, y);
y += layout.getDescent() + layout.getLeading();
}
image.getRaster().getDataElements(0, 0, image.getWidth(), image.getHeight(), pixels);
}
示例4: nextLayout
import java.awt.font.LineBreakMeasurer; //導入方法依賴的package包/類
/**
* @param wrappingWidth width of the layout
* @param frCtx for possible new instance of LineBreakMeasurer
* @return next TextLayout that is to be rendered
*/
private TextLayout nextLayout(float wrappingWidth, FontRenderContext frc) {
TextLayout l;
if (currentLayout == textLayouts.size()) {
LineBreakMeasurer old = lineBreakMeasurer;
LineBreakMeasurer measurer = getMeasurer(frc);
if (measurer == null) {
return null;
}
l = measurer.nextLayout(wrappingWidth);
textLayouts.add(l);
if (old != measurer) { // new line
startLayouts.add(l);
}
} else {
l = (TextLayout) textLayouts.get(currentLayout);
}
currentLayout++; // advance to next
return l;
}
示例5: drawText
import java.awt.font.LineBreakMeasurer; //導入方法依賴的package包/類
private void drawText(Graphics2D g2, String text, float width) {
FontRenderContext frc = g2.getFontRenderContext();
AttributedString styledText = new AttributedString(text);
AttributedCharacterIterator aci = styledText.getIterator();
int start = aci.getBeginIndex();
int end = aci.getEndIndex();
LineBreakMeasurer measurer = new LineBreakMeasurer(aci, frc);
measurer.setPosition(start);
float x = 0, y = 0;
while (measurer.getPosition() < end) {
TextLayout layout = measurer.nextLayout(width);
y += layout.getAscent();
float dx = layout.isLeftToRight() ? 0 : width - layout.getAdvance();
layout.draw(g2, x + dx, y);
y += layout.getDescent() + layout.getLeading();
}
}
示例6: wrapLines
import java.awt.font.LineBreakMeasurer; //導入方法依賴的package包/類
private String wrapLines(String s, float wrappingWidth, FontRenderContext frc, String lineBreak) {
AttributedCharacterIterator aci = new AttributedString(s).getIterator();
int end = aci.getEndIndex();
LineBreakMeasurer measurer = new LineBreakMeasurer(aci, frc);
StringBuffer output = new StringBuffer();
int position = 0;
int lastPosition = 0;
do {
measurer.nextLayout(wrappingWidth);
position = measurer.getPosition();
output.append(s, lastPosition, position);
output.append(lineBreak);
lastPosition = position;
} while (position < end);
return output.toString();
}
示例7: getPreferredSize
import java.awt.font.LineBreakMeasurer; //導入方法依賴的package包/類
public Dimension getPreferredSize(JComponent c) {
String tipText = ((JToolTip)c).getTipText();
if (tipText == null) {
return new Dimension(0, 0);
}
float x = 0f;
float y = 0f;
for (String line : lineBreak.split(((JToolTip) c).getTipText())) {
AttributedCharacterIterator styledText
= new AttributedString(line).getIterator();
LineBreakMeasurer measurer = new LineBreakMeasurer(styledText, frc);
while (measurer.getPosition() < styledText.getEndIndex()) {
TextLayout layout = measurer.nextLayout(maximumWidth);
x = Math.max(x, layout.getVisibleAdvance());
y += layout.getAscent() + layout.getDescent() + layout.getLeading();
}
}
return new Dimension((int) (x + 2 * margin),
(int) (y + 2 * margin));
}
示例8: countLines
import java.awt.font.LineBreakMeasurer; //導入方法依賴的package包/類
private static int countLines(JTextArea textArea)
{
AttributedString text = new AttributedString(textArea.getText());
FontRenderContext frc = textArea.getFontMetrics(textArea.getFont()).getFontRenderContext();
AttributedCharacterIterator charIt = text.getIterator();
LineBreakMeasurer lbm = new LineBreakMeasurer(charIt, frc);
float formatWidth = (float) textArea.getSize().width;
lbm.setPosition(charIt.getBeginIndex());
int numLines =0;
while (lbm.getPosition()<charIt.getEndIndex())
{
lbm.nextLayout(formatWidth);
numLines++;
}
return numLines;
}
示例9: paintOrGetSize
import java.awt.font.LineBreakMeasurer; //導入方法依賴的package包/類
private Dimension paintOrGetSize(Graphics2D g, int width) {
Insets insets = getInsets();
width -= insets.left + insets.right + margin.left + margin.right;
float w = insets.left + insets.right + margin.left + margin.right;
float x = insets.left + margin.left, y = insets.top + margin.top;
if (width > 0 && text != null && text.length() > 0) {
AttributedString as = new AttributedString(getText());
as.addAttribute(TextAttribute.FONT, getFont());
AttributedCharacterIterator aci = as.getIterator();
LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
float max = 0;
while (lbm.getPosition() < aci.getEndIndex()) {
TextLayout textLayout = lbm.nextLayout(width);
if (g != null && isJustified() && textLayout.getVisibleAdvance() > 0.80 * width)
textLayout = textLayout.getJustifiedLayout(width);
if (g != null)
textLayout.draw(g, x, y + textLayout.getAscent());
y += textLayout.getDescent() + textLayout.getLeading() + textLayout.getAscent();
max = Math.max(max, textLayout.getVisibleAdvance());
}
w += max;
}
return new Dimension((int)Math.ceil(w), (int)Math.ceil(y) + insets.bottom + margin.bottom);
}
示例10: render
import java.awt.font.LineBreakMeasurer; //導入方法依賴的package包/類
@Override
public void render(final Graphics2D g) {
if (this.displayedText == null || this.displayedText.isEmpty() || !Game.getRenderEngine().canRender(this.entity)) {
return;
}
final Point2D location = Game.getCamera().getViewPortLocation(this.entity);
RenderEngine.renderImage(g, this.bubble, new Point2D.Double(location.getX() + this.entity.getWidth() / 2.0 - this.textBoxWidth / 2.0 - PADDING, location.getY() - this.height - PADDING));
g.setColor(SPEAK_FONT_COLOR);
final FontRenderContext frc = g.getFontRenderContext();
final String text = this.displayedText;
final AttributedString styledText = new AttributedString(text);
styledText.addAttribute(TextAttribute.FONT, this.font);
final AttributedCharacterIterator iterator = styledText.getIterator();
final LineBreakMeasurer measurer = new LineBreakMeasurer(iterator, frc);
measurer.setPosition(0);
final float x = (float) Game.getCamera().getViewPortLocation(this.entity).getX() + this.entity.getWidth() / 2.0f - this.textBoxWidth / 2.0f;
float y = (float) Game.getCamera().getViewPortLocation(this.entity).getY() - this.height;
while (measurer.getPosition() < text.length()) {
final TextLayout layout = measurer.nextLayout(this.textBoxWidth);
y += layout.getAscent();
final float dx = layout.isLeftToRight() ? 0 : this.textBoxWidth - layout.getAdvance();
layout.draw(g, x + dx, y);
y += layout.getDescent() + layout.getLeading();
}
}
示例11: paint
import java.awt.font.LineBreakMeasurer; //導入方法依賴的package包/類
@Override
public void paint(Graphics g, JComponent c) {
if (c.isOpaque()) {
ImageLibrary.drawTiledImage("image.background.FreeColToolTip", g, c, null);
}
g.setColor(Color.BLACK); // FIXME: find out why this is necessary
Graphics2D graphics = (Graphics2D)g;
float x = margin;
float y = margin;
for (String line : lineBreak.split(((JToolTip) c).getTipText())) {
if (line.isEmpty()) {
y += LEADING;
continue;
}
AttributedCharacterIterator styledText =
new AttributedString(line).getIterator();
LineBreakMeasurer measurer = new LineBreakMeasurer(styledText, frc);
while (measurer.getPosition() < styledText.getEndIndex()) {
TextLayout layout = measurer.nextLayout(maximumWidth);
y += (layout.getAscent());
float dx = layout.isLeftToRight() ?
0 : (maximumWidth - layout.getAdvance());
layout.draw(graphics, x + dx, y);
y += layout.getDescent() + layout.getLeading();
}
}
}
示例12: test
import java.awt.font.LineBreakMeasurer; //導入方法依賴的package包/類
public void test() {
// construct a paragraph as follows: MIXED + [SPACING + WORD] + ...
StringBuffer text = new StringBuffer(MIXED);
for (int i=0; i < NUM_WORDS; i++) {
text.append(SPACING);
text.append(WORD);
}
AttributedString attrString = new AttributedString(text.toString());
attrString.addAttribute(TextAttribute.SIZE, new Float(24.0));
LineBreakMeasurer measurer = new LineBreakMeasurer(attrString.getIterator(),
DEFAULT_FRC);
// get width of a space-word sequence, in context
int sequenceLength = WORD.length()+SPACING.length();
measurer.setPosition(text.length() - sequenceLength);
TextLayout layout = measurer.nextLayout(10000.0f);
if (layout.getCharacterCount() != sequenceLength) {
throw new Error("layout length is incorrect!");
}
final float sequenceAdvance = layout.getVisibleAdvance();
float wrappingWidth = sequenceAdvance * 2;
// now run test with a variety of widths
while (wrappingWidth < (sequenceAdvance*NUM_WORDS)) {
measurer.setPosition(0);
checkMeasurer(measurer,
wrappingWidth,
sequenceAdvance,
text.length());
wrappingWidth += sequenceAdvance / 5;
}
}
示例13: checkMeasurer
import java.awt.font.LineBreakMeasurer; //導入方法依賴的package包/類
/**
* Iterate through measurer and check that every line is
* not too long and not too short, but just right.
*/
private void checkMeasurer(LineBreakMeasurer measurer,
float wrappingWidth,
float sequenceAdvance,
int endPosition) {
do {
TextLayout layout = measurer.nextLayout(wrappingWidth);
float visAdvance = layout.getVisibleAdvance();
// Check that wrappingWidth-sequenceAdvance < visAdvance
// Also, if we're not at the end of the paragraph,
// check that visAdvance <= wrappingWidth
if (visAdvance > wrappingWidth) {
// line is too long for given wrapping width
throw new Error("layout is too long");
}
if (measurer.getPosition() < endPosition) {
if (visAdvance <= wrappingWidth - sequenceAdvance) {
// line is too short for given wrapping width;
// another word would have fit
throw new Error("room for more words on line. diff=" +
(wrappingWidth - sequenceAdvance - visAdvance));
}
}
} while (measurer.getPosition() != endPosition);
}
示例14: getWordWrappedTextDimension
import java.awt.font.LineBreakMeasurer; //導入方法依賴的package包/類
/**
* Returns the {@link Dimension} for the given {@link JTextComponent}
* subclass that will show the whole word wrapped text in the given width.
* It won't work for styled text of varied size or style, it's assumed that
* the whole text is rendered with the {@link JTextComponent}s font.
*
* @param textComponent the {@link JTextComponent} to calculate the {@link Dimension} for
* @param width the width of the resulting {@link Dimension}
* @param text the {@link String} which should be word wrapped
* @return The calculated {@link Dimension}
*/
public static Dimension getWordWrappedTextDimension(JTextComponent textComponent, int width, String text) {
if (textComponent == null) {
throw new IllegalArgumentException("textComponent cannot be null");
}
if (width < 1) {
throw new IllegalArgumentException("width must be 1 or greater");
}
if (text == null) {
text = textComponent.getText();
}
if (text.isEmpty()) {
return new Dimension(width, 0);
}
FontMetrics metrics = textComponent.getFontMetrics(textComponent.getFont());
FontRenderContext rendererContext = metrics.getFontRenderContext();
float formatWidth = width - textComponent.getInsets().left - textComponent.getInsets().right;
int lines = 0;
String[] paragraphs = text.split("\n");
for (String paragraph : paragraphs) {
if (paragraph.isEmpty()) {
lines++;
} else {
AttributedString attributedText = new AttributedString(paragraph);
attributedText.addAttribute(TextAttribute.FONT, textComponent.getFont());
AttributedCharacterIterator charIterator = attributedText.getIterator();
LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(charIterator, rendererContext);
lineMeasurer.setPosition(charIterator.getBeginIndex());
while (lineMeasurer.getPosition() < charIterator.getEndIndex()) {
lineMeasurer.nextLayout(formatWidth);
lines++;
}
}
}
return new Dimension(width, metrics.getHeight() * lines + textComponent.getInsets().top + textComponent.getInsets().bottom);
}
示例15: extractLineBreaks
import java.awt.font.LineBreakMeasurer; //導入方法依賴的package包/類
private ArrayList extractLineBreaks(final AttributedCharacterIterator itr, final LineBreakMeasurer measurer) {
ArrayList breakList;
breakList = new ArrayList();
while (measurer.getPosition() < itr.getEndIndex()) {
if (constrainWidthToTextWidth) {
measurer.nextLayout(Float.MAX_VALUE);
}
else {
measurer.nextLayout((float) Math.ceil(getWidth() - insets.left - insets.right));
}
breakList.add(new Integer(measurer.getPosition()));
}
return breakList;
}