1 42 43 package org.jfree.chart.editor; 44 45 import java.awt.BasicStroke ; 46 import java.awt.Component ; 47 import java.awt.Dimension ; 48 import java.awt.Graphics ; 49 import java.awt.Graphics2D ; 50 import java.awt.Insets ; 51 import java.awt.RenderingHints ; 52 import java.awt.geom.Line2D ; 53 54 import javax.swing.JComponent ; 55 import javax.swing.JList ; 56 import javax.swing.ListCellRenderer ; 57 58 import org.jfree.chart.plot.ColorPalette; 59 60 61 66 public class PaletteSample extends JComponent implements ListCellRenderer { 67 68 69 private ColorPalette palette; 70 71 72 private Dimension preferredSize; 73 74 79 public PaletteSample(ColorPalette palette) { 80 this.palette = palette; 81 this.preferredSize = new Dimension (80, 18); 82 } 83 84 98 public Component getListCellRendererComponent(JList list, Object value, 99 int index, 100 boolean isSelected, 101 boolean cellHasFocus) { 102 if (value instanceof PaletteSample) { 103 PaletteSample in = (PaletteSample) value; 104 setPalette(in.getPalette()); 105 } 106 return this; 107 } 108 109 114 public ColorPalette getPalette() { 115 return this.palette; 116 } 117 118 123 public Dimension getPreferredSize() { 124 return this.preferredSize; 125 } 126 127 132 public void paintComponent(Graphics g) { 133 134 Graphics2D g2 = (Graphics2D ) g; 135 g2.setRenderingHint( 136 RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF 137 ); 138 Dimension size = getSize(); 139 Insets insets = getInsets(); 140 double ww = size.getWidth() - insets.left - insets.right; 141 double hh = size.getHeight() - insets.top - insets.bottom; 142 143 g2.setStroke(new BasicStroke (1.0f)); 144 145 double y1 = insets.top; 146 double y2 = y1 + hh; 147 double xx = insets.left; 148 Line2D line = new Line2D.Double (); 149 int count = 0; 150 while (xx <= insets.left + ww) { 151 count++; 152 line.setLine(xx, y1, xx, y2); 153 g2.setPaint(this.palette.getColor(count)); 154 g2.draw(line); 155 xx += 1; 156 } 157 } 158 159 164 public void setPalette(ColorPalette palette) { 165 this.palette = palette; 166 this.repaint(); 167 } 168 169 } 170 | Popular Tags |