1. /*
  2. * @(#)MetalBumps.java 1.19 00/02/02
  3. *
  4. * Copyright 1998-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.metal;
  11. import java.awt.*;
  12. import javax.swing.*;
  13. import java.io.*;
  14. import java.util.*;
  15. /**
  16. * Implements the bumps used throughout the Metal Look and Feel.
  17. *
  18. * @version 1.19 02/02/00
  19. * @author Tom Santos
  20. * @author Steve Wilson
  21. */
  22. class MetalBumps implements Icon {
  23. protected int xBumps;
  24. protected int yBumps;
  25. protected Color topColor = MetalLookAndFeel.getPrimaryControlHighlight();
  26. protected Color shadowColor = MetalLookAndFeel.getPrimaryControlDarkShadow();
  27. protected Color backColor = MetalLookAndFeel.getPrimaryControlShadow();
  28. protected static Vector buffers = new Vector();
  29. protected BumpBuffer buffer;
  30. public MetalBumps( Dimension bumpArea ) {
  31. this( bumpArea.width, bumpArea.height );
  32. }
  33. public MetalBumps( int width, int height ) {
  34. setBumpArea( width, height );
  35. buffer = getBuffer( topColor, shadowColor, backColor );
  36. if ( buffer == null ) {
  37. createBuffer();
  38. }
  39. }
  40. public MetalBumps( int width, int height,
  41. Color newTopColor, Color newShadowColor, Color newBackColor ) {
  42. setBumpArea( width, height );
  43. setBumpColors( newTopColor, newShadowColor, newBackColor );
  44. buffer = getBuffer( topColor, shadowColor, backColor );
  45. if ( buffer == null ) {
  46. createBuffer();
  47. }
  48. }
  49. protected void createBuffer() {
  50. buffer = new BumpBuffer( topColor, shadowColor, backColor );
  51. buffers.addElement( buffer );
  52. }
  53. protected BumpBuffer getBuffer( Color aTopColor, Color aShadowColor, Color aBackColor ) {
  54. BumpBuffer result = null;
  55. Enumeration elements = buffers.elements();
  56. while ( elements.hasMoreElements() ) {
  57. BumpBuffer aBuffer = (BumpBuffer)elements.nextElement();
  58. if ( aBuffer.hasSameColors( aTopColor, aShadowColor, aBackColor ) ) {
  59. result = aBuffer;
  60. break;
  61. }
  62. }
  63. return result;
  64. }
  65. public void setBumpArea( Dimension bumpArea ) {
  66. setBumpArea( bumpArea.width, bumpArea.height );
  67. }
  68. public void setBumpArea( int width, int height ) {
  69. xBumps = width / 2;
  70. yBumps = height / 2;
  71. }
  72. public void setBumpColors( Color newTopColor, Color newShadowColor, Color newBackColor ) {
  73. topColor = newTopColor;
  74. shadowColor = newShadowColor;
  75. backColor = newBackColor;
  76. buffer = getBuffer( topColor, shadowColor, backColor );
  77. if ( buffer == null ) {
  78. createBuffer();
  79. }
  80. }
  81. public void paintIcon( Component c, Graphics g, int x, int y ) {
  82. int bufferWidth = buffer.getImageSize().width;
  83. int bufferHeight = buffer.getImageSize().height;
  84. int iconWidth = getIconWidth();
  85. int iconHeight = getIconHeight();
  86. int x2 = x + iconWidth;
  87. int y2 = y + iconHeight;
  88. int savex = x;
  89. while (y < y2) {
  90. int h = Math.min(y2 - y, bufferHeight);
  91. for (x = savex; x < x2; x += bufferWidth) {
  92. int w = Math.min(x2 - x, bufferWidth);
  93. g.drawImage(buffer.getImage(),
  94. x, y, x+w, y+h,
  95. 0, 0, w, h,
  96. null);
  97. }
  98. y += bufferHeight;
  99. }
  100. }
  101. public int getIconWidth() {
  102. return xBumps * 2;
  103. }
  104. public int getIconHeight() {
  105. return yBumps * 2;
  106. }
  107. }
  108. class BumpBuffer {
  109. static Frame frame;
  110. static Component component;
  111. static final int IMAGE_SIZE = 64;
  112. static Dimension imageSize = new Dimension( IMAGE_SIZE, IMAGE_SIZE );
  113. transient Image image;
  114. Color topColor;
  115. Color shadowColor;
  116. Color backColor;
  117. public BumpBuffer( Color aTopColor, Color aShadowColor, Color aBackColor ) {
  118. createComponent();
  119. image = getComponent().createImage( IMAGE_SIZE, IMAGE_SIZE );
  120. topColor = aTopColor;
  121. shadowColor = aShadowColor;
  122. backColor = aBackColor;
  123. fillBumpBuffer();
  124. }
  125. public boolean hasSameColors( Color aTopColor, Color aShadowColor, Color aBackColor ) {
  126. return topColor.equals( aTopColor ) &&
  127. shadowColor.equals( aShadowColor ) &&
  128. backColor.equals( aBackColor );
  129. }
  130. public Image getImage() {
  131. if (image == null) {
  132. image = getComponent().createImage( IMAGE_SIZE, IMAGE_SIZE );
  133. fillBumpBuffer();
  134. }
  135. return image;
  136. }
  137. public Dimension getImageSize() {
  138. return imageSize;
  139. }
  140. protected void fillBumpBuffer() {
  141. Graphics g = image.getGraphics();
  142. g.setColor( backColor );
  143. g.fillRect( 0, 0, IMAGE_SIZE, IMAGE_SIZE );
  144. g.setColor(topColor);
  145. for (int x = 0; x < IMAGE_SIZE; x+=4) {
  146. for (int y = 0; y < IMAGE_SIZE; y+=4) {
  147. g.drawLine( x, y, x, y );
  148. g.drawLine( x+2, y+2, x+2, y+2);
  149. }
  150. }
  151. g.setColor(shadowColor);
  152. for (int x = 0; x < IMAGE_SIZE; x+=4) {
  153. for (int y = 0; y < IMAGE_SIZE; y+=4) {
  154. g.drawLine( x+1, y+1, x+1, y+1 );
  155. g.drawLine( x+3, y+3, x+3, y+3);
  156. }
  157. }
  158. g.dispose();
  159. }
  160. protected Component getComponent() {return component;}
  161. protected void createComponent() {
  162. if (frame == null) {
  163. frame = new Frame( "bufferCreator" );
  164. }
  165. if (component == null ) {
  166. component = new Canvas();
  167. frame.add( component, BorderLayout.CENTER );
  168. }
  169. // fix for 4185993 (moved this outside if block)
  170. frame.addNotify();
  171. }
  172. }