annotate flys-artifacts/src/main/java/de/intevation/flys/jfree/StickyAxisAnnotation.java @ 1039:cf39205df113

Part of refactoring (see next commit). flys-artifacts/trunk@2500 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Wed, 17 Aug 2011 13:04:53 +0000
parents
children 8873c43119ca
rev   line source
1039
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
1 package de.intevation.flys.jfree;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
2
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
3 import org.apache.log4j.Logger;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
4
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
5 import java.util.Iterator;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
6
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
7 import java.awt.Shape;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
8 import java.awt.geom.Rectangle2D;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
9 import java.awt.geom.Line2D;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
10
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
11 import org.jfree.chart.annotations.XYTextAnnotation;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
12 import org.jfree.chart.axis.ValueAxis;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
13 import org.jfree.chart.util.LineUtilities;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
14 import org.jfree.text.TextUtilities;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
15 import org.jfree.chart.plot.PlotOrientation;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
16 import org.jfree.chart.plot.XYPlot;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
17 import org.jfree.chart.entity.XYAnnotationEntity;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
18 import org.jfree.chart.plot.PlotRenderingInfo;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
19 import org.jfree.chart.ChartRenderingInfo;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
20 import org.jfree.chart.plot.Plot;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
21
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
22 import org.jfree.ui.RectangleEdge;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
23
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
24
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
25 /**
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
26 * Custom Annotations class that is drawn only if no collisions with other
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
27 * already drawn CustomAnnotations in current plot are found.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
28 * Draws a given text and a line to it from either axis.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
29 */
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
30 public class StickyAxisAnnotation extends XYTextAnnotation {
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
31
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
32 /** Logger for this class. */
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
33 private static Logger logger =
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
34 Logger.getLogger(StickyAxisAnnotation.class);
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
35
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
36 /** Simplified view on axes. */
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
37 public static enum SimpleAxis {
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
38 X_AXIS, /** Usually "horizontal". */
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
39 Y_AXIS /** Usually "vertical". */
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
40 }
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
41
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
42 /** Which axis to stick to. */
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
43 protected SimpleAxis stickyAxis = SimpleAxis.X_AXIS;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
44
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
45
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
46 /**
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
47 * Trivial constructor.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
48 *
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
49 * @param text Text to display.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
50 * @param x X-position in dataspace (typical horizontal, in km).
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
51 * @param y Y-position in dataspace (typical vertical, in m).
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
52 */
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
53 public StickyAxisAnnotation(String text, float x, float y) {
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
54 super(text, x, y);
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
55 }
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
56
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
57
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
58 /**
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
59 * Sets the "sticky axis" (whether to draw annotations at the
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
60 * X- or the Y-Axis.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
61 *
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
62 * @param stickyAxis axis to stick to.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
63 */
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
64 public void setStickyAxis(SimpleAxis stickyAxis) {
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
65 this.stickyAxis = stickyAxis;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
66 }
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
67
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
68
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
69 /**
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
70 * Draws a small line at axis where this annotation resides.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
71 *
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
72 * @param g2 the graphics device.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
73 * @param dataArea the data area.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
74 * @param domainAxis the domain axis.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
75 * @param rangeAxis the range axis.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
76 * @param domainEdge the domain edge.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
77 * @param rangeEdge the range edge.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
78 * @param orientation the plot orientation.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
79 */
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
80 protected void drawAxisMark(
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
81 java.awt.Graphics2D g2,
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
82 java.awt.geom.Rectangle2D dataArea,
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
83 ValueAxis domainAxis,
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
84 ValueAxis rangeAxis,
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
85 RectangleEdge domainEdge,
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
86 RectangleEdge rangeEdge,
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
87 PlotOrientation orientation) {
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
88 float j2DX1 = 0.0f;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
89 float j2DX2 = 0.0f;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
90 float j2DY1 = 0.0f;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
91 float j2DY2 = 0.0f;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
92 float x = (float) getX();
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
93 float y = (float) getY();
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
94 /* When dependent on X/Y-Axis and orientation, following
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
95 can be used as a base:
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
96 if (orientation == PlotOrientation.VERTICAL) {
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
97 j2DX1 = (float) domainAxis.valueToJava2D(x, dataArea,
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
98 domainEdge);
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
99 j2DY1 = (float) rangeAxis.valueToJava2D(y, dataArea,
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
100 rangeEdge);
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
101 j2DX2 = (float) domainAxis.valueToJava2D(x, dataArea,
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
102 domainEdge);
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
103 j2DY2 = (float) rangeAxis.valueToJava2D(y, dataArea,
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
104 rangeEdge);
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
105 }
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
106 else if (orientation == PlotOrientation.HORIZONTAL) {
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
107 j2DY1 = (float) domainAxis.valueToJava2D(x, dataArea,
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
108 domainEdge);
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
109 j2DX1 = (float) rangeAxis.valueToJava2D(y, dataArea,
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
110 rangeEdge);
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
111 j2DY2 = (float) domainAxis.valueToJava2D(x, dataArea,
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
112 domainEdge);
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
113 j2DX2 = (float) rangeAxis.valueToJava2D(y, dataArea,
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
114 rangeEdge);
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
115 }
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
116
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
117 g2.setPaint(this.paint);
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
118 g2.setStroke(this.stroke);
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
119 */
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
120 j2DY1 = (float) RectangleEdge.coordinate(dataArea, domainEdge);
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
121 j2DY2 = j2DY1 - 0.10f * (float)
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
122 (rangeAxis.getRange().getUpperBound()
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
123 - rangeAxis.getRange().getLowerBound());
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
124 j2DX1 = (float) domainAxis.valueToJava2D(x, dataArea, domainEdge);
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
125 j2DX2 = j2DX1;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
126
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
127 Line2D line = new Line2D.Float(j2DX1, j2DY1, j2DX2, j2DY2);
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
128
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
129 // line is clipped to avoid JRE bug 6574155, for more info
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
130 // see JFreeChart bug 2221495
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
131 boolean visible = LineUtilities.clipLine(line, dataArea);
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
132 if (visible) {
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
133 g2.draw(line);
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
134 }
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
135 }
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
136
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
137
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
138 /**
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
139 * Draw the Annotiation if it does not collide with other already drawn
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
140 * Annotations.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
141 *
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
142 * @param g2 the graphics device.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
143 * @param plot the plot.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
144 * @param dataArea the data area.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
145 * @param domainAxis the domain axis.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
146 * @param rangeAxis the range axis.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
147 * @param rendererIndex the render index.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
148 * @param info state information, escpecially collects info about
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
149 * already drawn shapes (and thus annotations), used
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
150 * for collision detection.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
151 */
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
152 @Override
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
153 public void draw(
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
154 java.awt.Graphics2D g2,
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
155 XYPlot plot,
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
156 java.awt.geom.Rectangle2D dataArea,
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
157 ValueAxis domainAxis,
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
158 ValueAxis rangeAxis,
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
159 int rendererIndex,
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
160 PlotRenderingInfo info) {
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
161
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
162 if (info == null)
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
163 return;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
164
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
165 // Calculate the bounding box.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
166 ChartRenderingInfo chartInfo = info.getOwner();
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
167
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
168 PlotOrientation orientation = plot.getOrientation();
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
169 RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
170 plot.getDomainAxisLocation(), orientation);
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
171 RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
172 plot.getRangeAxisLocation(), orientation);
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
173 float anchorX = (float) domainAxis.valueToJava2D(
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
174 getX(), dataArea, domainEdge);
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
175 float anchorY = (float) rangeAxis.valueToJava2D(
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
176 getY(), dataArea, rangeEdge);
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
177 if (orientation == PlotOrientation.HORIZONTAL) {
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
178 float tempAnchor = anchorX;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
179 anchorX = anchorY;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
180 anchorY = tempAnchor;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
181 }
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
182
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
183 // Always draw the small line at axis.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
184 drawAxisMark(g2, dataArea, domainAxis, rangeAxis, domainEdge,
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
185 rangeEdge, orientation);
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
186
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
187 g2.setFont(getFont());
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
188 Shape hotspot = TextUtilities.calculateRotatedStringBounds(
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
189 getText(), g2, anchorX, anchorY, getTextAnchor(),
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
190 getRotationAngle(), getRotationAnchor());
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
191 Rectangle2D hotspotBox = hotspot.getBounds2D();
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
192 // Check for collisions with other XYAnnotations.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
193 for (Iterator i = chartInfo.getEntityCollection().iterator();
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
194 i.hasNext(); ) {
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
195 Object next = i.next();
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
196 // Collision with other stuff than XYAnnotations are okay.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
197 if (next instanceof XYAnnotationEntity) {
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
198 XYAnnotationEntity drawnShape = (XYAnnotationEntity) next;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
199 if (drawnShape.getArea().intersects(hotspotBox)) {
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
200 // Found collision, early stop.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
201 return;
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
202 }
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
203 }
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
204 }
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
205
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
206 // Actuall drawing.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
207 if (getBackgroundPaint() != null) {
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
208 g2.setPaint(getBackgroundPaint());
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
209 g2.fill(hotspot);
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
210 }
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
211 g2.setPaint(getPaint());
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
212 TextUtilities.drawRotatedString(getText(), g2, anchorX, anchorY,
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
213 getTextAnchor(), getRotationAngle(), getRotationAnchor());
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
214 // Draw outline.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
215 if (false) {
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
216 g2.setStroke(getOutlineStroke());
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
217 g2.setPaint(getOutlinePaint());
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
218 g2.draw(hotspot);
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
219 }
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
220
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
221 // Add info that we have drawn this Annotation.
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
222 addEntity(info, hotspot, rendererIndex, getToolTipText(), getURL());
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
223 }
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
224 }
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
225
cf39205df113 Part of refactoring (see next commit).
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
226

http://dive4elements.wald.intevation.org