Align.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. * Align.java
  29. * ----------
  30. * (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
  31. *
  32. * Original Author: Christian W. Zuckschwerdt;
  33. * Contributor(s): David Gilbert (for Object Refinery Limited);
  34. *
  35. * $Id: Align.java,v 1.5 2005/10/18 13:18:34 mungady Exp $
  36. *
  37. * Changes (from 30-May-2002)
  38. * --------------------------
  39. * 30-May-2002 : Added title (DG);
  40. * 13-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  41. *
  42. */
  43. package org.jfree.ui;
  44. import java.awt.geom.Rectangle2D;
  45. /**
  46. * A utility class for aligning rectangles.
  47. *
  48. * @author David Gilbert
  49. */
  50. public final class Align {
  51. /** Center alignment. */
  52. public static final int CENTER = 0x00;
  53. /** Top alignment. */
  54. public static final int TOP = 0x01;
  55. /** Bottom alignment. */
  56. public static final int BOTTOM = 0x02;
  57. /** Left alignment. */
  58. public static final int LEFT = 0x04;
  59. /** Right alignment. */
  60. public static final int RIGHT = 0x08;
  61. /** Top/Left alignment. */
  62. public static final int TOP_LEFT = TOP | LEFT;
  63. /** Top/Right alignment. */
  64. public static final int TOP_RIGHT = TOP | RIGHT;
  65. /** Bottom/Left alignment. */
  66. public static final int BOTTOM_LEFT = BOTTOM | LEFT;
  67. /** Bottom/Right alignment. */
  68. public static final int BOTTOM_RIGHT = BOTTOM | RIGHT;
  69. /** Horizontal fit. */
  70. public static final int FIT_HORIZONTAL = LEFT | RIGHT;
  71. /** Vertical fit. */
  72. public static final int FIT_VERTICAL = TOP | BOTTOM;
  73. /** Complete fit. */
  74. public static final int FIT = FIT_HORIZONTAL | FIT_VERTICAL;
  75. /** North alignment (same as TOP). */
  76. public static final int NORTH = TOP;
  77. /** South alignment (same as BOTTOM). */
  78. public static final int SOUTH = BOTTOM;
  79. /** West alignment (same as LEFT). */
  80. public static final int WEST = LEFT;
  81. /** East alignment (same as RIGHT). */
  82. public static final int EAST = RIGHT;
  83. /** North/West alignment (same as TOP_LEFT). */
  84. public static final int NORTH_WEST = NORTH | WEST;
  85. /** North/East alignment (same as TOP_RIGHT). */
  86. public static final int NORTH_EAST = NORTH | EAST;
  87. /** South/West alignment (same as BOTTOM_LEFT). */
  88. public static final int SOUTH_WEST = SOUTH | WEST;
  89. /** South/East alignment (same as BOTTOM_RIGHT). */
  90. public static final int SOUTH_EAST = SOUTH | EAST;
  91. /**
  92. * Private constructor.
  93. */
  94. private Align() {
  95. super();
  96. }
  97. /**
  98. * Aligns one rectangle (<code>rect</code>) relative to another rectangle (<code>frame</code>).
  99. *
  100. * @param rect the rectangle to be aligned (<code>null</code> not permitted).
  101. * @param frame the reference frame (<code>null</code> not permitted).
  102. * @param align the alignment code.
  103. */
  104. public static void align(final Rectangle2D rect, final Rectangle2D frame, final int align) {
  105. double x = frame.getCenterX() - rect.getWidth() / 2.0;
  106. double y = frame.getCenterY() - rect.getHeight() / 2.0;
  107. double w = rect.getWidth();
  108. double h = rect.getHeight();
  109. if ((align & FIT_VERTICAL) == FIT_VERTICAL) {
  110. h = frame.getHeight();
  111. }
  112. if ((align & FIT_HORIZONTAL) == FIT_HORIZONTAL) {
  113. w = frame.getWidth();
  114. }
  115. if ((align & TOP) == TOP) {
  116. y = frame.getMinY();
  117. }
  118. if ((align & BOTTOM) == BOTTOM) {
  119. y = frame.getMaxY() - h;
  120. }
  121. if ((align & LEFT) == LEFT) {
  122. x = frame.getX();
  123. }
  124. if ((align & RIGHT) == RIGHT) {
  125. x = frame.getMaxX() - w;
  126. }
  127. rect.setRect(x, y, w, h);
  128. }
  129. }