Statistics
| Revision:

root / trunk / extensions / extSymbology / src / org / gvsig / symbology / fmap / styles / ImageStyle.java @ 20768

History | View | Annotate | Download (3.69 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.symbology.fmap.styles;
42

    
43
import java.awt.Dimension;
44
import java.awt.Graphics2D;
45
import java.awt.Rectangle;
46
import java.awt.geom.AffineTransform;
47
import java.awt.image.BufferedImage;
48
import java.io.File;
49
import java.io.IOException;
50
import java.net.URL;
51

    
52
import javax.imageio.ImageIO;
53

    
54
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
55
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
56

    
57
/**
58
 * Controls the style of an image to be correctly painted. This class controls
59
 * aspects like the source path of the image, creates a rectangle to paint inside 
60
 * the image, draws the outline of the image and so on.
61
 * 
62
 * @author jaume dominguez faus - jaume.dominguez@iver.es
63
 */
64
public class ImageStyle extends BackgroundFileStyle {
65
        private BufferedImage img;
66
        /**
67
         * Creates a rectangle with the dimensions of the buffered image
68
         * @return Rectangle
69
         */
70
        public Rectangle getBounds() {
71
                return new Rectangle(new Dimension(img.getWidth(), img.getHeight()));
72
        }
73
        /**
74
         * Defines the source (file) from where the buffered image will be taken.
75
         * @param f,File
76
         */
77
        public void setSource(URL url) throws IOException {
78
                File f = new File(url.getFile());
79
                if(f.isAbsolute()){
80
                        sourceFile = url;
81
                        img = ImageIO.read(f);
82
                        this.isRelativePath=false;
83
                }
84
                else {
85
                        sourceFile = new URL(SymbologyFactory.SymbolLibraryPath + File.separator + f.getPath());
86
                        img = ImageIO.read(f);
87
                        this.isRelativePath=true;        
88
                }
89
        }
90

    
91
        public void drawInsideRectangle(Graphics2D g, Rectangle r, boolean keepAspectRatio) {
92
                double xOffset = 0;
93
                double yOffset = 0;
94
                double xScale = 1;
95
                double yScale = 1;
96
                if (keepAspectRatio) {
97
                        double scale;
98
                        if (img.getWidth()>img.getHeight()) {
99
                                scale = r.getWidth()/img.getWidth();
100
                                yOffset = 0.5*(r.getHeight() - img.getHeight()*scale);
101
                        } else {
102
                                scale = r.getHeight()/img.getHeight();
103
                                xOffset = 0.5*(r.getWidth() - img.getWidth()*scale);
104
                        }
105
                        xScale = yScale = scale;
106
                        
107
                } else {
108
                        xScale = r.getWidth()/img.getWidth();
109
                        yScale = r.getHeight()/img.getHeight();
110
                        yOffset = img.getHeight()*0.5*yScale ;
111
                        
112
                }
113
                
114

    
115
                AffineTransform at = AffineTransform.getTranslateInstance(xOffset, yOffset);
116
                at.concatenate(AffineTransform.getScaleInstance(xScale, yScale));
117
                g.drawRenderedImage(img, at);
118
        }
119

    
120
        public boolean isSuitableFor(ISymbol symbol) {
121
                // TODO Implement it
122
                throw new Error("Not yet implemented!");
123

    
124
        }
125

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

    
130
        public String getClassName() {
131
                return getClass().getName();
132
        }
133

    
134

    
135
}