Graphics2D Text Applet

We give the text a large font and rotate it. We could have rotated the text by -45 instead of +135 degrees with this instruction:

at_rotate := AffineTransform.getRotateInstance(-45 * Math.pi / 180, 30, height - 30);

The code follows a screenshot of the rotated text.

Rotated text

Rotated text

namespace text_demo;

interface

uses
  java.util, java.applet.*, java.awt.*;

type
  text_demo = public class(Applet)
  var
    ssFont : Font;
    aT, at_rotate : AffineTransform;
    str : string := 'Ascending';
    height : Integer;

  public
    method init; override;
    method paint(g :  Graphics); override;
  end;

implementation

method text_demo.init;
begin
  Background := Color.gray;
  height := getSize.height;
  ssFont := new Font('SansSerif', Font.BOLD, 40);
end;

method text_demo.paint(g : Graphics);
var
  g2 : Graphics2D;
begin
  g2 := Graphics2D(g);
  g2.setColor(Color.green);
  aT := g2.getTransform;  //save existing transform
  at_rotate := AffineTransform.getRotateInstance(315 * Math.pi / 180, 30, height - 30);
  g2.setTransform(at_rotate);
  g2.setColor(Color.green);
  g2.setFont(ssFont);
  g2.drawString(str,  30, height - 30);
  g2.setTransform(aT); //restore original transform
end;

end.


Programming - a skill for life!

Applet using Graphics2D for a rotation in Oxygene for Java