BevelArrowIcon.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. * BevelArrowIcon.java
  29. * -------------------
  30. * (C) Copyright 2000-2004, by Nobuo Tamemasa and Contributors.
  31. *
  32. * Original Author: Nobuo Tamemasa;
  33. * Contributor(s): David Gilbert (for Object Refinery Limited);
  34. *
  35. * $Id: BevelArrowIcon.java,v 1.5 2007/11/02 17:50:36 taqua Exp $
  36. *
  37. * Changes (from 26-Oct-2001)
  38. * --------------------------
  39. * 26-Oct-2001 : Changed package to com.jrefinery.ui.*;
  40. * 13-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  41. *
  42. */
  43. package org.jfree.ui;
  44. import java.awt.Color;
  45. import java.awt.Component;
  46. import java.awt.Graphics;
  47. import javax.swing.Icon;
  48. import javax.swing.UIManager;
  49. /**
  50. * An arrow icon that can point up or down (usually used to indicate the sort direction in a table).
  51. * <P>
  52. * This class (and also SortButtonRenderer) is based on original code by Nobuo Tamemasa (version
  53. * 1.0, 26-Feb-1999) posted on www.codeguru.com.
  54. *
  55. * @author Nobuo Tamemasa
  56. */
  57. public class BevelArrowIcon implements Icon {
  58. /** Constant indicating that the arrow is pointing up. */
  59. public static final int UP = 0;
  60. /** Constant indicating that the arrow is pointing down. */
  61. public static final int DOWN = 1;
  62. /** The default arrow size. */
  63. private static final int DEFAULT_SIZE = 11;
  64. /** Edge color 1. */
  65. private Color edge1;
  66. /** Edge color 2. */
  67. private Color edge2;
  68. /** The fill color for the arrow icon. */
  69. private Color fill;
  70. /** The size of the icon. */
  71. private int size;
  72. /** The direction that the arrow is pointing (UP or DOWN). */
  73. private int direction;
  74. /**
  75. * Standard constructor - builds an icon with the specified attributes.
  76. *
  77. * @param direction .
  78. * @param isRaisedView .
  79. * @param isPressedView .
  80. */
  81. public BevelArrowIcon(final int direction,
  82. final boolean isRaisedView,
  83. final boolean isPressedView) {
  84. if (isRaisedView) {
  85. if (isPressedView) {
  86. init(UIManager.getColor("controlLtHighlight"),
  87. UIManager.getColor("controlDkShadow"),
  88. UIManager.getColor("controlShadow"),
  89. DEFAULT_SIZE, direction);
  90. }
  91. else {
  92. init(UIManager.getColor("controlHighlight"),
  93. UIManager.getColor("controlShadow"),
  94. UIManager.getColor("control"),
  95. DEFAULT_SIZE, direction);
  96. }
  97. }
  98. else {
  99. if (isPressedView) {
  100. init(UIManager.getColor("controlDkShadow"),
  101. UIManager.getColor("controlLtHighlight"),
  102. UIManager.getColor("controlShadow"),
  103. DEFAULT_SIZE, direction);
  104. }
  105. else {
  106. init(UIManager.getColor("controlShadow"),
  107. UIManager.getColor("controlHighlight"),
  108. UIManager.getColor("control"),
  109. DEFAULT_SIZE, direction);
  110. }
  111. }
  112. }
  113. /**
  114. * Standard constructor - builds an icon with the specified attributes.
  115. *
  116. * @param edge1 the color of edge1.
  117. * @param edge2 the color of edge2.
  118. * @param fill the fill color.
  119. * @param size the size of the arrow icon.
  120. * @param direction the direction that the arrow points.
  121. */
  122. public BevelArrowIcon(final Color edge1,
  123. final Color edge2,
  124. final Color fill,
  125. final int size,
  126. final int direction) {
  127. init(edge1, edge2, fill, size, direction);
  128. }
  129. /**
  130. * Paints the icon at the specified position. Supports the Icon interface.
  131. *
  132. * @param c .
  133. * @param g .
  134. * @param x .
  135. * @param y .
  136. */
  137. public void paintIcon(final Component c,
  138. final Graphics g,
  139. final int x,
  140. final int y) {
  141. switch (this.direction) {
  142. case DOWN: drawDownArrow(g, x, y); break;
  143. case UP: drawUpArrow(g, x, y); break;
  144. }
  145. }
  146. /**
  147. * Returns the width of the icon. Supports the Icon interface.
  148. *
  149. * @return the icon width.
  150. */
  151. public int getIconWidth() {
  152. return this.size;
  153. }
  154. /**
  155. * Returns the height of the icon. Supports the Icon interface.
  156. * @return the icon height.
  157. */
  158. public int getIconHeight() {
  159. return this.size;
  160. }
  161. /**
  162. * Initialises the attributes of the arrow icon.
  163. *
  164. * @param edge1 the color of edge1.
  165. * @param edge2 the color of edge2.
  166. * @param fill the fill color.
  167. * @param size the size of the arrow icon.
  168. * @param direction the direction that the arrow points.
  169. */
  170. private void init(final Color edge1,
  171. final Color edge2,
  172. final Color fill,
  173. final int size,
  174. final int direction) {
  175. this.edge1 = edge1;
  176. this.edge2 = edge2;
  177. this.fill = fill;
  178. this.size = size;
  179. this.direction = direction;
  180. }
  181. /**
  182. * Draws the arrow pointing down.
  183. *
  184. * @param g the graphics device.
  185. * @param xo ??
  186. * @param yo ??
  187. */
  188. private void drawDownArrow(final Graphics g, final int xo, final int yo) {
  189. g.setColor(this.edge1);
  190. g.drawLine(xo, yo, xo + this.size - 1, yo);
  191. g.drawLine(xo, yo + 1, xo + this.size - 3, yo + 1);
  192. g.setColor(this.edge2);
  193. g.drawLine(xo + this.size - 2, yo + 1, xo + this.size - 1, yo + 1);
  194. int x = xo + 1;
  195. int y = yo + 2;
  196. int dx = this.size - 6;
  197. while (y + 1 < yo + this.size) {
  198. g.setColor(this.edge1);
  199. g.drawLine(x, y, x + 1, y);
  200. g.drawLine(x, y + 1, x + 1, y + 1);
  201. if (0 < dx) {
  202. g.setColor(this.fill);
  203. g.drawLine(x + 2, y, x + 1 + dx, y);
  204. g.drawLine(x + 2, y + 1, x + 1 + dx, y + 1);
  205. }
  206. g.setColor(this.edge2);
  207. g.drawLine(x + dx + 2, y, x + dx + 3, y);
  208. g.drawLine(x + dx + 2, y + 1, x + dx + 3, y + 1);
  209. x += 1;
  210. y += 2;
  211. dx -= 2;
  212. }
  213. g.setColor(this.edge1);
  214. g.drawLine(
  215. xo + (this.size / 2), yo + this.size - 1, xo + (this.size / 2), yo + this.size - 1
  216. );
  217. }
  218. /**
  219. * Draws the arrow pointing up.
  220. *
  221. * @param g the graphics device.
  222. * @param xo ??
  223. * @param yo ??
  224. */
  225. private void drawUpArrow(final Graphics g, final int xo, final int yo) {
  226. g.setColor(this.edge1);
  227. int x = xo + (this.size / 2);
  228. g.drawLine(x, yo, x, yo);
  229. x--;
  230. int y = yo + 1;
  231. int dx = 0;
  232. while (y + 3 < yo + this.size) {
  233. g.setColor(this.edge1);
  234. g.drawLine(x, y, x + 1, y);
  235. g.drawLine(x, y + 1, x + 1, y + 1);
  236. if (0 < dx) {
  237. g.setColor(this.fill);
  238. g.drawLine(x + 2, y, x + 1 + dx, y);
  239. g.drawLine(x + 2, y + 1, x + 1 + dx, y + 1);
  240. }
  241. g.setColor(this.edge2);
  242. g.drawLine(x + dx + 2, y, x + dx + 3, y);
  243. g.drawLine(x + dx + 2, y + 1, x + dx + 3, y + 1);
  244. x -= 1;
  245. y += 2;
  246. dx += 2;
  247. }
  248. g.setColor(this.edge1);
  249. g.drawLine(xo, yo + this.size - 3, xo + 1, yo + this.size - 3);
  250. g.setColor(this.edge2);
  251. g.drawLine(xo + 2, yo + this.size - 2, xo + this.size - 1, yo + this.size - 2);
  252. g.drawLine(xo, yo + this.size - 1, xo + this.size, yo + this.size - 1);
  253. }
  254. }