Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / styles / MarkerFillStyle.java @ 11051

History | View | Annotate | Download (3.78 KB)

1
package com.iver.cit.gvsig.fmap.core.styles;
2

    
3
import java.awt.Graphics;
4
import java.awt.Graphics2D;
5
import java.awt.Paint;
6
import java.awt.Rectangle;
7
import java.awt.RenderingHints;
8
import java.awt.TexturePaint;
9
import java.awt.geom.AffineTransform;
10
import java.awt.image.BufferedImage;
11

    
12
import com.iver.cit.gvsig.fmap.core.FPoint2D;
13
import com.iver.cit.gvsig.fmap.core.FShape;
14
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol;
15
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
16
import com.iver.cit.gvsig.fmap.core.symbols.SimpleMarkerSymbol;
17
import com.iver.utiles.XMLEntity;
18

    
19
public class MarkerFillStyle extends AbstractStyle implements IMarkerFillStyle {
20
        public static final int RANDOM_FILL = 3;
21
        public static final int GRID_FILL = 1;
22
        public static final int SINGLE_CENTERED_SYMBOL = 2;
23

    
24
        private int fillStyle;
25
        /* define an utility symbol to show up a thumbnail.
26
         * By default, this symbol is a SimpleMarkerSymbol.
27
         * Thus, the drawInsideRectangle will always work. But
28
         * it can be changed with setSampleSymbol(IMakerSymbol)
29
         */
30
        private IMarkerSymbol sampleSymbol = new SimpleMarkerSymbol();
31

    
32
        public void drawInsideRectangle(Graphics2D g, Rectangle r) {
33
                switch (fillStyle) {
34
                case SINGLE_CENTERED_SYMBOL:
35
                        FPoint2D p = new FPoint2D(r.getCenterX(), r.getCenterY());
36
                        sampleSymbol.draw(g, null, p);
37
                        break;
38
                case GRID_FILL:
39
                {
40
                        int size = (int) sampleSymbol.getSize();
41

    
42
                        Rectangle rProv = new Rectangle();
43

    
44
                        rProv.setFrame(0, 0,size,size);
45
                        Paint resulPatternFill = null;
46
                        BufferedImage bi = null;
47
                        bi= new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
48
                        Graphics2D gAux = bi.createGraphics();
49
                        sampleSymbol.drawInsideRectangle(gAux, new AffineTransform(), rProv);
50
                        resulPatternFill = new TexturePaint(bi,rProv);
51
                        g.setColor(null);
52
                        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
53
                                        RenderingHints.VALUE_ANTIALIAS_ON);
54
                        g.setPaint(resulPatternFill);
55
                        g.fill(r);
56
                }
57
                        break;
58
                case RANDOM_FILL:
59
                        int x = r.x;
60
                        int y = r.y;
61
                        int width = r.width;
62
                        int height= r.height;
63
                        g.setBackground(null);
64

    
65
                        sampleSymbol.draw(g, null, new FPoint2D((x+width*0.2), (y+height*0.8)));
66
                        sampleSymbol.draw(g, null, new FPoint2D((x+width*0.634), (y+height*0.3)));
67
                        sampleSymbol.draw(g, null, new FPoint2D((x+width*0.26), (y+height*0.35)));
68
                        sampleSymbol.draw(g, null, new FPoint2D((x+width*0.45), (y+height*0.98)));
69
                        sampleSymbol.draw(g, null, new FPoint2D((x+width*0.9), (y+height*0.54)));
70
                        sampleSymbol.draw(g, null, new FPoint2D((x+width*1.1), (y+height*0.7)));
71
                        break;
72
                }
73
        }
74

    
75
        /**
76
         * <p>
77
         * Define an utility symbol to show up a thumbnail
78
         * by default, this symbol is a SimpleMarkerSymbol.
79
         * Thus, the drawInsideRectangle will always work. But
80
         * it can be changed with setSampleSymbol(IMakerSymbol).<br>
81
         * </p>
82
         * <p>
83
         * If <b>marker</b> is null, it does nothing
84
         * </p>
85
         */
86
        public void setSampleSymbol(IMarkerSymbol marker) {
87
                if (marker != null)
88
                        this.sampleSymbol = marker;
89
        }
90

    
91
        public boolean isSuitableFor(ISymbol symbol) {
92
                return symbol.getSymbolType() == FShape.POLYGON;
93
        }
94

    
95
        public String getClassName() {
96
                return getClass().getName();
97
        }
98

    
99
        public XMLEntity getXMLEntity() {
100
                XMLEntity xml = new XMLEntity();
101
                xml.putProperty("className", getClassName());
102
                xml.putProperty("fillStyle", fillStyle);
103

    
104
                // please, avoid persist "sampleSymbol" field.
105
                // it is always initialized by this style's owner
106
                // when needs it.
107
                return xml;
108
        }
109

    
110
        public void setXMLEntity(XMLEntity xml) {
111
                fillStyle = xml.getIntProperty("fillStyle");
112

    
113
                // please, avoid initialize "sampleSymbol" field. It
114
                // is already controlled by this style's owner.
115
        }
116

    
117
        public int getFillStyle() {
118
                return fillStyle;
119
        }
120

    
121
        public void setFillStyle(int fillStyle) {
122
                this.fillStyle = fillStyle;
123
        }
124

    
125
        public void drawOutline(Graphics2D g, Rectangle r) {
126
                drawInsideRectangle(g, r);
127
        }
128
}