RadialLayout.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /* ========================================================================
  2. * JCommon : a free general purpose class library for the Java(tm) platform
  3. * ========================================================================
  4. *
  5. * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jcommon/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  22. * USA.
  23. *
  24. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  25. * in the United States and other countries.]
  26. *
  27. * -----------------
  28. * RadialLayout.java
  29. * -----------------
  30. * (C) Copyright 2003, 2004, by Bryan Scott (for Australian Antarctic Division).
  31. *
  32. * Original Author: Bryan Scott (for Australian Antarctic Division);
  33. * Contributor(s): David Gilbert (for Object Refinery Limited);
  34. *
  35. *
  36. * Changes:
  37. * --------
  38. * 30-Jun-2003 : Version 1 (BS);
  39. * 24-Jul-2003 : Completed missing Javadocs (DG);
  40. *
  41. */
  42. package org.jfree.layout;
  43. import java.awt.Checkbox;
  44. import java.awt.Component;
  45. import java.awt.Container;
  46. import java.awt.Dimension;
  47. import java.awt.Frame;
  48. import java.awt.Insets;
  49. import java.awt.LayoutManager;
  50. import java.awt.Panel;
  51. import java.io.Serializable;
  52. /**
  53. * RadialLayout is a component layout manager. Compents are laid out in a
  54. * circle. If only one component is contained in the layout it is positioned
  55. * centrally, otherwise components are evenly spaced around the centre with
  56. * the first component placed to the North.
  57. *<P>
  58. * This code was developed to display CTD rosette firing control
  59. *
  60. * WARNING: Not thoughly tested, use at own risk.
  61. *
  62. * @author Bryan Scott (for Australian Antarctic Division)
  63. */
  64. public class RadialLayout implements LayoutManager, Serializable {
  65. /** For serialization. */
  66. private static final long serialVersionUID = -7582156799248315534L;
  67. /** The minimum width. */
  68. private int minWidth = 0;
  69. /** The minimum height. */
  70. private int minHeight = 0;
  71. /** The maximum component width. */
  72. private int maxCompWidth = 0;
  73. /** The maximum component height. */
  74. private int maxCompHeight = 0;
  75. /** The preferred width. */
  76. private int preferredWidth = 0;
  77. /** The preferred height. */
  78. private int preferredHeight = 0;
  79. /** Size unknown flag. */
  80. private boolean sizeUnknown = true;
  81. /**
  82. * Constructs this layout manager with default properties.
  83. */
  84. public RadialLayout() {
  85. super();
  86. }
  87. /**
  88. * Not used.
  89. *
  90. * @param comp the component.
  91. */
  92. public void addLayoutComponent(final Component comp) {
  93. // not used
  94. }
  95. /**
  96. * Not used.
  97. *
  98. * @param comp the component.
  99. */
  100. public void removeLayoutComponent(final Component comp) {
  101. // not used
  102. }
  103. /**
  104. * Not used.
  105. *
  106. * @param name the component name.
  107. * @param comp the component.
  108. */
  109. public void addLayoutComponent(final String name, final Component comp) {
  110. // not used
  111. }
  112. /**
  113. * Not used.
  114. *
  115. * @param name the component name.
  116. * @param comp the component.
  117. */
  118. public void removeLayoutComponent(final String name, final Component comp) {
  119. // not used
  120. }
  121. /**
  122. * Sets the sizes attribute of the RadialLayout object.
  123. *
  124. * @param parent the parent.
  125. *
  126. * @see LayoutManager
  127. */
  128. private void setSizes(final Container parent) {
  129. final int nComps = parent.getComponentCount();
  130. //Reset preferred/minimum width and height.
  131. this.preferredWidth = 0;
  132. this.preferredHeight = 0;
  133. this.minWidth = 0;
  134. this.minHeight = 0;
  135. for (int i = 0; i < nComps; i++) {
  136. final Component c = parent.getComponent(i);
  137. if (c.isVisible()) {
  138. final Dimension d = c.getPreferredSize();
  139. if (this.maxCompWidth < d.width) {
  140. this.maxCompWidth = d.width;
  141. }
  142. if (this.maxCompHeight < d.height) {
  143. this.maxCompHeight = d.height;
  144. }
  145. this.preferredWidth += d.width;
  146. this.preferredHeight += d.height;
  147. }
  148. }
  149. this.preferredWidth = this.preferredWidth / 2;
  150. this.preferredHeight = this.preferredHeight / 2;
  151. this.minWidth = this.preferredWidth;
  152. this.minHeight = this.preferredHeight;
  153. }
  154. /**
  155. * Returns the preferred size.
  156. *
  157. * @param parent the parent.
  158. *
  159. * @return The preferred size.
  160. * @see LayoutManager
  161. */
  162. public Dimension preferredLayoutSize(final Container parent) {
  163. final Dimension dim = new Dimension(0, 0);
  164. setSizes(parent);
  165. //Always add the container's insets!
  166. final Insets insets = parent.getInsets();
  167. dim.width = this.preferredWidth + insets.left + insets.right;
  168. dim.height = this.preferredHeight + insets.top + insets.bottom;
  169. this.sizeUnknown = false;
  170. return dim;
  171. }
  172. /**
  173. * Returns the minimum size.
  174. *
  175. * @param parent the parent.
  176. *
  177. * @return The minimum size.
  178. * @see LayoutManager
  179. */
  180. public Dimension minimumLayoutSize(final Container parent) {
  181. final Dimension dim = new Dimension(0, 0);
  182. //Always add the container's insets!
  183. final Insets insets = parent.getInsets();
  184. dim.width = this.minWidth + insets.left + insets.right;
  185. dim.height = this.minHeight + insets.top + insets.bottom;
  186. this.sizeUnknown = false;
  187. return dim;
  188. }
  189. /**
  190. * This is called when the panel is first displayed, and every time its size
  191. * changes.
  192. * Note: You CAN'T assume preferredLayoutSize or minimumLayoutSize will be
  193. * called -- in the case of applets, at least, they probably won't be.
  194. *
  195. * @param parent the parent.
  196. * @see LayoutManager
  197. */
  198. public void layoutContainer(final Container parent) {
  199. final Insets insets = parent.getInsets();
  200. final int maxWidth = parent.getSize().width
  201. - (insets.left + insets.right);
  202. final int maxHeight = parent.getSize().height
  203. - (insets.top + insets.bottom);
  204. final int nComps = parent.getComponentCount();
  205. int x = 0;
  206. int y = 0;
  207. // Go through the components' sizes, if neither preferredLayoutSize nor
  208. // minimumLayoutSize has been called.
  209. if (this.sizeUnknown) {
  210. setSizes(parent);
  211. }
  212. if (nComps < 2) {
  213. final Component c = parent.getComponent(0);
  214. if (c.isVisible()) {
  215. final Dimension d = c.getPreferredSize();
  216. c.setBounds(x, y, d.width, d.height);
  217. }
  218. }
  219. else {
  220. double radialCurrent = Math.toRadians(90);
  221. final double radialIncrement = 2 * Math.PI / nComps;
  222. final int midX = maxWidth / 2;
  223. final int midY = maxHeight / 2;
  224. final int a = midX - this.maxCompWidth;
  225. final int b = midY - this.maxCompHeight;
  226. for (int i = 0; i < nComps; i++) {
  227. final Component c = parent.getComponent(i);
  228. if (c.isVisible()) {
  229. final Dimension d = c.getPreferredSize();
  230. x = (int) (midX
  231. - (a * Math.cos(radialCurrent))
  232. - (d.getWidth() / 2)
  233. + insets.left);
  234. y = (int) (midY
  235. - (b * Math.sin(radialCurrent))
  236. - (d.getHeight() / 2)
  237. + insets.top);
  238. // Set the component's size and position.
  239. c.setBounds(x, y, d.width, d.height);
  240. }
  241. radialCurrent += radialIncrement;
  242. }
  243. }
  244. }
  245. /**
  246. * Returns the class name.
  247. *
  248. * @return The class name.
  249. */
  250. public String toString() {
  251. return getClass().getName();
  252. }
  253. /**
  254. * Run a demonstration.
  255. *
  256. * @param args ignored.
  257. *
  258. * @throws Exception when an error occurs.
  259. */
  260. public static void main(final String[] args) throws Exception {
  261. final Frame frame = new Frame();
  262. final Panel panel = new Panel();
  263. panel.setLayout(new RadialLayout());
  264. panel.add(new Checkbox("One"));
  265. panel.add(new Checkbox("Two"));
  266. panel.add(new Checkbox("Three"));
  267. panel.add(new Checkbox("Four"));
  268. panel.add(new Checkbox("Five"));
  269. panel.add(new Checkbox("One"));
  270. panel.add(new Checkbox("Two"));
  271. panel.add(new Checkbox("Three"));
  272. panel.add(new Checkbox("Four"));
  273. panel.add(new Checkbox("Five"));
  274. frame.add(panel);
  275. frame.setSize(300, 500);
  276. frame.setVisible(true);
  277. }
  278. }