1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jfreechart/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * ------------------
  25. * PaletteSample.java
  26. * ------------------
  27. * (C) Copyright 2002-2004, by David M. O'Donnell.
  28. *
  29. * Original Author: David M. O'Donnell;
  30. * Contributor(s): David Gilbert (for Object Refinery Limited);
  31. *
  32. * $Id: PaletteSample.java,v 1.1 2004/08/31 14:54:21 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 21-Jan-2003 : Added standard header (DG);
  37. *
  38. */
  39. package org.jfree.chart.ui;
  40. import java.awt.BasicStroke;
  41. import java.awt.Component;
  42. import java.awt.Dimension;
  43. import java.awt.Graphics;
  44. import java.awt.Graphics2D;
  45. import java.awt.Insets;
  46. import java.awt.RenderingHints;
  47. import java.awt.geom.Line2D;
  48. import javax.swing.JComponent;
  49. import javax.swing.JList;
  50. import javax.swing.ListCellRenderer;
  51. /**
  52. * A panel that displays a palette sample.
  53. *
  54. * @author David M. O'Donnell
  55. */
  56. public class PaletteSample extends JComponent implements ListCellRenderer {
  57. /** The palette being displayed. */
  58. private ColorPalette palette;
  59. /** The preferred size of the component; */
  60. private Dimension preferredSize;
  61. /**
  62. * Creates a new sample.
  63. *
  64. * @param palette the palette.
  65. */
  66. public PaletteSample(ColorPalette palette) {
  67. this.palette = palette;
  68. this.preferredSize = new Dimension(80, 18);
  69. }
  70. /**
  71. * Returns a list cell renderer for the stroke, so the sample can be displayed in a list or
  72. * combo.
  73. *
  74. * @param list the list component.
  75. * @param value the value.
  76. * @param index the index.
  77. * @param isSelected a flag that indicates whether or not the item is selected.
  78. * @param cellHasFocus a flag that indicates whether or not the cell has the focus.
  79. *
  80. * @return The renderer.
  81. */
  82. public Component getListCellRendererComponent(JList list, Object value, int index,
  83. boolean isSelected, boolean cellHasFocus) {
  84. if (value instanceof PaletteSample) {
  85. PaletteSample in = (PaletteSample) value;
  86. setPalette(in.getPalette());
  87. }
  88. return this;
  89. }
  90. /**
  91. * Returns the current palette object being displayed.
  92. *
  93. * @return The palette.
  94. */
  95. public ColorPalette getPalette() {
  96. return this.palette;
  97. }
  98. /**
  99. * Returns the preferred size of the component.
  100. *
  101. * @return The preferred size.
  102. */
  103. public Dimension getPreferredSize() {
  104. return this.preferredSize;
  105. }
  106. /**
  107. * Draws the sample.
  108. *
  109. * @param g the graphics device.
  110. */
  111. public void paintComponent(Graphics g) {
  112. Graphics2D g2 = (Graphics2D) g;
  113. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
  114. Dimension size = getSize();
  115. Insets insets = getInsets();
  116. double ww = size.getWidth() - insets.left - insets.right;
  117. double hh = size.getHeight() - insets.top - insets.bottom;
  118. g2.setStroke(new BasicStroke(1.0f));
  119. double y1 = insets.top;
  120. double y2 = y1 + hh;
  121. double xx = insets.left;
  122. Line2D line = new Line2D.Double();
  123. int count = 0;
  124. while (xx <= insets.left + ww) {
  125. count++;
  126. line.setLine(xx, y1, xx, y2);
  127. g2.setPaint(this.palette.getColor(count));
  128. g2.draw(line);
  129. xx += 1;
  130. }
  131. }
  132. /**
  133. * Sets the palette object being displayed.
  134. *
  135. * @param palette the palette.
  136. */
  137. public void setPalette(ColorPalette palette) {
  138. this.palette = palette;
  139. this.repaint();
  140. }
  141. }