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