1 41 42 package org.jfree.chart.ui; 43 44 import java.awt.BasicStroke ; 45 import java.awt.Component ; 46 import java.awt.Dimension ; 47 import java.awt.Graphics ; 48 import java.awt.Graphics2D ; 49 import java.awt.Insets ; 50 import java.awt.RenderingHints ; 51 import java.awt.geom.Line2D ; 52 53 import javax.swing.JComponent ; 54 import javax.swing.JList ; 55 import javax.swing.ListCellRenderer ; 56 57 58 63 public class PaletteSample extends JComponent implements ListCellRenderer { 64 65 66 private ColorPalette palette; 67 68 69 private Dimension preferredSize; 70 71 76 public PaletteSample(ColorPalette palette) { 77 this.palette = palette; 78 this.preferredSize = new Dimension (80, 18); 79 } 80 81 95 public Component getListCellRendererComponent(JList list, Object value, 96 int index, 97 boolean isSelected, 98 boolean cellHasFocus) { 99 if (value instanceof PaletteSample) { 100 PaletteSample in = (PaletteSample) value; 101 setPalette(in.getPalette()); 102 } 103 return this; 104 } 105 106 111 public ColorPalette getPalette() { 112 return this.palette; 113 } 114 115 120 public Dimension getPreferredSize() { 121 return this.preferredSize; 122 } 123 124 129 public void paintComponent(Graphics g) { 130 131 Graphics2D g2 = (Graphics2D ) g; 132 g2.setRenderingHint( 133 RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF 134 ); 135 Dimension size = getSize(); 136 Insets insets = getInsets(); 137 double ww = size.getWidth() - insets.left - insets.right; 138 double hh = size.getHeight() - insets.top - insets.bottom; 139 140 g2.setStroke(new BasicStroke (1.0f)); 141 142 double y1 = insets.top; 143 double y2 = y1 + hh; 144 double xx = insets.left; 145 Line2D line = new Line2D.Double (); 146 int count = 0; 147 while (xx <= insets.left + ww) { 148 count++; 149 line.setLine(xx, y1, xx, y2); 150 g2.setPaint(this.palette.getColor(count)); 151 g2.draw(line); 152 xx += 1; 153 } 154 } 155 156 161 public void setPalette(ColorPalette palette) { 162 this.palette = palette; 163 this.repaint(); 164 } 165 166 } 167 | Popular Tags |