1. /*
  2. * @(#)BasicArrowButton.java 1.19 00/02/02
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing.plaf.basic;
  11. import java.awt.Dimension;
  12. import java.awt.Graphics;
  13. import java.awt.Color;
  14. import javax.swing.*;
  15. /**
  16. * JButton object that draws a scaled Arrow in one of the cardinal directions.
  17. * <p>
  18. * <strong>Warning:</strong>
  19. * Serialized objects of this class will not be compatible with
  20. * future Swing releases. The current serialization support is appropriate
  21. * for short term storage or RMI between applications running the same
  22. * version of Swing. A future release of Swing will provide support for
  23. * long term persistence.
  24. *
  25. * @version 1.19 02/02/00
  26. * @author David Kloba
  27. */
  28. public class BasicArrowButton extends JButton implements SwingConstants
  29. {
  30. protected int direction;
  31. public BasicArrowButton(int direction) {
  32. super();
  33. setRequestFocusEnabled(false);
  34. setDirection(direction);
  35. setBackground(UIManager.getColor("control"));
  36. }
  37. public int getDirection() { return direction; }
  38. public void setDirection(int dir) { direction = dir; }
  39. public void paint(Graphics g) {
  40. Color origColor;
  41. boolean isPressed, isEnabled;
  42. int w, h, size;
  43. w = getSize().width;
  44. h = getSize().height;
  45. origColor = g.getColor();
  46. isPressed = getModel().isPressed();
  47. isEnabled = isEnabled();
  48. g.setColor(getBackground());
  49. g.fillRect(1, 1, w-2, h-2);
  50. /// Draw the proper Border
  51. if (isPressed) {
  52. g.setColor(UIManager.getColor("controlShadow"));
  53. g.drawRect(0, 0, w-1, h-1);
  54. } else {
  55. // Using the background color set above
  56. g.drawLine(0, 0, 0, h-1);
  57. g.drawLine(1, 0, w-2, 0);
  58. g.setColor(UIManager.getColor("controlLtHighlight")); // inner 3D border
  59. g.drawLine(1, 1, 1, h-3);
  60. g.drawLine(2, 1, w-3, 1);
  61. g.setColor(UIManager.getColor("controlShadow")); // inner 3D border
  62. g.drawLine(1, h-2, w-2, h-2);
  63. g.drawLine(w-2, 1, w-2, h-3);
  64. g.setColor(UIManager.getColor("controlDkShadow")); // black drop shadow __|
  65. g.drawLine(0, h-1, w-1, h-1);
  66. g.drawLine(w-1, h-1, w-1, 0);
  67. }
  68. // If there's no room to draw arrow, bail
  69. if(h < 5 || w < 5) {
  70. g.setColor(origColor);
  71. return;
  72. }
  73. if (isPressed) {
  74. g.translate(1, 1);
  75. }
  76. // Draw the arrow
  77. size = Math.min((h - 4) / 3, (w - 4) / 3);
  78. size = Math.max(size, 2);
  79. paintTriangle(g, (w - size) / 2, (h - size) / 2,
  80. size, direction, isEnabled);
  81. // Reset the Graphics back to it's original settings
  82. if (isPressed) {
  83. g.translate(-1, -1);
  84. }
  85. g.setColor(origColor);
  86. }
  87. public Dimension getPreferredSize() {
  88. return new Dimension(16, 16);
  89. }
  90. public Dimension getMinimumSize() {
  91. return new Dimension(5, 5);
  92. }
  93. public Dimension getMaximumSize() {
  94. return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
  95. }
  96. public boolean isFocusTraversable() {
  97. return false;
  98. }
  99. public void paintTriangle(Graphics g, int x, int y, int size,
  100. int direction, boolean isEnabled) {
  101. Color oldColor = g.getColor();
  102. int mid, i, j;
  103. j = 0;
  104. size = Math.max(size, 2);
  105. mid = size / 2;
  106. g.translate(x, y);
  107. if(isEnabled)
  108. g.setColor(UIManager.getColor("controlDkShadow"));
  109. else
  110. g.setColor(UIManager.getColor("controlShadow"));
  111. switch(direction) {
  112. case NORTH:
  113. for(i = 0; i < size; i++) {
  114. g.drawLine(mid-i, i, mid+i, i);
  115. }
  116. if(!isEnabled) {
  117. g.setColor(UIManager.getColor("controlLtHighlight"));
  118. g.drawLine(mid-i+2, i, mid+i, i);
  119. }
  120. break;
  121. case SOUTH:
  122. if(!isEnabled) {
  123. g.translate(1, 1);
  124. g.setColor(UIManager.getColor("controlLtHighlight"));
  125. for(i = size-1; i >= 0; i--) {
  126. g.drawLine(mid-i, j, mid+i, j);
  127. j++;
  128. }
  129. g.translate(-1, -1);
  130. g.setColor(UIManager.getColor("controlShadow"));
  131. }
  132. j = 0;
  133. for(i = size-1; i >= 0; i--) {
  134. g.drawLine(mid-i, j, mid+i, j);
  135. j++;
  136. }
  137. break;
  138. case WEST:
  139. for(i = 0; i < size; i++) {
  140. g.drawLine(i, mid-i, i, mid+i);
  141. }
  142. if(!isEnabled) {
  143. g.setColor(UIManager.getColor("controlLtHighlight"));
  144. g.drawLine(i, mid-i+2, i, mid+i);
  145. }
  146. break;
  147. case EAST:
  148. if(!isEnabled) {
  149. g.translate(1, 1);
  150. g.setColor(UIManager.getColor("controlLtHighlight"));
  151. for(i = size-1; i >= 0; i--) {
  152. g.drawLine(j, mid-i, j, mid+i);
  153. j++;
  154. }
  155. g.translate(-1, -1);
  156. g.setColor(UIManager.getColor("controlShadow"));
  157. }
  158. j = 0;
  159. for(i = size-1; i >= 0; i--) {
  160. g.drawLine(j, mid-i, j, mid+i);
  161. j++;
  162. }
  163. break;
  164. }
  165. g.translate(-x, -y);
  166. g.setColor(oldColor);
  167. }
  168. }