• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:Fontを回転する
#navi(../)
*Fontを回転する [#o8a16de0]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2006-10-30~
更新日:&lastmod;
---
category: swing
folder: TransformedShape
title: Fontを回転する
tags: [Font, Shape, TextLayout, Animation, AffineTransform]
author: aterai
pubdate: 2006-10-30T12:57:51+09:00
description: Fontから文字のアウトラインを取得し、その中心をアンカーポイントに設定して回転します。
image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTVtRPTfJI/AAAAAAAAAoE/Qiy0jcMt_l0/s800/TransformedShape.png
---
* 概要 [#summary]
`Font`から文字のアウトラインを取得し、その中心をアンカーポイントに設定して回転します。

#contents
#download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTVtRPTfJI/AAAAAAAAAoE/Qiy0jcMt_l0/s800/TransformedShape.png)

**概要 [#yd0de09b]
文字のアウトラインを取得して、これを回転してみます。
* サンプルコード [#sourcecode]
#code(link){{
class FontRotateAnimation extends JComponent implements ActionListener {
  private final Timer animator;
  private int rotate;
  private final Shape shape;
  private Shape s;

#screenshot
  public FontRotateAnimation(String str) {
    super();
    animator = new Timer(10, this);
    addHierarchyListener(new HierarchyListener() {
      @Override public void hierarchyChanged(HierarchyEvent e) {
        if ((e.getChangeFlags() & HierarchyEvent.DISPLAYABILITY_CHANGED) != 0
            && animator != null && !e.getComponent().isDisplayable()) {
          animator.stop();
        }
      }
    });
    Font font = new Font(Font.SERIF, Font.PLAIN, 200);
    FontRenderContext frc = new FontRenderContext(null, true, true);
    shape = new TextLayout(str, font, frc).getOutline(null);
    s = shape;
    animator.start();
  }

**サンプルコード [#v9820ba3]
 class FontAnime extends JComponent implements ActionListener {
   private int rotate;
   private final Shape shape;
   public FontAnime() {
     super();
     Font font = new Font("serif", Font.PLAIN, 200);
     FontRenderContext frc = new FontRenderContext(null,true,true);
     shape = new TextLayout("あ", font, frc).getOutline(null);
   }
   public void paintComponent(Graphics g) {
     Graphics2D g2 = (Graphics2D)g;
     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                         RenderingHints.VALUE_ANTIALIAS_ON);
     g2.setColor(Color.black);
 
     Rectangle2D b = shape.getBounds();
     AffineTransform at = AffineTransform.getRotateInstance(
       Math.toRadians(rotate),
       b.getX() + b.getWidth()/2,
       b.getY() + b.getHeight()/2);
     AffineTransform toCenterAT = AffineTransform.getTranslateInstance(
       getWidth()/2  - b.getWidth()/2  - b.getX(),
       getHeight()/2 - b.getHeight()/2 - b.getY());
 
     Shape s = at.createTransformedShape(shape);
     g2.fill(toCenterAT.createTransformedShape(s));
 
     rotate = (rotate>=360) ? 0 : rotate+2;
   }
   public void actionPerformed(ActionEvent e) {
     repaint();
   }
 }
  @Override protected void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setPaint(Color.BLACK);
    g2.fill(s);
    g2.dispose();
  }

-&jnlp;
-&jar;
-&zip;
  @Override public void actionPerformed(ActionEvent e) {
    repaint(s.getBounds());
    Rectangle2D b = shape.getBounds2D();
    Point2D p = new Point2D.Double(
        b.getX() + b.getWidth() / 2d, b.getY() + b.getHeight() / 2d);
    AffineTransform at = AffineTransform.getRotateInstance(
        Math.toRadians(rotate), p.getX(), p.getY());
    AffineTransform toCenterAT = AffineTransform.getTranslateInstance(
        getWidth() / 2d - p.getX(), getHeight() / 2d - p.getY());
    Shape s1 = at.createTransformedShape(shape);
    s = toCenterAT.createTransformedShape(s1);
    repaint(s.getBounds());
    rotate = rotate >= 360 ? 0 : rotate + 2;
  }
}
}}

**解説 [#j87facc1]
上記のサンプルでは、TextLayout から文字列のアウトラインを Shape として取得しています。
* 解説 [#explanation]
上記のサンプルでは、対象文字、`Font`、`FontRenderContext`から`TextLayout`を生成し、`TextLayout#getOutline()`メソッドで文字のアウトラインを`Shape`として取得しています。

//**参考リンク
**コメント [#lba2f99d]
----
- `OpenJDK Corretto-8.212`で実行すると回転中にフォント表示が乱れる件は、[https://github.com/corretto/corretto-8/issues/127 Java2D rendering may break when using soft clipping effects · Issue #127 · corretto/corretto-8]、`8u222`で修正済み %%される予定%%

* 参考リンク [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/java/awt/font/TextLayout.html TextLayout (Java Platform SE 8)]
- [[TextLayoutでFontのメトリック情報を取得する>Swing/TextLayout]]
- [[Timerを使用してJPanelにアナログ時計の針を描画する>Swing/AnalogClock]]

* コメント [#comment]
#comment
#comment