-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircleLayout.java
More file actions
110 lines (90 loc) · 3.62 KB
/
CircleLayout.java
File metadata and controls
110 lines (90 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package circleLayout;
import java.awt.*;
/**
* A layout manager that lays out components along a circle.
*/
public class CircleLayout implements LayoutManager {
private int minWidth = 0;
private int minHeight = 0;
private int preferredWidth = 0;
private int preferredHeight = 0;
private boolean sizesSet = false;
private int maxComponentWidth = 0;
private int maxComponentHeight = 0;
@Override
public void addLayoutComponent(String name, Component comp) {
}
@Override
public void removeLayoutComponent(Component comp) {
}
@Override
public Dimension preferredLayoutSize(Container parent) {
setSizes(parent);
Insets insets = parent.getInsets();
int width = preferredWidth + insets.left + insets.right;
int height = preferredHeight + insets.top + insets.bottom;
return new Dimension(width, height);
}
@Override
public Dimension minimumLayoutSize(Container parent) {
setSizes(parent);
Insets insets = parent.getInsets();
int width = minWidth + insets.left + insets.right;
int height = minHeight + insets.top + insets.bottom;
return new Dimension(width, height);
}
@Override
public void layoutContainer(Container parent) {
setSizes(parent);
// compute center of the circle
Insets insets = parent.getInsets();
int containerWidth = parent.getSize().width - insets.left - insets.right;
int containerHeight = parent.getSize().height - insets.top - insets.bottom;
int x_center = insets.left + containerWidth / 2;
int y_center = insets.top + containerHeight / 2;
// compute radius of the circle
int x_radius = (containerWidth - maxComponentWidth) / 2;
int y_radius = (containerHeight - maxComponentHeight) / 2;
int radius = Math.min(x_radius, y_radius);
// lay out components along the circle
int n = parent.getComponentCount();
for (int i = 0; i < n; i++) {
Component component = parent.getComponent(i);
if (component.isVisible()) {
double angle = 2 * Math.PI * i / n;
// center point of component
int x = x_center + (int) (Math.cos(angle) * radius);
int y = y_center + (int) (Math.sin(angle) * radius);
// move component so that its center is (x, y)
// and its size is its preferred size
Dimension d = component.getPreferredSize();
component.setBounds(x - d.width / 2, y - d.height / 2, d.width, d.height);
}
}
}
public void setSizes(Container parent) {
if (sizesSet) return;
int n = parent.getComponentCount();
preferredWidth = 0;
preferredHeight = 0;
minWidth = 0;
minHeight = 0;
maxComponentWidth = 0;
maxComponentHeight = 0;
// compute the maximum component widths and heights
// and set the preferred size to the sum of the component sizes.
for (int i = 0; i < n; i++) {
Component component = parent.getComponent(i);
if (component.isVisible()) {
Dimension dimension = component.getPreferredSize();
maxComponentWidth = Math.max(maxComponentWidth, dimension.width);
maxComponentHeight = Math.max(maxComponentHeight, dimension.height);
preferredWidth += dimension.width;
preferredHeight += dimension.height;
}
}
minWidth = preferredWidth / 2;
minHeight = preferredHeight / 2;
sizesSet = true;
}
}