Note that there are some explanatory texts on larger screens.

plurals
  1. POAlign text to the right in a TextLayout, using Java Graphics2D API
    primarykey
    data
    text
    <p>So, I'm using the code in Java tutorial to draw a piece of text, but I don't know how to align text to the right margin.</p> <p>I just included <code>attstring.addAttribute(TextAttribute.RUN_DIRECTION, TextAttribute.RUN_DIRECTION_RTL);</code> in the code for that case but it doesn't work.</p> <pre><code>protected float drawParagraph (Graphics2D g2, String text, float width, float x, float y, Boolean dir){ AttributedString attstring = new AttributedString(text); attstring.addAttribute(TextAttribute.FONT, font); if (dir == TextAttribute.RUN_DIRECTION_RTL){ attstring.addAttribute(TextAttribute.RUN_DIRECTION, TextAttribute.RUN_DIRECTION_RTL); } AttributedCharacterIterator paragraph = attstring.getIterator(); int paragraphStart = paragraph.getBeginIndex(); int paragraphEnd = paragraph.getEndIndex(); FontRenderContext frc = g2.getFontRenderContext(); LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(paragraph, frc); // Set break width to width of Component. float breakWidth = width; float drawPosY = y; // Set position to the index of the first character in the paragraph. lineMeasurer.setPosition(paragraphStart); // Get lines until the entire paragraph has been displayed. while (lineMeasurer.getPosition() &lt; paragraphEnd) { // Retrieve next layout. A cleverer program would also cache // these layouts until the component is re-sized. TextLayout layout = lineMeasurer.nextLayout(breakWidth); // Compute pen x position. If the paragraph is right-to-left we // will align the TextLayouts to the right edge of the panel. // Note: drawPosX is always where the LEFT of the text is placed. float drawPosX = (float) (layout.isLeftToRight() ? x : breakWidth - layout.getAdvance()); // Move y-coordinate by the ascent of the layout. drawPosY += layout.getAscent(); // Draw the TextLayout at (drawPosX, drawPosY). layout.draw(g2, drawPosX, drawPosY); // Move y-coordinate in preparation for next layout. drawPosY += layout.getDescent() + layout.getLeading(); } return drawPosY; } </code></pre> <p>Give a hand please, I'm lost ^^</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload