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:
70:
75: public class ColorBar implements Cloneable, Serializable {
76:
77:
78: private static final long serialVersionUID = -2101776212647268103L;
79:
80:
81: public static final int DEFAULT_COLORBAR_THICKNESS = 0;
82:
83:
84: public static final double DEFAULT_COLORBAR_THICKNESS_PERCENT = 0.10;
85:
86:
87: public static final int DEFAULT_OUTERGAP = 2;
88:
89:
90: private ValueAxis axis;
91:
92:
93: private int colorBarThickness = DEFAULT_COLORBAR_THICKNESS;
94:
95:
98: private double colorBarThicknessPercent
99: = DEFAULT_COLORBAR_THICKNESS_PERCENT;
100:
101:
102: private ColorPalette colorPalette = null;
103:
104:
105: private int colorBarLength = 0;
106:
107:
108: private int outerGap;
109:
110:
116: public ColorBar(String label) {
117:
118: NumberAxis a = new NumberAxis(label);
119: a.setAutoRangeIncludesZero(false);
120: this.axis = a;
121: this.axis.setLowerMargin(0.0);
122: this.axis.setUpperMargin(0.0);
123:
124: this.colorPalette = new RainbowPalette();
125: this.colorBarThickness = DEFAULT_COLORBAR_THICKNESS;
126: this.colorBarThicknessPercent = DEFAULT_COLORBAR_THICKNESS_PERCENT;
127: this.outerGap = DEFAULT_OUTERGAP;
128: this.colorPalette.setMinZ(this.axis.getRange().getLowerBound());
129: this.colorPalette.setMaxZ(this.axis.getRange().getUpperBound());
130:
131: }
132:
133:
138: public void configure(ContourPlot plot) {
139: double minZ = plot.getDataset().getMinZValue();
140: double maxZ = plot.getDataset().getMaxZValue();
141: setMinimumValue(minZ);
142: setMaximumValue(maxZ);
143: }
144:
145:
150: public ValueAxis getAxis() {
151: return this.axis;
152: }
153:
154:
159: public void setAxis(ValueAxis axis) {
160: this.axis = axis;
161: }
162:
163:
166: public void autoAdjustRange() {
167: this.axis.autoAdjustRange();
168: this.colorPalette.setMinZ(this.axis.getLowerBound());
169: this.colorPalette.setMaxZ(this.axis.getUpperBound());
170: }
171:
172:
186: public double draw(Graphics2D g2, double cursor,
187: Rectangle2D plotArea, Rectangle2D dataArea,
188: Rectangle2D reservedArea, RectangleEdge edge) {
189:
190:
191: Rectangle2D colorBarArea = null;
192:
193: double thickness = calculateBarThickness(dataArea, edge);
194: if (this.colorBarThickness > 0) {
195: thickness = this.colorBarThickness;
196: }
197:
198: double length = 0.0;
199: if (RectangleEdge.isLeftOrRight(edge)) {
200: length = dataArea.getHeight();
201: }
202: else {
203: length = dataArea.getWidth();
204: }
205:
206: if (this.colorBarLength > 0) {
207: length = this.colorBarLength;
208: }
209:
210: if (edge == RectangleEdge.BOTTOM) {
211: colorBarArea = new Rectangle2D.Double(
212: dataArea.getX(), plotArea.getMaxY() + this.outerGap,
213: length, thickness
214: );
215: }
216: else if (edge == RectangleEdge.TOP) {
217: colorBarArea = new Rectangle2D.Double(
218: dataArea.getX(), reservedArea.getMinY() + this.outerGap,
219: length, thickness
220: );
221: }
222: else if (edge == RectangleEdge.LEFT) {
223: colorBarArea = new Rectangle2D.Double(
224: plotArea.getX() - thickness - this.outerGap ,
225: dataArea.getMinY(), thickness, length
226: );
227: }
228: else if (edge == RectangleEdge.RIGHT) {
229: colorBarArea = new Rectangle2D.Double(
230: plotArea.getMaxX() + this.outerGap, dataArea.getMinY(),
231: thickness, length
232: );
233: }
234:
235:
236: this.axis.refreshTicks(
237: g2, new AxisState(), colorBarArea, edge
238: );
239:
240: drawColorBar(g2, colorBarArea, edge);
241:
242: AxisState state = null;
243: if (edge == RectangleEdge.TOP) {
244: cursor = colorBarArea.getMinY();
245: state = this.axis.draw(
246: g2, cursor, reservedArea, colorBarArea, RectangleEdge.TOP, null
247: );
248: }
249: else if (edge == RectangleEdge.BOTTOM) {
250: cursor = colorBarArea.getMaxY();
251: state = this.axis.draw(
252: g2, cursor, reservedArea, colorBarArea, RectangleEdge.BOTTOM,
253: null
254: );
255: }
256: else if (edge == RectangleEdge.LEFT) {
257: cursor = colorBarArea.getMinX();
258: state = this.axis.draw(
259: g2, cursor, reservedArea, colorBarArea, RectangleEdge.LEFT, null
260: );
261: }
262: else if (edge == RectangleEdge.RIGHT) {
263: cursor = colorBarArea.getMaxX();
264: state = this.axis.draw(
265: g2, cursor, reservedArea, colorBarArea, RectangleEdge.RIGHT,
266: null
267: );
268: }
269: return state.getCursor();
270:
271: }
272:
273:
281: public void drawColorBar(Graphics2D g2, Rectangle2D colorBarArea,
282: RectangleEdge edge) {
283:
284: Object antiAlias = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
285: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
286: RenderingHints.VALUE_ANTIALIAS_OFF);
287:
288:
289:
290:
291: Stroke strokeSaved = g2.getStroke();
292: g2.setStroke(new BasicStroke(1.0f));
293:
294: if (RectangleEdge.isTopOrBottom(edge)) {
295: double y1 = colorBarArea.getY();
296: double y2 = colorBarArea.getMaxY();
297: double xx = colorBarArea.getX();
298: Line2D line = new Line2D.Double();
299: while (xx <= colorBarArea.getMaxX()) {
300: double value = this.axis.java2DToValue(xx, colorBarArea, edge);
301: line.setLine(xx, y1, xx, y2);
302: g2.setPaint(getPaint(value));
303: g2.draw(line);
304: xx += 1;
305: }
306: }
307: else {
308: double y1 = colorBarArea.getX();
309: double y2 = colorBarArea.getMaxX();
310: double xx = colorBarArea.getY();
311: Line2D line = new Line2D.Double();
312: while (xx <= colorBarArea.getMaxY()) {
313: double value = this.axis.java2DToValue(xx, colorBarArea, edge);
314: line.setLine(y1, xx, y2, xx);
315: g2.setPaint(getPaint(value));
316: g2.draw(line);
317: xx += 1;
318: }
319: }
320:
321: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antiAlias);
322: g2.setStroke(strokeSaved);
323:
324: }
325:
326:
331: public ColorPalette getColorPalette() {
332: return this.colorPalette;
333: }
334:
335:
342: public Paint getPaint(double value) {
343: return this.colorPalette.getPaint(value);
344: }
345:
346:
351: public void setColorPalette(ColorPalette palette) {
352: this.colorPalette = palette;
353: }
354:
355:
360: public void setMaximumValue(double value) {
361: this.colorPalette.setMaxZ(value);
362: this.axis.setUpperBound(value);
363: }
364:
365:
370: public void setMinimumValue(double value) {
371: this.colorPalette.setMinZ(value);
372: this.axis.setLowerBound(value);
373: }
374:
375:
387: public AxisSpace reserveSpace(Graphics2D g2, Plot plot,
388: Rectangle2D plotArea,
389: Rectangle2D dataArea, RectangleEdge edge,
390: AxisSpace space) {
391:
392: AxisSpace result = this.axis.reserveSpace(
393: g2, plot, plotArea, edge, space
394: );
395: double thickness = calculateBarThickness(dataArea, edge);
396: result.add(thickness + 2 * this.outerGap, edge);
397: return result;
398:
399: }
400:
401:
409: private double calculateBarThickness(Rectangle2D plotArea,
410: RectangleEdge edge) {
411: double result = 0.0;
412: if (RectangleEdge.isLeftOrRight(edge)) {
413: result = plotArea.getWidth() * this.colorBarThicknessPercent;
414: }
415: else {
416: result = plotArea.getHeight() * this.colorBarThicknessPercent;
417: }
418: return result;
419: }
420:
421:
429: public Object clone() throws CloneNotSupportedException {
430:
431: ColorBar clone = (ColorBar) super.clone();
432: clone.axis = (ValueAxis) this.axis.clone();
433: return clone;
434:
435: }
436:
437:
444: public boolean equals(Object obj) {
445:
446: if (obj == this) {
447: return true;
448: }
449: if (!(obj instanceof ColorBar)) {
450: return false;
451: }
452: ColorBar that = (ColorBar) obj;
453: if (!this.axis.equals(that.axis)) {
454: return false;
455: }
456: if (this.colorBarThickness != that.colorBarThickness) {
457: return false;
458: }
459: if (this.colorBarThicknessPercent != that.colorBarThicknessPercent) {
460: return false;
461: }
462: if (!this.colorPalette.equals(that.colorPalette)) {
463: return false;
464: }
465: if (this.colorBarLength != that.colorBarLength) {
466: return false;
467: }
468: if (this.outerGap != that.outerGap) {
469: return false;
470: }
471: return true;
472:
473: }
474:
475:
480: public int hashCode() {
481: return this.axis.hashCode();
482: }
483:
484: }