1. package org.jr.awt.icon;
  2. /**
  3. * Copyright: Copyright (c) 2002-2004
  4. * Company: JavaResearch(http://www.javaresearch.org)
  5. * 最后更新日期:2003年2月16日
  6. * @author Cherami
  7. */
  8. import java.awt.*;
  9. import java.awt.image.*;
  10. import javax.swing.*;
  11. import org.jr.awt.image.*;
  12. /**
  13. * 灰度变换图标。
  14. * @since 0.1
  15. */
  16. public class GrayIcon
  17. implements Icon {
  18. Icon icon;
  19. Image image;
  20. /**
  21. * 根据原图标构造一个GrayIcon。
  22. * @param icon 原图标
  23. * @since 0.1
  24. */
  25. public GrayIcon(Icon icon) {
  26. this.icon = icon;
  27. }
  28. /**
  29. * 绘制图标
  30. * @param c 绘制组件
  31. * @param g 图形设备
  32. * @param x 绘制的x坐标的起始点
  33. * @param y 绘制的y坐标的起始点
  34. * @since 0.1
  35. */
  36. public void paintIcon(Component c, Graphics g, int x, int y) {
  37. if (image == null) {
  38. Image orgImage = c.createImage(getIconWidth(), getIconHeight());
  39. Graphics imageG = orgImage.getGraphics();
  40. Color background = c.getBackground();
  41. imageG.setColor(background);
  42. imageG.fillRect(0, 0, getIconWidth(), getIconHeight());
  43. icon.paintIcon(c, imageG, x, y);
  44. ImageFilter colorfilter = new org.jr.awt.image.GrayFilter(GrayModel.
  45. CS_AVERAGE);
  46. image = c.createImage(
  47. new FilteredImageSource(orgImage.getSource(), colorfilter));
  48. }
  49. g.drawImage(image, x, y, null);
  50. }
  51. /**
  52. * 图标宽度。
  53. * @return 图标宽度
  54. * @since 0.1
  55. */
  56. public int getIconWidth() {
  57. return icon.getIconWidth();
  58. }
  59. /**
  60. * 图标高度。
  61. * @return 图标高度
  62. * @since 0.1
  63. */
  64. public int getIconHeight() {
  65. return icon.getIconHeight();
  66. }
  67. }