svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap / src-test / org / gvsig / fmap / mapcontext / rendering / symbol / TestDrawMarkers.java @ 21156
History | View | Annotate | Download (7.58 KB)
1 |
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib��ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package org.gvsig.fmap.mapcontext.rendering.symbol; |
42 |
|
43 |
import java.awt.Color; |
44 |
import java.awt.Dimension; |
45 |
import java.awt.Graphics2D; |
46 |
import java.awt.Rectangle; |
47 |
import java.awt.geom.AffineTransform; |
48 |
import java.awt.geom.Point2D; |
49 |
import java.awt.image.BufferedImage; |
50 |
import java.util.ArrayList; |
51 |
import java.util.Random; |
52 |
|
53 |
import junit.framework.TestCase; |
54 |
|
55 |
import org.gvsig.fmap.mapcontext.rendering.symbols.IMarkerSymbol; |
56 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
57 |
|
58 |
/**
|
59 |
* Integration test to test that Marker symbols always draw in the same
|
60 |
* place respecting size constraints.
|
61 |
*
|
62 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
63 |
*/
|
64 |
public class TestDrawMarkers extends TestCase { |
65 |
private IMarkerSymbol[] symbols; |
66 |
private final Dimension sz = new Dimension(400, 400); |
67 |
private final org.gvsig.fmap.geom.primitive.Point2D centerP = new org.gvsig.fmap.geom.primitive.Point2D(sz.width/2, sz.height/2); |
68 |
private static final int OUTTER_TOLERANCE = 1; |
69 |
private static final int INNER_TOLERANCE = 1; |
70 |
|
71 |
private static final double sizes[] = new double[] { |
72 |
200,
|
73 |
100,
|
74 |
50,
|
75 |
30,
|
76 |
16,
|
77 |
5,
|
78 |
3,
|
79 |
2,
|
80 |
// smaller sizes don't make any sense
|
81 |
|
82 |
}; |
83 |
|
84 |
|
85 |
|
86 |
protected void setUp() throws Exception { |
87 |
// get all the symbols in the Test Suite
|
88 |
super.setUp();
|
89 |
|
90 |
ISymbol[] allSymbols = TestISymbol.getNewSymbolInstances();
|
91 |
// Filter the marker ones
|
92 |
ArrayList symbols = new ArrayList(); |
93 |
|
94 |
for (int i = 0; i < allSymbols.length; i++) { |
95 |
if (allSymbols[i] instanceof IMarkerSymbol) { |
96 |
IMarkerSymbol sym = (IMarkerSymbol) allSymbols[i]; |
97 |
symbols.add(sym); |
98 |
|
99 |
} |
100 |
} |
101 |
this.symbols = (IMarkerSymbol[]) symbols.toArray(new IMarkerSymbol[symbols.size()]); |
102 |
} |
103 |
|
104 |
public void testDrawingSize() { |
105 |
for (int i = 0; i < symbols.length; i++) { |
106 |
for (int j = 0; j < sizes.length; j++) { |
107 |
// new blank buffered image
|
108 |
BufferedImage bi = new BufferedImage(sz.width, sz.height, BufferedImage.TYPE_INT_ARGB); |
109 |
|
110 |
// the graphics for the image, so we can draw onto the buffered image
|
111 |
Graphics2D g = bi.createGraphics();
|
112 |
|
113 |
IMarkerSymbol testSymbol = symbols[i]; |
114 |
testSymbol.setColor(Color.YELLOW);
|
115 |
testSymbol.setSize(sizes[j]); |
116 |
String name = testSymbol.getClassName().substring(
|
117 |
testSymbol.getClassName().lastIndexOf('.')+1, |
118 |
testSymbol.getClassName().length()); |
119 |
|
120 |
testSymbol.draw(g, new AffineTransform(), centerP, null); |
121 |
|
122 |
Rectangle wrappingRect = new Rectangle( |
123 |
(int) (centerP.getX()-sizes[j]/2), |
124 |
(int) (centerP.getY()-sizes[j]/2), |
125 |
(int) (sizes[j]),
|
126 |
(int) (sizes[j]));
|
127 |
|
128 |
// if (testSymbol instanceof CharacterMarkerSymbol)
|
129 |
// continue;
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
assertFalse("fails sizing marker, too big ("+name+", "+sizes[j]+"px)", isOutsideRect(bi, wrappingRect, OUTTER_TOLERANCE )); |
135 |
assertTrue("fails sizing marker, too small ("+name+", "+sizes[j]+"px) \n" + |
136 |
"\t - forgot to enable ANTIALIASING?", fitsInsideRect(bi, wrappingRect, INNER_TOLERANCE));
|
137 |
|
138 |
|
139 |
} |
140 |
} |
141 |
} |
142 |
|
143 |
public void testDrawingOffset() { |
144 |
Random random = new Random(System.currentTimeMillis()); |
145 |
for (int i = 0; i < symbols.length; i++) { |
146 |
for (int j = 0; j < sizes.length; j++) { |
147 |
// new blank buffered image
|
148 |
BufferedImage bi = new BufferedImage(sz.width, sz.height, BufferedImage.TYPE_INT_ARGB); |
149 |
|
150 |
// the graphics for the image, so we can draw onto the buffered image
|
151 |
Graphics2D g = bi.createGraphics();
|
152 |
|
153 |
IMarkerSymbol testSymbol = symbols[i]; |
154 |
testSymbol.setColor(Color.YELLOW);
|
155 |
testSymbol.setSize(sizes[j]); |
156 |
|
157 |
String name = testSymbol.getClassName().substring(
|
158 |
testSymbol.getClassName().lastIndexOf('.')+1, |
159 |
testSymbol.getClassName().length()); |
160 |
|
161 |
double xOffset = (random.nextDouble()-0.5)*sz.width; |
162 |
double yOffset = (random.nextDouble()-0.5)*sz.height; |
163 |
|
164 |
testSymbol.setOffset(new Point2D.Double(xOffset, yOffset)); |
165 |
|
166 |
Rectangle wrappingRect = new Rectangle( |
167 |
(int) ((centerP.getX()-sizes[j]/2) + xOffset), |
168 |
(int) ((centerP.getY()-sizes[j]/2) + yOffset), |
169 |
(int) (sizes[j]),
|
170 |
(int) (sizes[j]));
|
171 |
|
172 |
// if (testSymbol instanceof CharacterMarkerSymbol)
|
173 |
// continue;
|
174 |
|
175 |
|
176 |
|
177 |
testSymbol.draw(g, new AffineTransform(), centerP, null); |
178 |
assertFalse("fails sizing marker, too big ("+name+", "+sizes[j]+"px)", isOutsideRect(bi, wrappingRect, OUTTER_TOLERANCE )); |
179 |
assertTrue("fails sizing marker, too small ("+name+", "+sizes[j]+"px) \n" + |
180 |
"\t - forgot to enable ANTIALIASING?", fitsInsideRect(bi, wrappingRect, INNER_TOLERANCE));
|
181 |
|
182 |
} |
183 |
} |
184 |
} |
185 |
|
186 |
public void testDrawingRotation() { |
187 |
// TODO implement it
|
188 |
|
189 |
} |
190 |
|
191 |
private boolean fitsInsideRect(BufferedImage bi, Rectangle wrappingRect, int tolerance) { |
192 |
int myTolerance = Math.min(tolerance, wrappingRect.width); |
193 |
Rectangle myRect;
|
194 |
if (myTolerance >= INNER_TOLERANCE) {
|
195 |
myRect = new Rectangle( |
196 |
wrappingRect.x+myTolerance, |
197 |
wrappingRect.y+myTolerance, |
198 |
wrappingRect.width-myTolerance-myTolerance, |
199 |
wrappingRect.height-myTolerance-myTolerance); |
200 |
} else {
|
201 |
myRect = new Rectangle(-1, -1, 0, 0); // just ensure it does not disturb the check |
202 |
} |
203 |
|
204 |
for (int i = 0; i < bi.getWidth(); i++) { |
205 |
for (int j = 0; j < bi.getHeight(); j++) { |
206 |
if (wrappingRect.contains(i,j)
|
207 |
&& !myRect.contains(i,j)) { |
208 |
|
209 |
if (bi.getRGB(i,j) != 0) |
210 |
return true; |
211 |
} |
212 |
} |
213 |
} |
214 |
return false; |
215 |
} |
216 |
|
217 |
private boolean isOutsideRect(BufferedImage bi, Rectangle wrappingRect, int tolerance) { |
218 |
Rectangle myRect = new Rectangle( |
219 |
wrappingRect.x-tolerance, |
220 |
wrappingRect.y-tolerance, |
221 |
wrappingRect.width+tolerance+tolerance, |
222 |
wrappingRect.height+tolerance+tolerance); |
223 |
for (int i = 0; i < bi.getWidth(); i++) { |
224 |
for (int j = 0; j < bi.getHeight(); j++) { |
225 |
if (!myRect.contains(i,j))
|
226 |
continue;
|
227 |
|
228 |
if (bi.getRGB(i,j) != 0) return false; |
229 |
} |
230 |
} |
231 |
return true; |
232 |
} |
233 |
} |
234 |
|
235 |
/// paste this piece of code to produce some png files to check visually what is happening
|
236 |
//g.setColor(Color.BLUE);
|
237 |
//Rectangle myRect = new Rectangle(
|
238 |
// wrappingRect.x+INNER_TOLERANCE,
|
239 |
// wrappingRect.y+INNER_TOLERANCE,
|
240 |
// wrappingRect.width-INNER_TOLERANCE-INNER_TOLERANCE,
|
241 |
// wrappingRect.height-INNER_TOLERANCE-INNER_TOLERANCE);
|
242 |
//
|
243 |
//g.draw(myRect);
|
244 |
//g.setColor(Color.RED);
|
245 |
//g.draw(wrappingRect);
|
246 |
//try {
|
247 |
//
|
248 |
// File dstDir = new File (System.getProperty("java.io.tmpdir")+"/prova-imatges/");
|
249 |
// if (!dstDir.exists()) dstDir.mkdir();
|
250 |
// ImageIO.write(bi, "png",
|
251 |
// new File(dstDir.getAbsoluteFile()+File.separator+
|
252 |
// name+"_size_"+sizes[j]
|
253 |
// +".png"));
|
254 |
//} catch (IOException e) {
|
255 |
// e.printStackTrace();
|
256 |
// fail();
|
257 |
//}
|
258 |
///
|