svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.symbology / src / test / java / org / gvsig / symbology / fmap / mapcontext / rendering / symbol / TestDrawLines.java @ 30754
History | View | Annotate | Download (6.67 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.symbology.fmap.mapcontext.rendering.symbol; |
42 |
|
43 |
import java.awt.Color; |
44 |
import java.awt.Dimension; |
45 |
import java.awt.Graphics2D; |
46 |
import java.awt.geom.AffineTransform; |
47 |
import java.awt.geom.Point2D; |
48 |
import java.awt.image.BufferedImage; |
49 |
import java.util.ArrayList; |
50 |
|
51 |
import junit.framework.TestCase; |
52 |
|
53 |
import org.gvsig.compat.CompatLocator; |
54 |
import org.gvsig.fmap.geom.GeometryLocator; |
55 |
import org.gvsig.fmap.geom.GeometryManager; |
56 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
57 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
58 |
import org.gvsig.fmap.geom.primitive.Curve; |
59 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
60 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
61 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol; |
62 |
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase; |
63 |
import org.slf4j.Logger; |
64 |
import org.slf4j.LoggerFactory; |
65 |
|
66 |
/**
|
67 |
* Integration test to test that Line symbols always draw in the same
|
68 |
* place respecting size constraints.
|
69 |
*
|
70 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
71 |
*/
|
72 |
public class TestDrawLines extends AbstractLibraryAutoInitTestCase { |
73 |
private GeometryManager geomManager;
|
74 |
private static final Logger logger = LoggerFactory.getLogger(TestDrawLines.class); |
75 |
private ILineSymbol[] symbols; |
76 |
private final Dimension sz = new Dimension(401, 401); |
77 |
private Curve centerL;
|
78 |
|
79 |
protected void doSetUp() throws Exception { |
80 |
geomManager = GeometryLocator.getGeometryManager(); |
81 |
|
82 |
GeneralPathX gp = new GeneralPathX();
|
83 |
gp.moveTo(0, sz.height / 2 ); |
84 |
gp.lineTo(sz.width, sz.height /2 );
|
85 |
|
86 |
try {
|
87 |
centerL = geomManager.createCurve(gp, SUBTYPES.GEOM2D); |
88 |
} catch (CreateGeometryException e) {
|
89 |
e.printStackTrace(); |
90 |
} |
91 |
|
92 |
ISymbol[] allSymbols = TestISymbol.getNewSymbolInstances();
|
93 |
// Filter the marker ones
|
94 |
ArrayList symbols = new ArrayList(); |
95 |
|
96 |
for (int i = 0; i < allSymbols.length; i++) { |
97 |
if (allSymbols[i] instanceof ILineSymbol) { |
98 |
ILineSymbol sym = (ILineSymbol) allSymbols[i]; |
99 |
symbols.add(sym); |
100 |
|
101 |
} |
102 |
} |
103 |
this.symbols = (ILineSymbol[]) symbols.toArray(new ILineSymbol[symbols.size()]); |
104 |
} |
105 |
private static final double sizes[] = new double[] { |
106 |
300,
|
107 |
250,
|
108 |
225,
|
109 |
200,
|
110 |
100,
|
111 |
50,
|
112 |
30,
|
113 |
15,
|
114 |
5,
|
115 |
3,
|
116 |
2,
|
117 |
1,
|
118 |
// smaller sizes don't make any sense
|
119 |
}; |
120 |
|
121 |
private static final float INNER_TOLERANCE = 1F; |
122 |
private static final float OUTTER_TOLERANCE = 1F; |
123 |
|
124 |
public void testDrawingSize() { |
125 |
for (int i = 0; i < symbols.length; i++) { |
126 |
for (int j = 0; j < sizes.length; j++) { |
127 |
// new blank buffered image
|
128 |
|
129 |
BufferedImage bi = CompatLocator.getGraphicsUtils().createBufferedImage(sz.width, sz.height, BufferedImage.TYPE_INT_ARGB); |
130 |
|
131 |
// the graphics for the image, so we can draw onto the buffered image
|
132 |
Graphics2D g = bi.createGraphics();
|
133 |
|
134 |
ILineSymbol testSymbol = symbols[i]; |
135 |
testSymbol.setLineColor(Color.YELLOW);
|
136 |
testSymbol.setLineWidth(sizes[j]); |
137 |
String name = testSymbol.getClass().getName().substring(
|
138 |
testSymbol.getClass().getName().lastIndexOf('.')+1, |
139 |
testSymbol.getClass().getName().length()); |
140 |
|
141 |
Point2D upperP1 = new Point2D.Double(0, centerL.getBounds().getY() - sizes[j]*0.5); |
142 |
Point2D upperP2 = new Point2D.Double(centerL.getBounds().getWidth(), centerL.getBounds().getY() - sizes[j]*0.5); |
143 |
GeneralPathX gpUp = new GeneralPathX();
|
144 |
gpUp.moveTo(upperP1.getX(), upperP1.getY()); |
145 |
gpUp.lineTo(upperP2.getX(), upperP2.getY()); |
146 |
|
147 |
|
148 |
GeneralPathX gpDown = new GeneralPathX();
|
149 |
Point2D lowerP1 = new Point2D.Double(0, centerL.getBounds().getY() + sizes[j]*0.5); |
150 |
Point2D lowerP2 = new Point2D.Double(centerL.getBounds().getWidth(), centerL.getBounds().getY() + sizes[j]*0.5); |
151 |
gpDown.moveTo(lowerP1.getX(), lowerP1.getY()); |
152 |
gpDown.lineTo(lowerP2.getX(), lowerP2.getY()); |
153 |
|
154 |
|
155 |
testSymbol.draw(g, new AffineTransform(), centerL, null, null); |
156 |
|
157 |
/// per a borrar
|
158 |
// g.setStroke(new BasicStroke());
|
159 |
// g.setColor(Color.RED);
|
160 |
//
|
161 |
// g.draw(gpUp);
|
162 |
//
|
163 |
// g.setColor(Color.BLUE);
|
164 |
// g.draw(gpDown);
|
165 |
//
|
166 |
// try {
|
167 |
//
|
168 |
// File dstDir = new File (System.getProperty("java.io.tmpdir")+"/prova-imatges/");
|
169 |
// if (!dstDir.exists()) dstDir.mkdir();
|
170 |
// ImageIO.write(bi, "png",
|
171 |
// new File(dstDir.getAbsoluteFile()+File.separator+
|
172 |
// name+"_size_"+sizes[j]
|
173 |
// +".png"));
|
174 |
// } catch (IOException e) {
|
175 |
// e.printStackTrace();
|
176 |
// fail();
|
177 |
// }
|
178 |
/// fi per a borrar
|
179 |
assertFalse("fails sizing line, too big ("+name+", "+sizes[j]+"px)", isOutsideRect(bi, upperP1, lowerP1, OUTTER_TOLERANCE )); |
180 |
assertTrue("fails sizing line, too small ("+name+", "+sizes[j]+"px) \n" + |
181 |
"\t - forgot to enable ANTIALIASING?", fitsInsideRect(bi, upperP1, lowerP1, INNER_TOLERANCE));
|
182 |
|
183 |
} |
184 |
} |
185 |
} |
186 |
|
187 |
private boolean isOutsideRect(BufferedImage bi, Point2D upper, Point2D lower, float outterTolerance) { |
188 |
for (int i = 0; i < bi.getWidth(); i++) { |
189 |
for (int j = 0; j < bi.getHeight(); j++) { |
190 |
if (j<upper.getY()-outterTolerance && j>lower.getY()+outterTolerance)
|
191 |
if (bi.getRGB(i,j) != 0) { |
192 |
System.out.println("too big In pixel ("+i+", "+j+")"); |
193 |
return true; |
194 |
} |
195 |
|
196 |
} |
197 |
} |
198 |
return false; |
199 |
} |
200 |
|
201 |
private boolean fitsInsideRect(BufferedImage bi,Point2D upper, Point2D lower , float innerTolerance) { |
202 |
for (int i = 0; i < bi.getWidth(); i++) { |
203 |
for (int j = 0; j < bi.getHeight(); j++) { |
204 |
if (j<upper.getY()+innerTolerance && j>lower.getY()-innerTolerance)
|
205 |
if (bi.getRGB(i,j) == 0) { |
206 |
System.out.println("does not fit big In pixel ("+i+", "+j+")"); |
207 |
return false; |
208 |
} |
209 |
|
210 |
} |
211 |
} |
212 |
return true; |
213 |
} |
214 |
|
215 |
} |