---
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 [#summary]
`LayoutManager`を拡張して曲線上にコンポーネントを配置します。
#download(https://lh4.googleusercontent.com/-Rww2mulIVEI/TggO-rFh_2I/AAAAAAAAA98/R3ZVsfyu3IU/s800/CurveLayout.png)
* Source Code Examples [#sourcecode]
#code(link){{
final double A2 = 4.0;
panel2.setLayout(new FlowLayout() {
@Override public void layoutContainer(Container target) {
synchronized(target.getTreeLock()) {
Insets i = target.getInsets();
int nmembers = target.getComponentCount();
if (nmembers <= 0) {
return;
}
int vgap = getVgap();
int hgap = getHgap();
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()) {
Dimension d = m.getPreferredSize();
m.setSize(d.width, d.height);
m.setLocation(x, y);
y += (vgap + Math.min(rowh, d.height));
x = (int) (A2 * Math.sqrt(y));
}
}
}
}
});
}}
* Description [#explanation]
* Description [#description]
- 左: `FlowLayout(LEFT)`
-- `new FlowLayout(FlowLayout.LEFT)`を設定した`JPanel`にコンポーネントを配置
- 右: `y=Math.pow(x/4.0, 2.0)`
-- `FlowLayout#layoutContainer(...)`をオーバーライドして二次曲線の上にコンポーネントを配置するレイアウトを作成して`JPanel`に設定
* Reference [#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
#comment