comparison flys-artifacts/src/main/java/de/intevation/flys/jfree/CollisionFreeXYTextAnnotation.java @ 2157:c4ceade9d50e

Added new Annotation class that prevents collisions (to replace StickyAxisAnnotation). flys-artifacts/trunk@3742 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Fri, 20 Jan 2012 13:20:37 +0000
parents
children c68f4f227c09
comparison
equal deleted inserted replaced
2156:4e0878695c5f 2157:c4ceade9d50e
1 package de.intevation.flys.jfree;
2
3 import org.apache.log4j.Logger;
4
5 import java.util.Iterator;
6
7 import java.awt.Shape;
8 import java.awt.geom.Rectangle2D;
9 import java.awt.Color;
10 import java.awt.Font;
11 import java.awt.BasicStroke;
12
13 import org.jfree.chart.annotations.XYTextAnnotation;
14 import org.jfree.chart.axis.ValueAxis;
15 import org.jfree.chart.plot.PlotOrientation;
16 import org.jfree.chart.plot.XYPlot;
17 import org.jfree.chart.entity.XYAnnotationEntity;
18 import org.jfree.chart.plot.PlotRenderingInfo;
19 import org.jfree.chart.plot.Plot;
20
21 import org.jfree.text.TextUtilities;
22
23 import org.jfree.ui.RectangleEdge;
24
25 import de.intevation.flys.utils.ThemeAccess;
26
27 /**
28 * Custom Annotations class that is drawn only if no collisions with other
29 * already drawn CustomAnnotations in current plot are found.
30 */
31 public class CollisionFreeXYTextAnnotation extends XYTextAnnotation {
32
33 /** Logger for this class. */
34 private static Logger logger =
35 Logger.getLogger(CollisionFreeXYTextAnnotation.class);
36
37 public CollisionFreeXYTextAnnotation(String text, float x, double y) {
38 super(text, x, y);
39 }
40
41
42 /**
43 * Draw the Annotation only if it does not collide with other
44 * already drawn Annotations- texts.
45 *
46 * @param g2 the graphics device.
47 * @param plot the plot.
48 * @param dataArea the data area.
49 * @param domainAxis the domain axis.
50 * @param rangeAxis the range axis.
51 * @param rendererIndex the render index.
52 * @param info state information, escpecially collects info about
53 * already drawn shapes (and thus annotations), used
54 * for collision detection.
55 */
56 @Override
57 public void draw(
58 java.awt.Graphics2D g2,
59 XYPlot plot,
60 java.awt.geom.Rectangle2D dataArea,
61 ValueAxis domainAxis,
62 ValueAxis rangeAxis,
63 int rendererIndex,
64 PlotRenderingInfo info
65 ) {
66 // From superclass, adjusted access only.
67 PlotOrientation orientation = plot.getOrientation();
68 RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
69 plot.getDomainAxisLocation(), orientation);
70 RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
71 plot.getRangeAxisLocation(), orientation);
72
73 float anchorX = (float) domainAxis.valueToJava2D(
74 this.getX(), dataArea, domainEdge);
75 float anchorY = (float) rangeAxis.valueToJava2D(
76 this.getY(), dataArea, rangeEdge);
77
78 if (orientation == PlotOrientation.HORIZONTAL) {
79 float tempAnchor = anchorX;
80 anchorX = anchorY;
81 anchorY = tempAnchor;
82 }
83
84 g2.setFont(getFont());
85 Shape hotspot = TextUtilities.calculateRotatedStringBounds(
86 getText(), g2, anchorX, anchorY, getTextAnchor(),
87 getRotationAngle(), getRotationAnchor());
88
89 // Deviation from superclass: prevent collision.
90 Rectangle2D hotspotBox = hotspot.getBounds2D();
91 for (Iterator i = info.getOwner().getEntityCollection().iterator();
92 i.hasNext(); ) {
93 Object next = i.next();
94 // Collision with other stuff than XYAnnotations are okay.
95 if (next instanceof XYAnnotationEntity) {
96 XYAnnotationEntity drawnShape = (XYAnnotationEntity) next;
97 if (drawnShape.getArea().intersects(hotspotBox)) {
98 // Found collision, early stop.
99 return;
100 }
101 }
102 }
103
104 if (this.getBackgroundPaint() != null) {
105 g2.setPaint(this.getBackgroundPaint());
106 g2.fill(hotspot);
107 }
108 g2.setPaint(getPaint());
109 TextUtilities.drawRotatedString(getText(), g2, anchorX, anchorY,
110 getTextAnchor(), getRotationAngle(), getRotationAnchor());
111 if (this.isOutlineVisible()) {
112 g2.setStroke(this.getOutlineStroke());
113 g2.setPaint(this.getOutlinePaint());
114 g2.draw(hotspot);
115 }
116
117 String toolTip = getToolTipText();
118 String url = getURL();
119 if (toolTip != null || url != null) {
120 addEntity(info, hotspot, rendererIndex, toolTip, url);
121 }
122 else {
123 addEntity(info, hotspot, rendererIndex,
124 "CollisionFreeXYTextAnnotation",
125 "CollisionFreeXYTextAnnotation");
126 }
127 }
128
129
130 /*
131 public void applyTheme(ThemeAccess ta) {
132 lineWidth = ta.parseLineWidth();
133 lineColor = ta.parseLineColorField();
134 textColor = ta.parseTextColor();
135 font = ta.parseTextFont();
136 textOrientation = ta.parseTextOrientation();
137 textBackground = ta.parseTextBackground();
138 showBackground = ta.parseShowTextBackground();
139 }
140 */
141 }
142 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org