當前位置: 首頁>>代碼示例>>Java>>正文


Java TextLayout類代碼示例

本文整理匯總了Java中java.awt.font.TextLayout的典型用法代碼示例。如果您正苦於以下問題:Java TextLayout類的具體用法?Java TextLayout怎麽用?Java TextLayout使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


TextLayout類屬於java.awt.font包,在下文中一共展示了TextLayout類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: paint

import java.awt.font.TextLayout; //導入依賴的package包/類
@Override
public void paint(Graphics g) {
	Dimension d = vv.getSize();
	FontRenderContext frc = ((Graphics2D) g).getFontRenderContext();
	Font f = new Font("Times", Font.BOLD, 30);

	TextLayout tl = new TextLayout(str, f, frc);
	AffineTransform transform = new AffineTransform();
	transform.setToTranslation(d.width / 2, d.height / 2);
	transform.rotate(Math.toRadians(315));
	Shape shape = tl.getOutline(transform);
	g.translate(-shape.getBounds().width / 2,
			shape.getBounds().height / 2);
	g.setColor(Color.lightGray);
	((Graphics2D) g).draw(shape);
}
 
開發者ID:KeepTheBeats,項目名稱:alevin-svn2,代碼行數:17,代碼來源:LayerViewer.java

示例2: getPreferredSpan

