Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGraph_predes / src / com / iver / cit / gvsig / fmap / core / v02 / FArrowSymbol.java @ 9173

History | View | Annotate | Download (6.8 KB)

1
/*
2
 * Created on 25-oct-2006 by azabala
3
 *
4
 */
5
package com.iver.cit.gvsig.fmap.core.v02;
6

    
7
import java.awt.BasicStroke;
8
import java.awt.Color;
9
import java.awt.Graphics2D;
10
import java.awt.Rectangle;
11
import java.awt.Shape;
12
import java.awt.Stroke;
13
import java.awt.geom.AffineTransform;
14
import java.awt.geom.PathIterator;
15
import java.awt.image.BufferedImage;
16
import java.util.ArrayList;
17

    
18
import javax.print.attribute.PrintRequestAttributeSet;
19

    
20
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
21
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
22
import com.iver.cit.gvsig.fmap.core.FShape;
23
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
24
import com.iver.cit.gvsig.fmap.core.IGeometry;
25
import com.iver.cit.gvsig.fmap.core.ISymbol;
26
import com.iver.utiles.StringUtilities;
27
import com.iver.utiles.XMLEntity;
28

    
29
/**
30
 * Symbol to draw a line with an arrow at the end.
31
 * @author alzabord
32
 */
33
public class FArrowSymbol implements ISymbol {
34

    
35
        protected static Color selectionColor = Color.YELLOW;
36
        
37
        protected static BufferedImage img = new BufferedImage(1, 1,
38
                        BufferedImage.TYPE_INT_ARGB);
39
        
40
        protected static Rectangle rect = new Rectangle(0, 0, 1, 1);
41
        
42
        protected Color symColor;
43
        
44
        protected Stroke m_Stroke;
45
        
46
        protected int m_symbolType = FConstant.SYMBOL_TYPE_LINE;
47

    
48
        protected String m_Descrip;
49
        
50
        protected int rgb;
51

    
52
        protected boolean m_bDrawShape = true;
53
        
54
        protected int lenghtArrow = 15;
55
        protected int widthArrow = 10;
56
        
57
        
58
        
59
        
60
        public FArrowSymbol(Color symColor){
61
                this.symColor = symColor;
62
                calculateRgb();
63
        }
64
        
65
        public void calculateRgb() {
66
                // Recalculamos el RGB
67
                Graphics2D g2 = img.createGraphics();
68
                drawInsideRectangle(g2, g2.getTransform(), rect);
69
                rgb = img.getRGB(0, 0);
70
        }
71
        
72
        /**
73
         * Devuelve el color que se aplica a los shapes seleccionados.
74
         *
75
         * @return DOCUMENT ME!
76
         */
77
        public static Color getSelectionColor() {
78
                return selectionColor;
79
        }
80
        
81
        
82
        public Color getColor(){
83
                return symColor;
84
        }
85
        
86
        public Stroke getStroke(){
87
                return m_Stroke;
88
        }
89
        
90
        public void setStroke(Stroke stroke){
91
                this.m_Stroke = stroke;
92
        }
93
        
94
        
95
        /* (non-Javadoc)
96
         * @see com.iver.cit.gvsig.fmap.core.ISymbol#getSymbolForSelection()
97
         */
98
        public ISymbol getSymbolForSelection() {
99
                FArrowSymbol solution = new FArrowSymbol(selectionColor);
100
                solution.m_bDrawShape = m_bDrawShape;
101
                solution.m_Stroke = m_Stroke;
102
                return solution;
103
        }
104
        
105
        
106
        
107

    
108
        
109
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp) {
110
                
111
                //1? dibujamos la linea
112
                if (shp == null || (! isShapeVisible())) {
113
                        return;
114
                }
115
        g.setColor(getColor());
116
                if (getStroke() != null) {
117
                        g.setStroke(getStroke());
118
                }
119
                g.draw(shp);        
120
                
121
                /*
122
                 * Ahora intentamos obtener el ultimo segmento
123
                 * */
124
                PathIterator theIterator;
125
                theIterator = shp.getPathIterator(null, 0.8);
126
                int theType;
127
                double[] theData = new double[2];
128
                ArrayList arrayCoords = new ArrayList();
129
                while (!theIterator.isDone()) {
130
                        theType = theIterator.currentSegment(theData);
131
                    if(theType == PathIterator.SEG_LINETO || theType == PathIterator.SEG_MOVETO)
132
                            arrayCoords.add(theData);
133
                    theData = new double[2];
134
                        theIterator.next();
135
                } //end while loop
136
                
137
                double length = 0d;
138
                double[] previous = null;
139
                for(int i = 0; i < arrayCoords.size(); i++ ){
140
                        double[] coords = (double[]) arrayCoords.get(i);
141
                        if(previous == null)
142
                                previous = coords;
143
                        else{
144
                                double dx = coords[0] - previous[0];
145
                                double dy = coords[1] - previous[1];
146
                                double dist = Math.sqrt( ( dx * dx ) + (dy * dy));
147
                                length += dist;
148
                        }//else
149
                }//for
150
                
151
                if(lenghtArrow > (0.5 * length))//to avoid arrows collisions
152
                        return;
153
                
154
                double[] last = (double[]) arrayCoords.get(arrayCoords.size() -1);
155
                double[] prevLast = (double[]) arrayCoords.get(arrayCoords.size() -2);
156
                
157
                
158
                double mx = last[0];
159
                double my = last[1];
160
                double Mx = prevLast[0];
161
                double My = prevLast[1];
162
//                
163

    
164
                // tama?o de la flecha
165
                double tipLength = lenghtArrow;
166
                double tipWidth = widthArrow;
167

    
168
                double        tip1x = mx + (((Mx - mx) * tipLength + ( tipWidth / 2)*(my - My))/
169
                                                Math.sqrt(( my - My) * (my - My)+(mx-Mx)*(mx-Mx)));
170
                
171
                double  tip2x = mx + (((Mx-mx) * tipLength-(tipWidth/2)*(my-My))/
172
                                                Math.sqrt((my-My)*(my-My)+(mx-Mx)*(mx-Mx)));
173
                
174
                double  tip1y = my + (((My-my)*tipLength-(tipWidth/2)*(mx-Mx))/
175
                                                 Math.sqrt((my-My)*(my-My)+(mx-Mx)*(mx-Mx)));
176
                
177
                double tip2y = my + (((My-my)*tipLength+(tipWidth/2)*(mx-Mx))/
178
                                    Math.sqrt((my-My)*(my-My)+(mx-Mx)*(mx-Mx)));
179
                
180
                GeneralPathX path = new GeneralPathX();
181
                path.moveTo(mx, my);
182
                path.lineTo(tip1x, tip1y);
183
                path.lineTo(tip2x, tip2y);
184
                path.closePath();
185
                FPolygon2D arrow = new FPolygon2D(path);
186
                g.fill(arrow);
187
        }
