TITLE:LayoutManagerを拡張して曲線上にコンポーネントを配置
Posted by at 2011-06-27

LayoutManagerを拡張して曲線上にコンポーネントを配置

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

概要

LayoutManagerを拡張して曲線上にコンポーネントを配置します。
CurveLayout.png

サンプルコード

#spanend
#spanadd
* サンプルコード [#sourcecode]
#spanend
#spanadd
#code(link){{
#spanend
#spanadd
final double A2 = 4.0;
#spanend
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));
        }
      }
    }
  }
});

解説

    • panel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
    • FlowLayout#layoutContainer(...)をオーバーライドして、二次曲線の上にコンポーネントを並べる

解説

  • 左: FlowLayout(LEFT)
    • new FlowLayout(FlowLayout.LEFT)を設定したJPanelにコンポーネントを配置
  • 右: y=Math.pow(x/4.0, 2.0)
    • FlowLayout#layoutContainer(...)をオーバーライドして二次曲線の上にコンポーネントを配置するレイアウトを作成してJPanelに設定

コメント

参考リンク

コメント