import java.awt.font.TextLayout; //導入依賴的package包/類
@Override
public float getPreferredSpan(int axis) {
    if (axis == View.X_AXIS) {
        String desc = fold.getDescription(); // For empty desc a single-space text layout is returned
        float advance = 0;
        if (desc.length() > 0) {
            TextLayout textLayout = getTextLayout();
            if (textLayout == null) {
                return 0f;
            }
            advance = textLayout.getAdvance();
        }
        return advance + (2 * EXTRA_MARGIN_WIDTH);
    } else {
        EditorView.Parent parent = (EditorView.Parent) getParent();
        return (parent != null) ? parent.getViewRenderContext().getDefaultRowHeight() : 0f;
    }
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:19,代碼來源:FoldView.java

示例3: runTest

import java.awt.font.TextLayout; //導入依賴的package包/類
public void runTest(Object ctx, int numReps) {
    TLExContext tlctx = (TLExContext)ctx;
    TextLayout tl = tlctx.tl;
    TextHitInfo[] hits = tlctx.hits;
    Rectangle2D lb = tlctx.lb;
    Shape s;
    if (hits.length < 3) {
        do {
            s = tl.getVisualHighlightShape(hits[0], hits[hits.length - 1], lb);
        } while (--numReps >= 0);
    } else {
        do {
            for (int i = 3; i < hits.length; ++i) {
                s = tl.getVisualHighlightShape(hits[i-3], hits[i], lb);
            }
        } while (--numReps >= 0);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:19,代碼來源:TextMeasureTests.java

示例4: drawString

import java.awt.font.TextLayout; //導入依賴的package包/類
public void drawString(AttributedCharacterIterator iterator,
                       float x, float y) {
    if (iterator == null) {
        throw new NullPointerException("AttributedCharacterIterator is null");
    }
    if (iterator.getBeginIndex() == iterator.getEndIndex()) {
        return; /* nothing to draw */
    }
    TextLayout tl = new TextLayout(iterator, getFontRenderContext());
    tl.draw(this, x, y);
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:12,代碼來源:SunGraphics2D.java

示例5: getRealAlloc

import java.awt.font.TextLayout; //導入依賴的package包/類
/**
     * Get real allocation (possibly not rectangular) of a part of layout.
     * <br>
     * It's used when rendering the text layout for filling background highlights of the view.
     *
     * @param length Total number of characters for which the allocation is computed.
     * @param alloc Allocation given by a parent view.
     * @return
     */
    public static Shape getRealAlloc(TextLayout textLayout, Rectangle2D textLayoutRect,
            TextHitInfo startHit, TextHitInfo endHit)
    {
        // Quick-fix to eliminate missing line in rendering italic "d" - more elaborate fix is needed
        textLayoutRect = new Rectangle2D.Double(textLayoutRect.getX(), textLayoutRect.getY(),
                textLayoutRect.getWidth() + 2, textLayoutRect.getHeight());
        Rectangle2D.Double zeroBasedRect = ViewUtils.shape2Bounds(textLayoutRect);
        zeroBasedRect.x = 0;
        zeroBasedRect.y = 0;
        Shape ret = textLayout.getVisualHighlightShape(startHit, endHit, zeroBasedRect);
        AffineTransform transform = AffineTransform.getTranslateInstance(
                textLayoutRect.getX(),
                textLayoutRect.getY()
        );
        ret = transform.createTransformedShape(ret);
        // The following gives bad result for some reason (works for layout but not for caret modelToView())
//        Shape ret2 = textLayout.getVisualHighlightShape(startHit.getCharIndex(), endHit.getCharIndex(), textLayoutRect);
        return ret;
    }
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:29,代碼來源:TextLayoutUtils.java

示例6: createPartTextLayout

import java.awt.font.TextLayout; //導入依賴的package包/類
TextLayout createPartTextLayout(int shift, int length) {
    checkTextLayoutValid();
    if (breakInfo == null) {
        breakInfo = new TextLayoutBreakInfo(textLayout.getCharacterCount());
    }
    TextLayout partTextLayout = breakInfo.findPartTextLayout(shift, length);
    if (partTextLayout == null) {
        DocumentView docView = getDocumentView();
        Document doc = docView.getDocument();
        CharSequence docText = DocumentUtilities.getText(doc);
        int startOffset = getStartOffset();
        String text = docText.subSequence(startOffset + shift, startOffset + shift + length).toString();
        if (docView.op.isNonPrintableCharactersVisible()) {
            text = text.replace(' ', DocumentViewOp.PRINTING_SPACE);
        }
        AttributeSet attrs = ViewUtils.getFirstAttributes(getAttributes());
        Font font = ViewUtils.getFont(attrs, docView.op.getDefaultFont());
        partTextLayout = docView.op.createTextLayout(text, font);
        breakInfo.add(shift, length, partTextLayout);
    }
    return partTextLayout;
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:23,代碼來源:HighlightsView.java

示例7: indexToView

import java.awt.font.TextLayout; //導入依賴的package包/類
static Shape indexToView(TextLayout textLayout, Rectangle2D textLayoutBounds,
            int index, Position.Bias bias, int maxIndex, Shape alloc)
   {
       if (textLayout == null) {
           return alloc; // Leave given bounds
       }
       assert (maxIndex <= textLayout.getCharacterCount()) : "textLayout.getCharacterCount()=" + // NOI18N
               textLayout.getCharacterCount() + " < maxIndex=" + maxIndex; // NOI18N
       // If offset is >getEndOffset() use view-end-offset - otherwise it would throw exception from textLayout.getCaretInfo()
int charIndex = Math.min(index, maxIndex);
       // When e.g. creating fold-preview the offset can be < startOffset
       charIndex = Math.max(charIndex, 0);
       TextHitInfo startHit;
       TextHitInfo endHit;
       if (bias == Position.Bias.Forward) {
           startHit = TextHitInfo.leading(charIndex);
       } else { // backward bias
           startHit = TextHitInfo.trailing(charIndex - 1);
       }
       endHit = (charIndex < maxIndex) ? TextHitInfo.trailing(charIndex) : startHit;
       if (textLayoutBounds == null) {
           textLayoutBounds = ViewUtils.shapeAsRect(alloc);
       }
       return TextLayoutUtils.getRealAlloc(textLayout, textLayoutBounds, startHit, endHit);
   }
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:26,代碼來源:HighlightsViewUtils.java

示例8: getTextLayout

import java.awt.font.TextLayout; //導入依賴的package包/類
private TextLayout getTextLayout() {
    if (collapsedTextLayout == null) {
        EditorView.Parent parent = (EditorView.Parent) getParent();
        ViewRenderContext context = parent.getViewRenderContext();
        FontRenderContext frc = context.getFontRenderContext();
        assert (frc != null) : "Null FontRenderContext"; // NOI18N
        Font font = context.getRenderFont(component.getFont());
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < indent; i++) {
            sb.append(' ');
        }
        sb.append("...");
        String text = sb.toString();
        collapsedTextLayout = new TextLayout(text, font, frc);
    }
    return collapsedTextLayout;
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:18,代碼來源:SkipLinesViewFactory.java

示例9: main

import java.awt.font.TextLayout; //導入依賴的package包/類
public static void main(String args[]) {

        Font font = new Font(Font.DIALOG, Font.PLAIN, 12);
        String text = "hello world";
        FontRenderContext frc = new FontRenderContext(null, false, false);
        TextLayout tl1 = new TextLayout(text, font, frc);
        TextLayout tl2 = new TextLayout(text, font, frc);
        if (tl1.equals(tl2) ||
            tl2.equals(tl1) ||
            tl1.equals((Object)tl2) ||
            tl2.equals((Object)tl1))
        {
             throw new RuntimeException("Equal TextLayouts");
        }
        if (!tl1.equals(tl1) ||
            !tl1.equals((Object)tl1))
        {
             throw new RuntimeException("Non-Equal TextLayouts");
        }
    }
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:21,代碼來源:TextLayoutEqualsTest.java

示例10: drawStringUsingShapes

import java.awt.font.TextLayout; //導入依賴的package包/類
private void drawStringUsingShapes(AttributedCharacterIterator iterator, float x, float y) {
	Stroke originalStroke = stroke;
	Paint originalPaint = paint;
	TextLayout textLayout = new TextLayout(iterator, getFontRenderContext());
	textLayout.draw(this, x, y);
	paint = originalPaint;
	stroke = originalStroke;
}
 
開發者ID:rototor,項目名稱:pdfbox-graphics2d,代碼行數:9,代碼來源:PdfBoxGraphics2D.java

示例11: getPreferredSize

import java.awt.font.TextLayout; //導入依賴的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));

}
 
開發者ID:wintertime,項目名稱:FreeCol,代碼行數:32,代碼來源:FreeColToolTipUI.java

示例12: runTest

import java.awt.font.TextLayout; //導入依賴的package包/類
public void runTest(Object ctx, int numReps) {
    TLExContext tlctx = (TLExContext)ctx;
    TextLayout tl = tlctx.tl;
    int len = tlctx.text.length();
    Rectangle2D lb = tlctx.lb;
    Shape s;
    if (len < 3) {
        do {
            s = tl.getLogicalHighlightShape(0, len, lb);
        } while (--numReps >= 0);
    } else {
        do {
            for (int i = 3; i < len; ++i) {
                s = tl.getLogicalHighlightShape(i-3, i, lb);
            }
        } while (--numReps >= 0);
    }
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:19,代碼來源:TextMeasureTests.java

示例13: getCaretRectangle

import java.awt.font.TextLayout; //導入依賴的package包/類
private Rectangle getCaretRectangle(TextHitInfo caret) {
    int caretLocation = 0;
    TextLayout layout = composedTextLayout;
    if (layout != null) {
        caretLocation = Math.round(layout.getCaretInfo(caret)[0]);
    }
    Graphics g = getGraphics();
    FontMetrics metrics = null;
    try {
        metrics = g.getFontMetrics();
    } finally {
        g.dispose();
    }
    return new Rectangle(TEXT_ORIGIN_X + caretLocation,
                         TEXT_ORIGIN_Y - metrics.getAscent(),
                         0, metrics.getAscent() + metrics.getDescent());
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:18,代碼來源:CompositionArea.java

示例14: render

import java.awt.font.TextLayout; //導入依賴的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();
  }
}
 
開發者ID:gurkenlabs,項目名稱:litiengine,代碼行數:30,代碼來源:SpeechBubble.java

示例15: computeDimensions

import java.awt.font.TextLayout; //導入依賴的package包/類
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;
}
 
開發者ID:LogisimIt,項目名稱:Logisim,代碼行數:29,代碼來源:EditableLabel.java


注:本文中的java.awt.font.TextLayout類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。