Hello, Has anyone else ever created a Custom Layout Manager that puts all
the items in a circle. Basically I am using this to put Playing Cards in a
circle around a table. Any help would be great. Thanks
This is old, Java 1.1 code, but it should be adaptable to a newer JVM:
import java.awt.*;
//
//
// ClockLayout
//
//
public class ClockLayout
implements LayoutManager2
{
//-------------------------
// finalants
//-------------------------
private static final String [] POS_NAME =
{
"0","1","2","3","4","5","6","7","8","9","10","11","12"
};
public static final String CENTER = "0";
public static final String CLOCK01 = "1";
public static final String CLOCK02 = "2";
public static final String CLOCK03 = "3";
public static final String CLOCK04 = "4";
public static final String CLOCK05 = "5";
public static final String CLOCK06 = "6";
public static final String CLOCK07 = "7";
public static final String CLOCK08 = "8";
public static final String CLOCK09 = "9";
public static final String CLOCK10 = "10";
public static final String CLOCK11 = "11";
public static final String CLOCK12 = "12";
public static final String CLOCK00 = CLOCK12;
//-------------------------
// fields
//-------------------------
private Component [] m_comp;
//-------------------------
// exceptions
//-------------------------
private static final IllegalArgumentException err1 = new IllegalArgumentException("invalid ClockLayout radius");
//-------------------------
// finalructors
//-------------------------
public ClockLayout()
{
// allocate component array
// automatically initialized to null references
m_comp = new Component [13];
}
//-------------------------
// methods
//-------------------------
public void addLayoutComponent
(
Component comp,
Object obj
)
{
// convert the object to a string
if (obj instanceof String)
addLayoutComponent((String)obj,comp);
else
addLayoutComponent(CENTER,comp);
}
public void addLayoutComponent
(
String name,
Component comp
)
{
// default value
if (name == null)
name = CENTER;
// find position and assign component
for (int i = 0; i < 13; ++i)
{
if (name.equals(POS_NAME
))
{
m_comp = comp;
break;
}
}
}
public void removeLayoutComponent
(
Component comp
)
{
// find position and assign component
for (int i = 0; i < 13; ++i)
{
if (m_comp == comp)
{
m_comp = null;
break;
}
}
}
public Dimension preferredLayoutSize
(
Container target
)
{
return target.getSize();
}
public Dimension minimumLayoutSize
(
Container target
)
{
return preferredLayoutSize(target);
}
public Dimension maximumLayoutSize
(
Container target
)
{
return preferredLayoutSize(target);
}
public float getLayoutAlignmentX
(
Container parent
)
{
return 0.5F;
}
public float getLayoutAlignmentY
(
Container parent
)
{
return 0.5F;
}
public void invalidateLayout
(
Container target
)
{
// not used
}
public void layoutContainer
(
Container target
)
{
// get target container size
Dimension dim = target.getSize();
// get parameters
Insets ins = target.getInsets();
// compute center of area
int h = (dim.height - ins.top - ins.bottom) / 2;
int w = (dim.width - ins.left - ins.right) / 2;
// clockwise angle for this component
double angle = 0.0;
final double INCR = 0.52359877559; // 30 degrees in radians
for (int i = 0; i < 13; ++i)
{
if ((m_comp != null) && (m_comp.isVisible()))
{
// set component to its preferred size
Dimension d = m_comp.getPreferredSize();
m_comp.setSize(d);
// coordinates for component
int x, y;
switch (i)
{
case 1:
case 2:
case 4:
case 5:
case 7:
case 8:
case 10:
case 11:
// use sine law to compute coordinates
x = (int)(w + Math.sin(angle) * w - d.width / 2);
y = (int)(h - Math.sin(1.57079632679 - angle) * h - d.height / 2);
break;
case 3:
// right side
x = 2 * w - d.width;
y = h - d.height / 2;
break;
case 6:
// bottom
x = w - d.width / 2;
y = 2 * h - d.height;
break;
case 9:
// left side
x = 0;
y = h - d.height / 2;
break;
case 12:
// top of area
x = w - d.width / 2;
y = 0;
break;
default: // put in center
x = w - d.width / 2;
y = h - d.height / 2;
}
m_comp.setLocation(x + ins.left,y + ins.top);
}
angle += INCR;
}
}
}