1:
54:
55: package ;
56:
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63: import ;
64: import ;
65:
66: import ;
67: import ;
68: import ;
69: import ;
70: import ;
71: import ;
72: import ;
73:
74:
81: public class ColorBar implements Cloneable, Serializable {
82:
83:
84: private static final long serialVersionUID = -2101776212647268103L;
85:
86:
87: public static final int DEFAULT_COLORBAR_THICKNESS = 0;
88:
89:
90: public static final double DEFAULT_COLORBAR_THICKNESS_PERCENT = 0.10;
91:
92:
93: public static final int DEFAULT_OUTERGAP = 2;
94:
95:
96: private ValueAxis axis;
97:
98:
99: private int colorBarThickness = DEFAULT_COLORBAR_THICKNESS;
100:
101:
104: private double colorBarThicknessPercent
105: = DEFAULT_COLORBAR_THICKNESS_PERCENT;
106:
107:
108: private ColorPalette colorPalette = null;
109:
110:
111: private int colorBarLength = 0;
112:
113:
114: private int outerGap;
115:
116:
122: public ColorBar(String label) {
123:
124: NumberAxis a = new NumberAxis(label);
125: a.setAutoRangeIncludesZero(false);
126: this.axis = a;
127: this.axis.setLowerMargin(0.0);
128: this.axis.setUpperMargin(0.0);
129:
130: this.colorPalette = new RainbowPalette();
131: this.colorBarThickness = DEFAULT_COLORBAR_THICKNESS;
132: this.colorBarThicknessPercent = DEFAULT_COLORBAR_THICKNESS_PERCENT;
133: this.outerGap = DEFAULT_OUTERGAP;
134: this.colorPalette.setMinZ(this.axis.getRange().getLowerBound());
135: this.colorPalette.setMaxZ(this.axis.getRange().getUpperBound());
136:
137: }
138:
139:
144: public void configure(ContourPlot plot) {
145: double minZ = plot.getDataset().getMinZValue();
146: double maxZ = plot.getDataset().getMaxZValue();
147: setMinimumValue(minZ);
148: setMaximumValue(maxZ);
149: }
150:
151:
156: public ValueAxis getAxis() {
157: return this.axis;
158: }
159:
160:
165: public void setAxis(ValueAxis axis) {
166: this.axis = axis;
167: }
168:
169:
172: public void autoAdjustRange() {
173: this.axis.autoAdjustRange();
174: this.colorPalette.setMinZ(this.axis.getLowerBound());
175: this.colorPalette.setMaxZ(this.axis.getUpperBound());
176: }
177:
178:
192: public double draw(Graphics2D g2, double cursor,
193: Rectangle2D plotArea, Rectangle2D dataArea,
194: Rectangle2D reservedArea, RectangleEdge edge) {
195:
196:
197: Rectangle2D colorBarArea = null;
198:
199: double thickness = calculateBarThickness(dataArea, edge);
200: if (this.colorBarThickness > 0) {
201: thickness = this.colorBarThickness;
202: }
203:
204: double length = 0.0;
205: if (RectangleEdge.isLeftOrRight(edge)) {
206: length = dataArea.getHeight();
207: }
208: else {
209: length = dataArea.getWidth();
210: }
211:
212: if (this.colorBarLength > 0) {
213: length = this.colorBarLength;
214: }
215:
216: if (edge == RectangleEdge.BOTTOM) {
217: colorBarArea = new Rectangle2D.Double(
218: dataArea.getX(), plotArea.getMaxY() + this.outerGap,
219: length, thickness
220: );
221: }
222: else if (edge == RectangleEdge.TOP) {
223: colorBarArea = new Rectangle2D.Double(
224: dataArea.getX(), reservedArea.getMinY() + this.outerGap,
225: length, thickness
226: );
227: }
228: else if (edge == RectangleEdge.LEFT) {
229: colorBarArea = new Rectangle2D.Double(
230: plotArea.getX() - thickness - this.outerGap ,
231: dataArea.getMinY(), thickness, length
232: );
233: }
234: else if (edge == RectangleEdge.RIGHT) {
235: colorBarArea = new Rectangle2D.Double(
236: plotArea.getMaxX() + this.outerGap, dataArea.getMinY(),
237: thickness, length
238: );
239: }
240:
241:
242: this.axis.refreshTicks(
243: g2, new AxisState(), colorBarArea, edge
244: );
245:
246: drawColorBar(g2, colorBarArea, edge);
247:
248: AxisState state = null;
249: if (edge == RectangleEdge.TOP) {
250: cursor = colorBarArea.getMinY();
251: state = this.axis.draw(
252: g2, cursor, reservedArea, colorBarArea, RectangleEdge.TOP, null
253: );
254: }
255: else if (edge == RectangleEdge.BOTTOM) {
256: cursor = colorBarArea.getMaxY();
257: state = this.axis.draw(
258: g2, cursor, reservedArea, colorBarArea, RectangleEdge.BOTTOM,
259: null
260: );
261: }
262: else if (edge == RectangleEdge.LEFT) {
263: cursor = colorBarArea.getMinX();
264: state = this.axis.draw(
265: g2, cursor, reservedArea, colorBarArea, RectangleEdge.LEFT, null
266: );
267: }
268: else if (edge == RectangleEdge.RIGHT) {
269: cursor = colorBarArea.getMaxX();
270: state = this.axis.draw(
271: g2, cursor, reservedArea, colorBarArea, RectangleEdge.RIGHT,
272: null
273: );
274: }
275: return state.getCursor();
276:
277: }
278:
279:
287: public void drawColorBar(Graphics2D g2, Rectangle2D colorBarArea,
288: RectangleEdge edge) {
289:
290: Object antiAlias = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
291: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
292: RenderingHints.VALUE_ANTIALIAS_OFF);
293:
294:
295:
296:
297: Stroke strokeSaved = g2.getStroke();
298: g2.setStroke(new BasicStroke(1.0f));
299:
300: if (RectangleEdge.isTopOrBottom(edge)) {
301: double y1 = colorBarArea.getY();
302: double y2 = colorBarArea.getMaxY();
303: double xx = colorBarArea.getX();
304: Line2D line = new Line2D.Double();
305: while (xx <= colorBarArea.getMaxX()) {
306: double value = this.axis.java2DToValue(xx, colorBarArea, edge);
307: line.setLine(xx, y1, xx, y2);
308: g2.setPaint(getPaint(value));
309: g2.draw(line);
310: xx += 1;
311: }
312: }
313: else {
314: double y1 = colorBarArea.getX();
315: double y2 = colorBarArea.getMaxX();
316: double xx = colorBarArea.getY();
317: Line2D line = new Line2D.Double();
318: while (xx <= colorBarArea.getMaxY()) {
319: double value = this.axis.java2DToValue(xx, colorBarArea, edge);
320: line.setLine(y1, xx, y2, xx);
321: g2.setPaint(getPaint(value));
322: g2.draw(line);
323: xx += 1;
324: }
325: }
326:
327: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antiAlias);
328: g2.setStroke(strokeSaved);
329:
330: }
331:
332:
337: public ColorPalette getColorPalette() {
338: return this.colorPalette;
339: }
340:
341:
348: public Paint getPaint(double value) {
349: return this.colorPalette.getPaint(value);
350: }
351:
352:
357: public void setColorPalette(ColorPalette palette) {
358: this.colorPalette = palette;
359: }
360:
361:
366: public void setMaximumValue(double value) {
367: this.colorPalette.setMaxZ(value);
368: this.axis.setUpperBound(value);
369: }
370:
371:
376: public void setMinimumValue(double value) {
377: this.colorPalette.setMinZ(value);
378: this.axis.setLowerBound(value);
379: }
380:
381:
393: public AxisSpace reserveSpace(Graphics2D g2, Plot plot,
394: Rectangle2D plotArea,
395: Rectangle2D dataArea, RectangleEdge edge,
396: AxisSpace space) {
397:
398: AxisSpace result = this.axis.reserveSpace(
399: g2, plot, plotArea, edge, space
400: );
401: double thickness = calculateBarThickness(dataArea, edge);
402: result.add(thickness + 2 * this.outerGap, edge);
403: return result;
404:
405: }
406:
407:
415: private double calculateBarThickness(Rectangle2D plotArea,
416: RectangleEdge edge) {
417: double result = 0.0;
418: if (RectangleEdge.isLeftOrRight(edge)) {
419: result = plotArea.getWidth() * this.colorBarThicknessPercent;
420: }
421: else {
422: result = plotArea.getHeight() * this.colorBarThicknessPercent;
423: }
424: return result;
425: }
426:
427:
435: public Object clone() throws CloneNotSupportedException {
436:
437: ColorBar clone = (ColorBar) super.clone();
438: clone.axis = (ValueAxis) this.axis.clone();
439: return clone;
440:
441: }
442:
443:
450: public boolean equals(Object obj) {
451:
452: if (obj == this) {
453: return true;
454: }
455: if (!(obj instanceof ColorBar)) {
456: return false;
457: }
458: ColorBar that = (ColorBar) obj;
459: if (!this.axis.equals(that.axis)) {
460: return false;
461: }
462: if (this.colorBarThickness != that.colorBarThickness) {
463: return false;
464: }
465: if (this.colorBarThicknessPercent != that.colorBarThicknessPercent) {
466: return false;
467: }
468: if (!this.colorPalette.equals(that.colorPalette)) {
469: return false;
470: }
471: if (this.colorBarLength != that.colorBarLength) {
472: return false;
473: }
474: if (this.outerGap != that.outerGap) {
475: return false;
476: }
477: return true;
478:
479: }
480:
481:
486: public int hashCode() {
487: return this.axis.hashCode();
488: }
489:
490: }