1 42 43 package org.jfree.layout; 44 45 import java.awt.Checkbox ; 46 import java.awt.Component ; 47 import java.awt.Container ; 48 import java.awt.Dimension ; 49 import java.awt.Frame ; 50 import java.awt.Insets ; 51 import java.awt.LayoutManager ; 52 import java.awt.Panel ; 53 import java.io.Serializable ; 54 55 67 68 public class RadialLayout implements LayoutManager , Serializable { 69 70 71 private static final long serialVersionUID = -7582156799248315534L; 72 73 74 private int minWidth = 0; 75 76 77 private int minHeight = 0; 78 79 80 private int maxCompWidth = 0; 81 82 83 private int maxCompHeight = 0; 84 85 86 private int preferredWidth = 0; 87 88 89 private int preferredHeight = 0; 90 91 92 private boolean sizeUnknown = true; 93 94 97 public RadialLayout() { 98 super(); 99 } 100 101 106 public void addLayoutComponent(final Component comp) { 107 } 109 110 115 public void removeLayoutComponent(final Component comp) { 116 } 118 119 125 public void addLayoutComponent(final String name, final Component comp) { 126 } 128 129 135 public void removeLayoutComponent(final String name, final Component comp) { 136 } 138 139 146 private void setSizes(final Container parent) { 147 final int nComps = parent.getComponentCount(); 148 this.preferredWidth = 0; 150 this.preferredHeight = 0; 151 this.minWidth = 0; 152 this.minHeight = 0; 153 for (int i = 0; i < nComps; i++) { 154 final Component c = parent.getComponent(i); 155 if (c.isVisible()) { 156 final Dimension d = c.getPreferredSize(); 157 if (this.maxCompWidth < d.width) { 158 this.maxCompWidth = d.width; 159 } 160 if (this.maxCompHeight < d.height) { 161 this.maxCompHeight = d.height; 162 } 163 this.preferredWidth += d.width; 164 this.preferredHeight += d.height; 165 } 166 } 167 this.preferredWidth = this.preferredWidth / 2; 168 this.preferredHeight = this.preferredHeight / 2; 169 this.minWidth = this.preferredWidth; 170 this.minHeight = this.preferredHeight; 171 } 172 173 181 public Dimension preferredLayoutSize(final Container parent) { 182 final Dimension dim = new Dimension (0, 0); 183 setSizes(parent); 184 185 final Insets insets = parent.getInsets(); 187 dim.width = this.preferredWidth + insets.left + insets.right; 188 dim.height = this.preferredHeight + insets.top + insets.bottom; 189 190 this.sizeUnknown = false; 191 return dim; 192 } 193 194 202 public Dimension minimumLayoutSize(final Container parent) { 203 final Dimension dim = new Dimension (0, 0); 204 205 final Insets insets = parent.getInsets(); 207 dim.width = this.minWidth + insets.left + insets.right; 208 dim.height = this.minHeight + insets.top + insets.bottom; 209 210 this.sizeUnknown = false; 211 return dim; 212 } 213 214 223 public void layoutContainer(final Container parent) { 224 final Insets insets = parent.getInsets(); 225 final int maxWidth = parent.getSize().width 226 - (insets.left + insets.right); 227 final int maxHeight = parent.getSize().height 228 - (insets.top + insets.bottom); 229 final int nComps = parent.getComponentCount(); 230 int x = 0; 231 int y = 0; 232 233 if (this.sizeUnknown) { 236 setSizes(parent); 237 } 238 239 if (nComps < 2) { 240 final Component c = parent.getComponent(0); 241 if (c.isVisible()) { 242 final Dimension d = c.getPreferredSize(); 243 c.setBounds(x, y, d.width, d.height); 244 } 245 } 246 else { 247 double radialCurrent = Math.toRadians(90); 248 final double radialIncrement = 2 * Math.PI / nComps; 249 final int midX = maxWidth / 2; 250 final int midY = maxHeight / 2; 251 final int a = midX - this.maxCompWidth; 252 final int b = midY - this.maxCompHeight; 253 for (int i = 0; i < nComps; i++) { 254 final Component c = parent.getComponent(i); 255 if (c.isVisible()) { 256 final Dimension d = c.getPreferredSize(); 257 x = (int) (midX 258 - (a * Math.cos(radialCurrent)) 259 - (d.getWidth() / 2) 260 + insets.left); 261 y = (int) (midY 262 - (b * Math.sin(radialCurrent)) 263 - (d.getHeight() / 2) 264 + insets.top); 265 266 c.setBounds(x, y, d.width, d.height); 268 } 269 radialCurrent += radialIncrement; 270 } 271 } 272 } 273 274 279 public String toString() { 280 return getClass().getName(); 281 } 282 283 290 public static void main(final String [] args) throws Exception { 291 final Frame frame = new Frame (); 292 final Panel panel = new Panel (); 293 panel.setLayout(new RadialLayout()); 294 295 panel.add(new Checkbox ("One")); 296 panel.add(new Checkbox ("Two")); 297 panel.add(new Checkbox ("Three")); 298 panel.add(new Checkbox ("Four")); 299 panel.add(new Checkbox ("Five")); 300 panel.add(new Checkbox ("One")); 301 panel.add(new Checkbox ("Two")); 302 panel.add(new Checkbox ("Three")); 303 panel.add(new Checkbox ("Four")); 304 panel.add(new Checkbox ("Five")); 305 306 frame.add(panel); 307 frame.setSize(300, 500); 308 frame.setVisible(true); 309 } 310 311 } 312 | Popular Tags |