Text Alignment

The alignment of paragraph text can be set to:

  • Left - left bound text

  • Right - right bound text, with added blanks left of the text to create the right alignment impression

  • Center - centered text, with added blanks left and right of the text to create the centered impression

  • Justified - justified text, with added blanks inside the text to create the justified impression

Justified alignment has two further specialized options:

  • Justified left - the last line of justified text will be aligned left

  • Justified right - the last line of justified text will be aligned right, with added blanks left of the text to create the right alignment impression

The following code creates a paragraph context and then a paragraph with some demo text. To demonstrate the text alignment, we then set the alignment in the context to justified, justified left, justified right, centered, left, and right and print the rendered text.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
AP_Context ctx = new AP_Context().setWidth(39);

AsciiParagraph ap = new AsciiParagraph(ctx);
ap.addText(new LoremIpsum().getWords(29));

ctx.setAlignment(TextAlignment.JUSTIFIED);
System.out.println(ap.render());

ctx.setAlignment(TextAlignment.JUSTIFIED_LEFT);
System.out.println(ap.render());

ctx.setAlignment(TextAlignment.JUSTIFIED_RIGHT);
System.out.println(ap.render());

ctx.setAlignment(TextAlignment.CENTER);
System.out.println(ap.render());

ctx.setAlignment(TextAlignment.LEFT);
System.out.println(ap.render());

ctx.setAlignment(TextAlignment.RIGHT);
System.out.println(ap.render());

The output for the first alignment, justified, is:

Lorem ipsum dolor sit amet,  consetetur
sadipscing  elitr,  sed   diam   nonumy
eirmod tempor  invidunt  ut  labore  et
dolore magna aliquyam  erat,  sed  diam
voluptua.  At  vero  eos   et   accusam

The output for the second alignment, justified left, is:

Lorem ipsum dolor sit amet,  consetetur
sadipscing  elitr,  sed   diam   nonumy
eirmod tempor  invidunt  ut  labore  et
dolore magna aliquyam  erat,  sed  diam
voluptua. At vero eos et accusam

The output for the third alignment, justified right, is:

Lorem ipsum dolor sit amet,  consetetur
sadipscing  elitr,  sed   diam   nonumy
eirmod tempor  invidunt  ut  labore  et
dolore magna aliquyam  erat,  sed  diam
       voluptua. At vero eos et accusam

The output for the fourth alignment, centered, is:

Lorem ipsum dolor sit amet, consetetur
   sadipscing elitr, sed diam nonumy
  eirmod tempor invidunt ut labore et
 dolore magna aliquyam erat, sed diam
   voluptua. At vero eos et accusam

The output for the fifth alignment, left, is:

Lorem ipsum dolor sit amet, consetetur
sadipscing elitr, sed diam nonumy
eirmod tempor invidunt ut labore et
dolore magna aliquyam erat, sed diam
voluptua. At vero eos et accusam

The output for the sixth alignment, right, is:

 Lorem ipsum dolor sit amet, consetetur
      sadipscing elitr, sed diam nonumy
    eirmod tempor invidunt ut labore et
   dolore magna aliquyam erat, sed diam
       voluptua. At vero eos et accusam