1:
52:
53: package ;
54:
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63:
64: import ;
65: import ;
66: import ;
67: import ;
68: import ;
69: import ;
70: import ;
71:
72:
79: public class ColorBar implements Cloneable, Serializable {
80:
81:
82: private static final long serialVersionUID = -2101776212647268103L;
83:
84:
85: public static final int DEFAULT_COLORBAR_THICKNESS = 0;
86:
87:
88: public static final double DEFAULT_COLORBAR_THICKNESS_PERCENT = 0.10;
89:
90:
91: public static final int DEFAULT_OUTERGAP = 2;
92:
93:
94: private ValueAxis axis;
95:
96:
97: private int colorBarThickness = DEFAULT_COLORBAR_THICKNESS;
98:
99:
102: private double colorBarThicknessPercent
103: = DEFAULT_COLORBAR_THICKNESS_PERCENT;
104:
105:
106: private ColorPalette colorPalette = null;
107:
108:
109: private int colorBarLength = 0;
110:
111:
112: private int outerGap;
113:
114:
120: public ColorBar(String label) {
121:
122: NumberAxis a = new NumberAxis(label);
123: a.setAutoRangeIncludesZero(false);
124: this.axis = a;
125: this.axis.setLowerMargin(0.0);
126: this.axis.setUpperMargin(0.0);
127:
128: this.colorPalette = new RainbowPalette();
129: this.colorBarThickness = DEFAULT_COLORBAR_THICKNESS;
130: this.colorBarThicknessPercent = DEFAULT_COLORBAR_THICKNESS_PERCENT;
131: this.outerGap = DEFAULT_OUTERGAP;
132: this.colorPalette.setMinZ(this.axis.getRange().getLowerBound());
133: this.colorPalette.setMaxZ(this.axis.getRange().getUpperBound());
134:
135: }
136:
137:
142: public void configure(ContourPlot plot) {
143: double minZ = plot.getDataset().getMinZValue();
144: double maxZ = plot.getDataset().getMaxZValue();
145: setMinimumValue(minZ);
146: setMaximumValue(maxZ);
147: }
148:
149:
154: public ValueAxis getAxis() {
155: return this.axis;
156: }
157:
158:
163: public void setAxis(ValueAxis axis) {
164: this.axis = axis;
165: }
166:
167:
170: public void autoAdjustRange() {
171: this.axis.autoAdjustRange();
172: this.colorPalette.setMinZ(this.axis.getLowerBound());
173: this.colorPalette.setMaxZ(this.axis.getUpperBound());
174: }
175:
176:
190: public double draw(Graphics2D g2, double cursor,
191: Rectangle2D plotArea, Rectangle2D dataArea,
192: Rectangle2D reservedArea, RectangleEdge edge) {
193:
194:
195: Rectangle2D colorBarArea = null;
196:
197: double thickness = calculateBarThickness(dataArea, edge);
198: if (this.colorBarThickness > 0) {
199: thickness = this.colorBarThickness;
200: }
201:
202: double length = 0.0;
203: if (RectangleEdge.isLeftOrRight(edge)) {
204: length = dataArea.getHeight();
205: }
206: else {
207: length = dataArea.getWidth();
208: }
209:
210: if (this.colorBarLength > 0) {
211: length = this.colorBarLength;
212: }
213:
214: if (edge == RectangleEdge.BOTTOM) {
215: colorBarArea = new Rectangle2D.Double(dataArea.getX(),
216: plotArea.getMaxY() + this.outerGap, length, thickness);
217: }
218: else if (edge == RectangleEdge.TOP) {
219: colorBarArea = new Rectangle2D.Double(dataArea.getX(),
220: reservedArea.getMinY() + this.outerGap, length, thickness);
221: }
222: else if (edge == RectangleEdge.LEFT) {
223: colorBarArea = new Rectangle2D.Double(plotArea.getX() - thickness
224: - this.outerGap, dataArea.getMinY(), thickness, length);
225: }
226: else if (edge == RectangleEdge.RIGHT) {
227: colorBarArea = new Rectangle2D.Double(plotArea.getMaxX()
228: + this.outerGap, dataArea.getMinY(), thickness, length);
229: }
230:
231:
232: this.axis.refreshTicks(g2, new AxisState(), colorBarArea, edge);
233:
234: drawColorBar(g2, colorBarArea, edge);
235:
236: AxisState state = null;
237: if (edge == RectangleEdge.TOP) {
238: cursor = colorBarArea.getMinY();
239: state = this.axis.draw(g2, cursor, reservedArea, colorBarArea,
240: RectangleEdge.TOP, null);
241: }
242: else if (edge == RectangleEdge.BOTTOM) {
243: cursor = colorBarArea.getMaxY();
244: state = this.axis.draw(g2, cursor, reservedArea, colorBarArea,
245: RectangleEdge.BOTTOM, null);
246: }
247: else if (edge == RectangleEdge.LEFT) {
248: cursor = colorBarArea.getMinX();
249: state = this.axis.draw(g2, cursor, reservedArea, colorBarArea,
250: RectangleEdge.LEFT, null);
251: }
252: else if (edge == RectangleEdge.RIGHT) {
253: cursor = colorBarArea.getMaxX();
254: state = this.axis.draw(g2, cursor, reservedArea, colorBarArea,
255: RectangleEdge.RIGHT, null);
256: }
257: return state.getCursor();
258:
259: }
260:
261:
269: public void drawColorBar(Graphics2D g2, Rectangle2D colorBarArea,
270: RectangleEdge edge) {
271:
272: Object antiAlias = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
273: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
274: RenderingHints.VALUE_ANTIALIAS_OFF);
275:
276:
277:
278:
279: Stroke strokeSaved = g2.getStroke();
280: g2.setStroke(new BasicStroke(1.0f));
281:
282: if (RectangleEdge.isTopOrBottom(edge)) {
283: double y1 = colorBarArea.getY();
284: double y2 = colorBarArea.getMaxY();
285: double xx = colorBarArea.getX();
286: Line2D line = new Line2D.Double();
287: while (xx <= colorBarArea.getMaxX()) {
288: double value = this.axis.java2DToValue(xx, colorBarArea, edge);
289: line.setLine(xx, y1, xx, y2);
290: g2.setPaint(getPaint(value));
291: g2.draw(line);
292: xx += 1;
293: }
294: }
295: else {
296: double y1 = colorBarArea.getX();
297: double y2 = colorBarArea.getMaxX();
298: double xx = colorBarArea.getY();
299: Line2D line = new Line2D.Double();
300: while (xx <= colorBarArea.getMaxY()) {
301: double value = this.axis.java2DToValue(xx, colorBarArea, edge);
302: line.setLine(y1, xx, y2, xx);
303: g2.setPaint(getPaint(value));
304: g2.draw(line);
305: xx += 1;
306: }
307: }
308:
309: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antiAlias);
310: g2.setStroke(strokeSaved);
311:
312: }
313:
314:
319: public ColorPalette getColorPalette() {
320: return this.colorPalette;
321: }
322:
323:
330: public Paint getPaint(double value) {
331: return this.colorPalette.getPaint(value);
332: }
333:
334:
339: public void setColorPalette(ColorPalette palette) {
340: this.colorPalette = palette;
341: }
342:
343:
348: public void setMaximumValue(double value) {
349: this.colorPalette.setMaxZ(value);
350: this.axis.setUpperBound(value);
351: }
352:
353:
358: public void setMinimumValue(double value) {
359: this.colorPalette.setMinZ(value);
360: this.axis.setLowerBound(value);
361: }
362:
363:
375: public AxisSpace reserveSpace(Graphics2D g2, Plot plot,
376: Rectangle2D plotArea,
377: Rectangle2D dataArea, RectangleEdge edge,
378: AxisSpace space) {
379:
380: AxisSpace result = this.axis.reserveSpace(g2, plot, plotArea, edge,
381: space);
382: double thickness = calculateBarThickness(dataArea, edge);
383: result.add(thickness + 2 * this.outerGap, edge);
384: return result;
385:
386: }
387:
388:
396: private double calculateBarThickness(Rectangle2D plotArea,
397: RectangleEdge edge) {
398: double result = 0.0;
399: if (RectangleEdge.isLeftOrRight(edge)) {
400: result = plotArea.getWidth() * this.colorBarThicknessPercent;
401: }
402: else {
403: result = plotArea.getHeight() * this.colorBarThicknessPercent;
404: }
405: return result;
406: }
407:
408:
416: public Object clone() throws CloneNotSupportedException {
417:
418: ColorBar clone = (ColorBar) super.clone();
419: clone.axis = (ValueAxis) this.axis.clone();
420: return clone;
421:
422: }
423:
424:
431: public boolean equals(Object obj) {
432:
433: if (obj == this) {
434: return true;
435: }
436: if (!(obj instanceof ColorBar)) {
437: return false;
438: }
439: ColorBar that = (ColorBar) obj;
440: if (!this.axis.equals(that.axis)) {
441: return false;
442: }
443: if (this.colorBarThickness != that.colorBarThickness) {
444: return false;
445: }
446: if (this.colorBarThicknessPercent != that.colorBarThicknessPercent) {
447: return false;
448: }
449: if (!this.colorPalette.equals(that.colorPalette)) {
450: return false;
451: }
452: if (this.colorBarLength != that.colorBarLength) {
453: return false;
454: }
455: if (this.outerGap != that.outerGap) {
456: return false;
457: }
458: return true;
459:
460: }
461:
462:
467: public int hashCode() {
468: return this.axis.hashCode();
469: }
470:
471: }