Circle Layout : Custom Layout « Swing « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Swing » Custom Layout 




/**
 * This layout manager allows you to place components to form a circle within a
 * Container
 
 @author Oscar De Leon oedeleon@netscape.net
 
 */

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class CircleLayoutDemo {

  public static void main(String a[]) {
    JFrame frame = new JFrame();
    JPanel circle = new JPanel();
    circle.setLayout(new CircleLayout(true));

    for (int i = 0; i < 10; i++) {
      circle.add(new JLabel("circle_" + i * 7));
    }
    frame.setContentPane(circle);
    frame.setSize(200400);

    frame.setVisible(true);
  }
}

/**
 * This layout manager allows you to place components to form a circle within a
 * Container
 
 @author Oscar De Leon oedeleon@netscape.net
 
 */

class CircleLayout implements LayoutManager {

  ArrayList components;

  ArrayList names;

  private boolean isCircle;

  /**
   * Creates a new CircleLayout that lays out components in a perfect circle
   */

  public CircleLayout() {
    this(true);
  }

  /**
   * Creates a new CircleLayout that lays out components in either an Ellipse or
   * a Circle. Ellipse Layout is not yet implemented.
   
   @param circle
   *          Indicated the shape to use. It's true for circle or false for
   *          ellipse.
   */
  public CircleLayout(boolean circle) {
    isCircle = circle;
  }

  /**
   * For compatibility with LayoutManager interface
   */
  public void addLayoutComponent(String name, Component comp) {
  }

  /**
   * Arranges the parent's Component objects in either an Ellipse or a Circle.
   * Ellipse is not yet implemented.
   */
  public void layoutContainer(Container parent) {
    int x, y, w, h, s, c;
    int n = parent.getComponentCount();
    double parentWidth = parent.getSize().width;
    double parentHeight = parent.getSize().height;
    Insets insets = parent.getInsets();
    int centerX = (int) (parentWidth - (insets.left + insets.right)) 2;
    int centerY = (int) (parentHeight - (insets.top + insets.bottom)) 2;

    Component comp = null;
    Dimension compPS = null;
    if (n == 1) {
      comp = parent.getComponent(0);
      x = centerX;
      y = centerY;
      compPS = comp.getPreferredSize();
      w = compPS.width;
      h = compPS.height;
      comp.setBounds(x, y, w, h);
    else {
      double r = (Math.min(parentWidth - (insets.left + insets.right), parentHeight
          (insets.top + insets.bottom))) 2;
      r *= 0.75// Multiply by .75 to account for extreme right and bottom
                  // Components
      for (int i = 0; i < n; i++) {
        comp = parent.getComponent(i);
        compPS = comp.getPreferredSize();
        if (isCircle) {
          c = (int) (r * Math.cos(* i * Math.PI / n));
          s = (int) (r * Math.sin(* i * Math.PI / n));
        else {
          c = (int) ((centerX * 0.75* Math.cos(* i * Math.PI / n));
          s = (int) ((centerY * 0.75* Math.sin(* i * Math.PI / n));
        }
        x = c + centerX;
        y = s + centerY;

        w = compPS.width;
        h = compPS.height;

        comp.setBounds(x, y, w, h);
      }
    }

  }

  /**
   * Returns this CircleLayout's preferred size based on its Container
   
   @param target
   *          This CircleLayout's target container
   @return The preferred size
   */

  public Dimension preferredLayoutSize(Container target) {
    return target.getSize();
  }

  /**
   * Returns this CircleLayout's minimum size based on its Container
   
   @param target
   *          This CircleLayout's target container
   @return The minimum size
   */
  public Dimension minimumLayoutSize(Container target) {
    return target.getSize();
  }

  /**
   * For compatibility with LayoutManager interface
   */
  public void removeLayoutComponent(Component comp) {
  }

  /**
   * Returns a String representation of this CircleLayout.
   
   @return A String that represents this CircleLayout
   */
  public String toString() {
    return this.getClass().getName();
  }

}














14.97.Custom Layout
14.97.1.Custom Layout: DiagonalLayoutCustom Layout: DiagonalLayout
14.97.2.GraphPaperLayout implements LayoutManager2
14.97.3.Circle Layout
14.97.4.This LayoutManager arranges the components into a column. Components are always given their preferred size
14.97.5.Compents are laid out in a circle.
14.97.6.Specialised layout manager for a grid of components.
14.97.7.Stack Layout, uses an orientation to determine if the contents should be arranged horizontally or vertically.
14.97.8.A layout manager that displays a single component in the center of its container.
14.97.9.A simple layoutmanager to overlay all components of a parent.
14.97.10.A layout manager that lays out components along a central axis
14.97.11.Wrapper Layout
14.97.12.Center Layout
14.97.13.DividerLayout is layout that divides two components with the column of actions
14.97.14.X Y Layout
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.