• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:LayoutManagerを拡張して曲線上にコンポーネントを配置
#navi(../)
RIGHT:Posted by &author(aterai); at 2011-06-27
*LayoutManagerを拡張して曲線上にコンポーネントを配置 [#jba0ffbd]
LayoutManagerを拡張して曲線上にコンポーネントを配置します。
---
category: swing
folder: CurveLayout
title: LayoutManagerを拡張して曲線上にコンポーネントを配置
tags: [LayoutManager, FlowLayout, JPanel]
author: aterai
pubdate: 2011-06-27T14:12:48+09:00
description: LayoutManagerを拡張して曲線上にコンポーネントを配置します。
image: https://lh4.googleusercontent.com/-Rww2mulIVEI/TggO-rFh_2I/AAAAAAAAA98/R3ZVsfyu3IU/s800/CurveLayout.png
hreflang:
    href: https://java-swing-tips.blogspot.com/2012/05/creating-custom-layout-manager.html
    lang: en
---
* 概要 [#summary]
`LayoutManager`を拡張して曲線上にコンポーネントを配置します。

-&jnlp;
-&jar;
-&zip;
#download(https://lh4.googleusercontent.com/-Rww2mulIVEI/TggO-rFh_2I/AAAAAAAAA98/R3ZVsfyu3IU/s800/CurveLayout.png)

//#screenshot
#ref(https://lh4.googleusercontent.com/-Rww2mulIVEI/TggO-rFh_2I/AAAAAAAAA98/R3ZVsfyu3IU/s800/CurveLayout.png)

**サンプルコード [#d3bf9d05]
#code{{
* サンプルコード [#sourcecode]
#code(link){{
final double A2 = 4.0;
panel2.setLayout(new FlowLayout() {
  public void layoutContainer(Container target) {
  @Override public void layoutContainer(Container target) {
    synchronized(target.getTreeLock()) {
      Insets insets = target.getInsets();
      Insets i = target.getInsets();
      int nmembers  = target.getComponentCount();
      if(nmembers<=0) return;
      if (nmembers <= 0) {
        return;
      }
      int vgap = getVgap();
      int hgap = getHgap();
      int rowh = (target.getHeight()-(insets.top+insets.bottom+vgap*2))/nmembers;
      int x = insets.left + hgap;
      int y = insets.top  + vgap;
      for(int i=0;i<nmembers;i++) {
      int rowh = (target.getHeight() - (i.top + i.bottom + vgap * 2)) / nmembers;
      int x = i.left + hgap;
      int y = i.top  + vgap;
      for (int i = 0; i < nmembers; i++) {
        Component m = target.getComponent(i);
        if(m.isVisible()) {
        if (m.isVisible()) {
          Dimension d = m.getPreferredSize();
          m.setSize(d.width, d.height);
          m.setLocation(x, y);
          y += (vgap + Math.min(rowh, d.height));
          x = (int)(a * Math.sqrt(y));
          x = (int) (A2 * Math.sqrt(y));
        }
      }
    }
  }
});
}}

**解説 [#cf9f86b7]
- 左
-- panel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
- 右
-- FlowLayout#layoutContainer(...)をオーバーライドして、二次曲線の上にコンポーネントを並べる
* 解説 [#explanation]
- 左: `FlowLayout(LEFT)`
-- `new FlowLayout(FlowLayout.LEFT)`を設定した`JPanel`にコンポーネントを配置
- 右: `y=Math.pow(x/4.0, 2.0)`
-- `FlowLayout#layoutContainer(...)`をオーバーライドして二次曲線の上にコンポーネントを配置するレイアウトを作成して`JPanel`に設定

//**参考リンク
**コメント [#a7a05756]
* 参考リンク [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/java/awt/FlowLayout.html#layoutContainer-java.awt.Container- FlowLayout#layoutContainer(Container) (Java Platform SE 8)]

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