1 19 package jcckit.plot; 20 21 import java.awt.Color ; 22 23 import jcckit.graphic.ClippingShape; 24 import jcckit.graphic.GraphPoint; 25 import jcckit.graphic.GraphicalComposite; 26 import jcckit.graphic.GraphicalElement; 27 import jcckit.graphic.LineAttributes; 28 import jcckit.graphic.Polygon; 29 import jcckit.graphic.ShapeAttributes; 30 import jcckit.util.ConfigParameters; 31 import jcckit.util.Factory; 32 33 38 public class SimpleCurve implements Curve { 39 40 public static final String SYMBOL_FACTORY_KEY = "symbolFactory", 41 WITH_LINE_KEY = "withLine", 42 SOFT_CLIPPING_KEY = "softClipping", 43 LINE_ATTRIBUTES_KEY = "lineAttributes", 44 INITIAL_HINT_FOR_NEXT_POINT_KEY 45 = "initialHintForNextPoint"; 46 private final ClippingShape _clippingShape; 47 private final SymbolFactory _symbolFactory; 48 private final GraphicalComposite _symbols; 49 private final GraphicalComposite _completeCurve; 50 private final GraphicalElement _legendSymbol; 51 private final Hint _initialHintForNextPoint; 52 private final Polygon _curve; 53 private final boolean _softClipping; 54 private Hint _hintForNextPoint; 55 56 102 public SimpleCurve(ConfigParameters config, int curveIndex, 103 int numberOfCurves, ClippingShape clippingShape, 104 Legend legend) { 105 _symbolFactory = (SymbolFactory) Factory.createOrGet( 106 config.getNode(SYMBOL_FACTORY_KEY), null); 107 boolean withLine = config.getBoolean(WITH_LINE_KEY, true); 108 LineAttributes lineAttributes = (LineAttributes) Factory.createOrGet( 109 config.getNode(LINE_ATTRIBUTES_KEY), 110 new ShapeAttributes(null, Color.getHSBColor((curveIndex % 6) / 6f, 111 1f, 0.8f), 112 0, null)); 113 if (_symbolFactory != null || withLine) { 114 _clippingShape = clippingShape; 115 _completeCurve = new GraphicalComposite(null); 116 if (withLine) { 117 GraphicalComposite container = new GraphicalComposite(clippingShape); 118 _curve = new Polygon(lineAttributes, false); 119 container.addElement(_curve); 120 _completeCurve.addElement(container); 121 } else { 122 _curve = null; 123 } 124 _softClipping = config.getBoolean(SOFT_CLIPPING_KEY, true); 125 if (_symbolFactory != null) { 126 _symbols = new GraphicalComposite(_softClipping ? null 127 : clippingShape); 128 _completeCurve.addElement(_symbols); 129 } else { 130 _symbols = null; 131 } 132 } else { 133 throw new IllegalArgumentException ( 134 "Either a SymbolFactory must exist or withLines == true."); 135 } 136 _hintForNextPoint = _initialHintForNextPoint 137 = (Hint) Factory.createOrGet( 138 config.getNode(INITIAL_HINT_FOR_NEXT_POINT_KEY), null); 139 _legendSymbol = legend.createSymbol(curveIndex, numberOfCurves, 140 _symbolFactory, withLine, 141 lineAttributes); 142 } 143 144 148 public GraphicalElement getView() { 149 return _completeCurve; 150 } 151 152 153 public GraphicalElement getLegendSymbol() { 154 return _legendSymbol; 155 } 156 157 158 public Hint addPoint(GraphPoint point, Hint hintFromPreviousCurve) { 159 if (_curve != null) { 160 _curve.addPoint(point); 161 } 162 Hint hintForNextCurve = hintFromPreviousCurve; 163 if (_symbolFactory != null) { 164 Symbol symbol = _symbolFactory.createSymbol(point, _hintForNextPoint, 165 hintFromPreviousCurve); 166 if (_clippingShape == null || !_softClipping 167 || _clippingShape.isInside(point)) { 168 _symbols.addElement(symbol.getSymbol()); 169 } 170 _hintForNextPoint = symbol.getHintForNextPoint(); 171 hintForNextCurve = symbol.getHintForNextCurve(); 172 } 173 return hintForNextCurve; 174 } 175 176 public void removeAllPoints() { 177 if (_curve != null) { 178 _curve.removeAllPoints(); 179 } 180 if (_symbols != null) { 181 _symbols.removeAllElements(); 182 } 183 _hintForNextPoint = _initialHintForNextPoint; 184 } 185 } 186 | Popular Tags |