188

    
189
        
190
        public int getPixExtentPlus(Graphics2D g, AffineTransform affineTransform, Shape shp) {
191
                // TODO Auto-generated method stub
192
                return 0;
193
        }
194

    
195
        
196
        
197
        
198
        
199
        public int getOnePointRgb() {
200
                return rgb;
201
        }
202

    
203
        
204
        public XMLEntity getXMLEntity() {
205
                XMLEntity xml = new XMLEntity();
206
                xml.putProperty("className",this.getClass().getName());
207
                xml.putProperty("m_symbolType", getSymbolType());
208
                if (getColor() != null) {
209
                        xml.putProperty("m_Color", StringUtilities.color2String(getColor()));
210
                }
211
                xml.putProperty("m_bDrawShape", isShapeVisible());
212
                
213
                
214
                //Ancho del stroke en float
215
                if (getStroke() != null) {
216
                        xml.putProperty("m_stroke",
217
                                ((BasicStroke) getStroke()).getLineWidth());
218
                } else {
219
                        xml.putProperty("m_stroke", 0f);
220
                }
221
                xml.putProperty("rgb", rgb);
222

    
223
                return xml;
224
        }
225

    
226
        
227
        
228
        public String getDescription() {
229
                return null;
230
        }
231

    
232
        
233
        
234
        
235
        
236
        public boolean isShapeVisible() {
237
                return m_bDrawShape;
238
        }
239
        
240
        
241
        
242
        public void setDescription(String m_Descrip) {
243
                this.m_Descrip = m_Descrip;
244
        }
245

    
246
        
247
        public int getSymbolType() {
248
                return m_symbolType;
249
        }
250
        
251
        
252

    
253
        public boolean isSuitableFor(IGeometry geom) {
254
                return true;
255
        }
256

    
257
        //TODO Copypasteado de graphics utilities para symboltype LINE
258
        public void drawInsideRectangle(Graphics2D g, 
259
                        AffineTransform scaleInstance, 
260
                        Rectangle r) {
261
                
262
                FShape shp;
263
                AffineTransform mT = new AffineTransform();
264
                mT.setToIdentity();
265

    
266
                Rectangle rect = mT.createTransformedShape(r).getBounds();
267
                GeneralPathX line = new GeneralPathX();
268
                
269
                
270
                
271
                line.moveTo(rect.x, rect.y + (rect.height / 2));
272
                line.curveTo(rect.x + (rect.width / 3),
273
                        rect.y + (2 * rect.height),
274
                        rect.x + ((2 * rect.width) / 3), rect.y - rect.height,
275
                        rect.x + rect.width, rect.y + (rect.height / 2));
276

    
277
                shp = new FPolyline2D(line);
278
                draw(g, mT, shp);
279
        }
280

    
281
        
282
        
283

    
284
        
285
        public void setXMLEntity(XMLEntity xml) {
286
                
287
                
288
        }
289

    
290

    
291
        public String getClassName() {
292
                // TODO Auto-generated method stub
293
                return null;
294
        }
295
        
296
        
297
        /**
298
         * @return Returns the m_bDrawShape.
299
         */
300
        public boolean isM_bDrawShape() {
301
                return m_bDrawShape;
302
        }
303
        /**
304
         * @param drawShape The m_bDrawShape to set.
305
         */
306
        public void setM_bDrawShape(boolean drawShape) {
307
                m_bDrawShape = drawShape;
308
        }
309

    
310
        public void setPrintingProperties(PrintRequestAttributeSet printProperties) {
311
                // TODO Auto-generated method stub
312
                
313
        }
314
}