@Baqueta と @sacha のアイデアが好きです。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CircleLayoutTest {
public JComponent makeUI() {
JPanel panel = new JPanel() {
@Override protected void paintComponent(Graphics g) {
super.paintComponent(g);
Insets i = getInsets();
g.translate(i.left, i.top);
g.setColor(Color.RED);
int w = getWidth() - i.left - i.right;
int h = getHeight() - i.top - i.bottom;
g.drawOval(0, 0, w, h);
g.translate(-i.left, -i.top);
}
};
panel.setLayout(new FlowLayout() {
@Override public void layoutContainer(Container target) {
synchronized(target.getTreeLock()) {
int nmembers = target.getComponentCount();
if(nmembers<=0) return;
Insets i = target.getInsets();
double cx = .5 * target.getWidth();
double cy = .5 * target.getHeight();
Component m = target.getComponent(0);
Dimension d = m.getPreferredSize();
m.setSize(d.width, d.height);
m.setLocation((int)(cx+.5-.5*d.width),(int)(cy+.5-.5*d.height));
if(nmembers-1<=0) return;
double rw = .5 * (target.getWidth() - i.left - i.right);
double rh = .5 * (target.getHeight() - i.top - i.bottom);
double x = 0, y = 0, r = 0;
double radian = 2.0 * Math.PI / (nmembers-1);
for(int j=1; j<nmembers; j++) {
m = target.getComponent(j);
if(m.isVisible()) {
d = m.getPreferredSize();
m.setSize(d.width, d.height);
x = cx + rw * Math.cos(r) - .5 * d.width;
y = cy + rh * Math.sin(r) - .5 * d.height;
m.setLocation((int)(x+.5), (int)(y+.5));
r += radian;
}
}
}
}
});
JPanel p = new JPanel(new BorderLayout());
p.add(initPanel(panel));
return p;
}
private static JComponent initPanel(JComponent p) {
p.setBorder(BorderFactory.createEmptyBorder(50,50,50,50));
for(int i=0; i<6; i++) {
p.add(new JLabel("No."+i));
}
return p;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new CircleLayoutTest().makeUI());
f.setSize(320 ,320);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}