Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / v02 / FArrowSymbol.java @ 9641

History | View | Annotate | Download (6.93 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.DriverException;
21
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
22
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
23
import com.iver.cit.gvsig.fmap.core.FShape;
24
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
25
import com.iver.cit.gvsig.fmap.core.IGeometry;
26
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
27
import com.iver.utiles.StringUtilities;
28
import com.iver.utiles.XMLEntity;
29

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

    
36
        protected static Color selectionColor = Color.YELLOW;
37

    
38
        protected static BufferedImage img = new BufferedImage(1, 1,
39
                        BufferedImage.TYPE_INT_ARGB);
40

    
41
        protected static Rectangle rect = new Rectangle(0, 0, 1, 1);
42

    
43
        protected Color symColor;
44

    
45
        protected Stroke m_Stroke;
46

    
47
        protected int m_symbolType = FConstant.SYMBOL_TYPE_LINE;
48

    
49
        protected String m_Descrip;
50

    
51
        protected int rgb;
52

    
53
        protected boolean m_bDrawShape = true;
54

    
55
        protected int lenghtArrow = 15;
56
        protected int widthArrow = 10;
57

    
58

    
59

    
60

    
61
        public FArrowSymbol(Color symColor){
62
                this.symColor = symColor;
63
                calculateRgb();
64
        }
65

    
66
        public void calculateRgb() {
67
                // Recalculamos el RGB
68
                Graphics2D g2 = img.createGraphics();
69
                drawInsideRectangle(g2, g2.getTransform(), rect);
70
                rgb = img.getRGB(0, 0);
71
        }
72

    
73
        /**
74
         * Devuelve el color que se aplica a los shapes seleccionados.
75
         *
76
         * @return DOCUMENT ME!
77
         */
78
        public static Color getSelectionColor() {
79
                return selectionColor;
80
        }
81

    
82

    
83
        public Color getColor(){
84
                return symColor;
85
        }
86

    
87
        public Stroke getStroke(){
88
                return m_Stroke;
89
        }
90

    
91
        public void setStroke(Stroke stroke){
92
                this.m_Stroke = stroke;
93
        }
94

    
95

    
96
        /* (non-Javadoc)
97
         * @see com.iver.cit.gvsig.fmap.core.ISymbol#getSymbolForSelection()
98
         */
99
        public ISymbol getSymbolForSelection() {
100
                FArrowSymbol solution = new FArrowSymbol(selectionColor);
101
                solution.m_bDrawShape = m_bDrawShape;
102
                solution.m_Stroke = m_Stroke;
103
                return solution;
104
        }
105

    
106

    
107

    
108

    
109

    
110
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp) {
111

    
112
                //1? dibujamos la linea
113
                if (shp == null || (! isShapeVisible())) {
114
                        return;
115
                }
116
        g.setColor(getColor());
117
                if (getStroke() != null) {
118
                        g.setStroke(getStroke());
119
                }
120
                g.draw(shp);
121

    
122
                /*
123
                 * Ahora intentamos obtener el ultimo segmento
124
                 * */
125
                PathIterator theIterator;
126
                theIterator = shp.getPathIterator(null, 0.8);
127
                int theType;
128
                double[] theData = new double[2];
129
                ArrayList arrayCoords = new ArrayList();
130
                while (!theIterator.isDone()) {
131
                        theType = theIterator.currentSegment(theData);
132
                    if(theType == PathIterator.SEG_LINETO || theType == PathIterator.SEG_MOVETO)
133
                            arrayCoords.add(theData);
134
                    theData = new double[2];
135
                        theIterator.next();
136
                } //end while loop
137

    
138
                double length = 0d;
139
                double[] previous = null;
140
                for(int i = 0; i < arrayCoords.size(); i++ ){
141
                        double[] coords = (double[]) arrayCoords.get(i);
142
                        if(previous == null)
143
                                previous = coords;
144
                        else{
145
                                double dx = coords[0] - previous[0];
146
                                double dy = coords[1] - previous[1];
147
                                double dist = Math.sqrt( ( dx * dx ) + (dy * dy));
148
                                length += dist;
149
                        }//else
150
                }//for
151

    
152
                if(lenghtArrow > (0.5 * length))//to avoid arrows collisions
153
                        return;
154

    
155
                double[] last = (double[]) arrayCoords.get(arrayCoords.size() -1);
156
                double[] prevLast = (double[]) arrayCoords.get(arrayCoords.size() -2);
157

    
158

    
159
                double mx = last[0];
160
                double my = last[1];
161
                double Mx = prevLast[0];
162
                double My = prevLast[1];
163
//
164

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

    
169
                double        tip1x = mx + (((Mx - mx) * tipLength + ( tipWidth / 2)*(my - My))/
170
                                                Math.sqrt(( my - My) * (my - My)+(mx-Mx)*(mx-Mx)));
171

    
172
                double  tip2x = mx + (((Mx-mx) * tipLength-(tipWidth/2)*(my-My))/
173
                                                Math.sqrt((my-My)*(my-My)+(mx-Mx)*(mx-Mx)));
174

    
175
                double  tip1y = my + (((My-my)*tipLength-(tipWidth/2)*(mx-Mx))/
176
                                                 Math.sqrt((my-My)*(my-My)+(mx-Mx)*(mx-Mx)));
177

    
178
                double tip2y = my + (((My-my)*tipLength+(tipWidth/2)*(mx-Mx))/
179
                                    Math.sqrt((my-My)*(my-My)+(mx-Mx)*(mx-Mx)));
180

    
181
                GeneralPathX path = new GeneralPathX();
182
                path.moveTo(mx, my);
183
                path.lineTo(tip1x, tip1y);
184
                path.lineTo(tip2x, tip2y);
185
                path.closePath();
186
                FPolygon2D arrow = new FPolygon2D(path);
187
                g.fill(arrow);
188
        }
