svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap / src-test / org / gvsig / fmap / mapcontext / rendering / symbols / TestDrawFills.java @ 20984
History | View | Annotate | Download (6.62 KB)
1 | 18627 | jdominguez | /* 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 | 20984 | jmvivo | package org.gvsig.fmap.mapcontext.rendering.symbols; |
42 | 18627 | jdominguez | |
43 | import java.awt.Dimension; |
||
44 | import java.awt.Graphics2D; |
||
45 | import java.awt.geom.AffineTransform; |
||
46 | import java.awt.geom.Point2D; |
||
47 | import java.awt.image.BufferedImage; |
||
48 | import java.io.File; |
||
49 | import java.io.IOException; |
||
50 | import java.util.ArrayList; |
||
51 | |||
52 | import javax.imageio.ImageIO; |
||
53 | |||
54 | 20984 | jmvivo | import org.gvsig.fmap.AllTests; |
55 | import org.gvsig.fmap.core.shapes.FPoint2D; |
||
56 | import org.gvsig.fmap.core.shapes.FPolygon2D; |
||
57 | import org.gvsig.fmap.core.shapes.FShape; |
||
58 | import org.gvsig.fmap.core.shapes.GeneralPathX; |
||
59 | import org.gvsig.fmap.mapcontext.MapContext; |
||
60 | import org.gvsig.fmap.mapcontext.rendering.symbols.IFillSymbol; |
||
61 | import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
||
62 | |||
63 | 18627 | jdominguez | import junit.framework.TestCase; |
64 | |||
65 | /**
|
||
66 | * Integration test to test that Fill symbols always draw in the same
|
||
67 | * place respecting size constraints.
|
||
68 | *
|
||
69 | * @author jaume dominguez faus - jaume.dominguez@iver.es
|
||
70 | */
|
||
71 | public class TestDrawFills extends TestCase { |
||
72 | |||
73 | private final Dimension sz = new Dimension(400, 400); |
||
74 | private final FPoint2D centerP = new FPoint2D(sz.width/2, sz.height/2); |
||
75 | private static final int OUTTER_TOLERANCE = 1; |
||
76 | private static final int INNER_TOLERANCE = 1; |
||
77 | private IFillSymbol[] symbols; |
||
78 | private IDrawFillSymbol[] drawFillSymbols; |
||
79 | private static ArrayList classesToTest; |
||
80 | |||
81 | private static final double sizes[] = new double[] { |
||
82 | 200,
|
||
83 | 100,
|
||
84 | 50,
|
||
85 | 30,
|
||
86 | 16,
|
||
87 | 5,
|
||
88 | 3,
|
||
89 | 2,
|
||
90 | // smaller sizes don't make any sense
|
||
91 | |||
92 | }; |
||
93 | |||
94 | |||
95 | private static ArrayList getClassesToTest() { |
||
96 | if (classesToTest == null) { |
||
97 | classesToTest = new ArrayList(); |
||
98 | // TestDrawFills.addSymbolToTest(DrawLineFillSymbol.class);
|
||
99 | // TestDrawFills.addSymbolToTest(DrawPictureFillSymbol.class);
|
||
100 | } |
||
101 | |||
102 | return classesToTest;
|
||
103 | } |
||
104 | |||
105 | public static void addSymbolToTest(Class symbolClass) { |
||
106 | try {
|
||
107 | IDrawFillSymbol sym = (IDrawFillSymbol) symbolClass.newInstance(); |
||
108 | } catch (InstantiationException e) { |
||
109 | // TODO Auto-generated catch block
|
||
110 | fail("Instantiating class, cannot test a non-instantiable symbol");
|
||
111 | } catch (IllegalAccessException e) { |
||
112 | // TODO Auto-generated catch block
|
||
113 | fail("Class not instantiable");
|
||
114 | } catch (ClassCastException ccEx) { |
||
115 | fail("Cannot test a non symbol class");
|
||
116 | } |
||
117 | getClassesToTest().add(symbolClass); |
||
118 | } |
||
119 | |||
120 | |||
121 | protected void setUp() throws Exception { |
||
122 | // get all the symbols in the Test Suite
|
||
123 | super.setUp();
|
||
124 | |||
125 | this.drawFillSymbols = new IDrawFillSymbol[getClassesToTest().size()]; |
||
126 | |||
127 | for (int i = 0; i < drawFillSymbols.length; i++) { |
||
128 | drawFillSymbols[i] = (IDrawFillSymbol) ((Class) getClassesToTest().get(i)).newInstance();
|
||
129 | } |
||
130 | |||
131 | |||
132 | ISymbol[] allSymbols = TestISymbol.getNewSymbolInstances();
|
||
133 | // Filter the marker ones
|
||
134 | ArrayList symbols = new ArrayList(); |
||
135 | |||
136 | for (int i = 0; i < allSymbols.length; i++) { |
||
137 | if (allSymbols[i] instanceof IFillSymbol) { |
||
138 | IFillSymbol sym = (IFillSymbol) allSymbols[i]; |
||
139 | symbols.add(sym); |
||
140 | |||
141 | } |
||
142 | } |
||
143 | this.symbols = (IFillSymbol[]) symbols.toArray(new IFillSymbol[symbols.size()]); |
||
144 | |||
145 | |||
146 | |||
147 | } |
||
148 | |||
149 | public void testDraw() { |
||
150 | MapContext mc = AllTests.newMapContext(AllTests.TEST_DEFAULT_MERCATOR_PROJECTION); |
||
151 | |||
152 | } |
||
153 | |||
154 | |||
155 | public final void testNullColorAvoidsFilling() { |
||
156 | |||
157 | for (int i = 0; i < symbols.length; i++) { |
||
158 | |||
159 | for (int s = 0; s < sizes.length; s++) { |
||
160 | |||
161 | BufferedImage bi = new BufferedImage(sz.width,sz.height, BufferedImage.TYPE_INT_ARGB); |
||
162 | // the graphics for the image, so we can draw onto the buffered image
|
||
163 | Graphics2D g = bi.createGraphics();
|
||
164 | |||
165 | IFillSymbol newSymbol = symbols[i]; |
||
166 | newSymbol.setFillColor(null);
|
||
167 | newSymbol.setOutline(null);
|
||
168 | |||
169 | |||
170 | |||
171 | |||
172 | String name = newSymbol.getClassName().substring(
|
||
173 | newSymbol.getClassName().lastIndexOf('.')+1, |
||
174 | newSymbol.getClassName().length()); |
||
175 | |||
176 | |||
177 | Point2D firstPoint = new Point2D.Float((float)(centerP.getX()-sizes[s]/2-3),(float)(centerP.getY()-sizes[s]/2-3)); |
||
178 | Point2D lastPoint = new Point2D.Float((float)(centerP.getX()+sizes[s]/2+3),(float)(centerP.getY()+sizes[s]/2+3)); |
||
179 | |||
180 | |||
181 | |||
182 | GeneralPathX gpx = new GeneralPathX();
|
||
183 | |||
184 | gpx.moveTo((float)firstPoint.getX(),(float) firstPoint.getY()); |
||
185 | gpx.lineTo((float)lastPoint.getX(),(float) firstPoint.getY()); |
||
186 | gpx.lineTo((float)lastPoint.getX(),(float) lastPoint.getY()); |
||
187 | gpx.lineTo((float)firstPoint.getX(),(float) lastPoint.getY()); |
||
188 | gpx.lineTo((float)firstPoint.getX(), (float)firstPoint.getY()); |
||
189 | gpx.closePath(); |
||
190 | |||
191 | |||
192 | for (int j = 0; j < drawFillSymbols.length; j++) { |
||
193 | if (drawFillSymbols[j].isSuitableFor(newSymbol)) {
|
||
194 | newSymbol = drawFillSymbols[j].makeSymbolTransparent(newSymbol); |
||
195 | break;
|
||
196 | } |
||
197 | } |
||
198 | |||
199 | |||
200 | newSymbol.draw(g, new AffineTransform(), new FPolygon2D(gpx), null); |
||
201 | |||
202 | try {
|
||
203 | |||
204 | File dstDir = new File (System.getProperty("java.io.tmpdir")+"/prova-imatges/"); |
||
205 | if (!dstDir.exists()) dstDir.mkdir();
|
||
206 | ImageIO.write(bi, "png", |
||
207 | new File(dstDir.getAbsoluteFile()+File.separator+ |
||
208 | name+"_size_"+sizes[s]
|
||
209 | +".png"));
|
||
210 | } catch (IOException e) { |
||
211 | e.printStackTrace(); |
||
212 | fail(); |
||
213 | } |
||
214 | |||
215 | for (int j = 0; j < bi.getWidth(); j++) { |
||
216 | for (int k = 0; k < bi.getHeight(); k++) { |
||
217 | if (isInsideShape(new FPolygon2D(gpx), j, k)) { |
||
218 | assertEquals("Failed, the fill is being painted when the color is null",bi.getRGB(j, k),0); |
||
219 | |||
220 | } |
||
221 | } |
||
222 | |||
223 | } |
||
224 | } |
||
225 | } |
||
226 | } |
||
227 | |||
228 | |||
229 | private boolean isInsideShape(FShape shp, int x, int y) { |
||
230 | Point2D p = new Point2D.Float(x,y); |
||
231 | if(shp.contains(p))return true; |
||
232 | return false; |
||
233 | } |
||
234 | |||
235 | |||
236 | } |