Creating a Custom Circular Layout Manager!

  • Thread starter ***C.Steamer***
  • Start date
C

***C.Steamer***

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
 
P

Paul Lutus

***C.Steamer*** said:
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.

Okay, we're ready to help you. Post your code.
 
O

Ozan Akdemir

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

Try using JGoodies Forms form layout: https://forms.dev.java.net/.
Actually it's not a "circular" layout system but you can locate your
components in a circular layout. It's really flexible and useful layout
system.
Have a look at the documents and tutorials about it.

Ozan.
 
S

Sudsy

***C.Steamer*** said:
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

I could write you such a layout manager...for a price. But you
could do it easily yourself, assuming more than passing knowledge
of Java. Math.sin and Math.cos, along with Math.toRadians, are
your friends!
 
T

Thomas Weidenfeller

***C.Steamer*** said:
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

I think Roedy did something like this. You might want to search his site
http://mindprod.com/jgloss/

/Thomas
 
S

Scott Robert Ladd

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;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top