189

    
190

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

    
196

    
197

    
198

    
199

    
200
        public int getOnePointRgb() {
201
                return rgb;
202
        }
203

    
204

    
205
        public XMLEntity getXMLEntity() {
206
                XMLEntity xml = new XMLEntity();
207
                xml.putProperty("className",this.getClass().getName());
208
                xml.putProperty("m_symbolType", getSymbolType());
209
                if (getColor() != null) {
210
                        xml.putProperty("m_Color", StringUtilities.color2String(getColor()));
211
                }
212
                xml.putProperty("m_bDrawShape", isShapeVisible());
213

    
214

    
215
                //Ancho del stroke en float
216
                if (getStroke() != null) {
217
                        xml.putProperty("m_stroke",
218
                                ((BasicStroke) getStroke()).getLineWidth());
219
                } else {
220
                        xml.putProperty("m_stroke", 0f);
221
                }
222
                xml.putProperty("rgb", rgb);
223

    
224
                return xml;
225
        }
226

    
227

    
228

    
229
        public String getDescription() {
230
                return null;
231
        }
232

    
233

    
234

    
235

    
236

    
237
        public boolean isShapeVisible() {
238
                return m_bDrawShape;
239
        }
240

    
241

    
242

    
243
        public void setDescription(String m_Descrip) {
244
                this.m_Descrip = m_Descrip;
245
        }
246

    
247

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

    
252

    
253

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

    
258
        //TODO Copypasteado de graphics utilities para symboltype LINE
259
        public void drawInsideRectangle(Graphics2D g,
260
                        AffineTransform scaleInstance,
261
                        Rectangle r) {
262

    
263
                FShape shp;
264
                AffineTransform mT = new AffineTransform();
265
                mT.setToIdentity();
266

    
267
                Rectangle rect = mT.createTransformedShape(r).getBounds();
268
                GeneralPathX line = new GeneralPathX();
269

    
270

    
271

    
272
                line.moveTo(rect.x, rect.y + (rect.height / 2));
273
                line.curveTo(rect.x + (rect.width / 3),
274
                        rect.y + (2 * rect.height),
275
                        rect.x + ((2 * rect.width) / 3), rect.y - rect.height,
276
                        rect.x + rect.width, rect.y + (rect.height / 2));
277

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

    
282

    
283

    
284

    
285

    
286
        public void setXMLEntity(XMLEntity xml) {
287

    
288

    
289
        }
290

    
291

    
292
        public String getClassName() {
293
                // TODO Auto-generated method stub
294
                return null;
295
        }
296

    
297

    
298
        /**
299
         * @return Returns the m_bDrawShape.
300
         */
301
        public boolean isM_bDrawShape() {
302
                return m_bDrawShape;
303
        }
304
        /**
305
         * @param drawShape The m_bDrawShape to set.
306
         */
307
        public void setM_bDrawShape(boolean drawShape) {
308
                m_bDrawShape = drawShape;
309
        }
310

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

    
314
        }
315

    
316
        public void print(Graphics2D g, AffineTransform at, FShape shape) throws DriverException {
317
                // TODO Implement it
318
                throw new Error("Not yet implemented!");
319

    
320
        }
321
}