1 44 45 package org.jfree.ui; 46 47 import java.awt.BasicStroke ; 48 import java.awt.Component ; 49 import java.awt.Dimension ; 50 import java.awt.Graphics ; 51 import java.awt.Graphics2D ; 52 import java.awt.Insets ; 53 import java.awt.RenderingHints ; 54 import java.awt.Stroke ; 55 import java.awt.geom.Ellipse2D ; 56 import java.awt.geom.Line2D ; 57 import java.awt.geom.Point2D ; 58 59 import javax.swing.JComponent ; 60 import javax.swing.JList ; 61 import javax.swing.ListCellRenderer ; 62 63 68 public class StrokeSample extends JComponent implements ListCellRenderer { 69 70 71 private Stroke stroke; 72 73 74 private Dimension preferredSize; 75 76 81 public StrokeSample(final Stroke stroke) { 82 this.stroke = stroke; 83 this.preferredSize = new Dimension (80, 18); 84 } 85 86 91 public Stroke getStroke() { 92 return this.stroke; 93 } 94 95 100 public void setStroke(final Stroke stroke) { 101 this.stroke = stroke; 102 repaint(); 103 } 104 105 110 public Dimension getPreferredSize() { 111 return this.preferredSize; 112 } 113 114 119 public void paintComponent(final Graphics g) { 120 121 final Graphics2D g2 = (Graphics2D ) g; 122 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 123 final Dimension size = getSize(); 124 final Insets insets = getInsets(); 125 final double xx = insets.left; 126 final double yy = insets.top; 127 final double ww = size.getWidth() - insets.left - insets.right; 128 final double hh = size.getHeight() - insets.top - insets.bottom; 129 130 final Point2D one = new Point2D.Double (xx + 6, yy + hh / 2); 132 final Point2D two = new Point2D.Double (xx + ww - 6, yy + hh / 2); 134 final Ellipse2D circle1 = new Ellipse2D.Double (one.getX() - 5, one.getY() - 5, 10, 10); 136 final Ellipse2D circle2 = new Ellipse2D.Double (two.getX() - 6, two.getY() - 5, 10, 10); 137 138 g2.draw(circle1); 140 g2.fill(circle1); 141 g2.draw(circle2); 142 g2.fill(circle2); 143 144 final Line2D line = new Line2D.Double (one, two); 146 if (this.stroke != null) { 147 g2.setStroke(this.stroke); 148 } 149 else { 150 g2.setStroke(new BasicStroke (0.0f)); 151 } 152 g2.draw(line); 153 154 } 155 156 168 public Component getListCellRendererComponent(final JList list, final Object value, final int index, 169 final boolean isSelected, final boolean cellHasFocus) { 170 if (value instanceof StrokeSample) { 171 final StrokeSample in = (StrokeSample) value; 172 setStroke(in.getStroke()); 173 } 174 return this; 175 } 176 177 } 178 | Popular Tags |