svn-gvsig-desktop / trunk / extensions / extSymbology / src / org / gvsig / symbology / fmap / styles / ImageStyle.java @ 25807
History | View | Annotate | Download (3.87 KB)
1 | 20768 | jdominguez | /* 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 | 22644 | vcaballero | import com.iver.cit.gvsig.fmap.core.symbols.SymbolDrawingException; |
57 | 20768 | jdominguez | |
58 | /**
|
||
59 | * Controls the style of an image to be correctly painted. This class controls
|
||
60 | * aspects like the source path of the image, creates a rectangle to paint inside
|
||
61 | * the image, draws the outline of the image and so on.
|
||
62 | *
|
||
63 | * @author jaume dominguez faus - jaume.dominguez@iver.es
|
||
64 | */
|
||
65 | public class ImageStyle extends BackgroundFileStyle { |
||
66 | private BufferedImage img; |
||
67 | /**
|
||
68 | * Creates a rectangle with the dimensions of the buffered image
|
||
69 | * @return Rectangle
|
||
70 | */
|
||
71 | public Rectangle getBounds() { |
||
72 | 22644 | vcaballero | if (img == null) return new Rectangle(); |
73 | 20768 | jdominguez | return new Rectangle(new Dimension(img.getWidth(), img.getHeight())); |
74 | } |
||
75 | /**
|
||
76 | * Defines the source (file) from where the buffered image will be taken.
|
||
77 | * @param f,File
|
||
78 | */
|
||
79 | public void setSource(URL url) throws IOException { |
||
80 | File f = new File(url.getFile()); |
||
81 | if(f.isAbsolute()){
|
||
82 | sourceFile = url; |
||
83 | img = ImageIO.read(f);
|
||
84 | this.isRelativePath=false; |
||
85 | } |
||
86 | else {
|
||
87 | sourceFile = new URL(SymbologyFactory.SymbolLibraryPath + File.separator + f.getPath()); |
||
88 | img = ImageIO.read(f);
|
||
89 | this.isRelativePath=true; |
||
90 | } |
||
91 | } |
||
92 | |||
93 | public void drawInsideRectangle(Graphics2D g, Rectangle r, boolean keepAspectRatio) { |
||
94 | 22082 | vcaballero | if (img != null) { |
95 | |||
96 | double xOffset = 0; |
||
97 | double yOffset = 0; |
||
98 | double xScale = 1; |
||
99 | double yScale = 1; |
||
100 | if (keepAspectRatio) {
|
||
101 | double scale;
|
||
102 | if (img.getWidth()>img.getHeight()) {
|
||
103 | scale = r.getWidth()/img.getWidth(); |
||
104 | yOffset = 0.5*(r.getHeight() - img.getHeight()*scale);
|
||
105 | } else {
|
||
106 | scale = r.getHeight()/img.getHeight(); |
||
107 | xOffset = 0.5*(r.getWidth() - img.getWidth()*scale);
|
||
108 | } |
||
109 | xScale = yScale = scale; |
||
110 | |||
111 | 20768 | jdominguez | } else {
|
112 | 22082 | vcaballero | xScale = r.getWidth()/img.getWidth(); |
113 | yScale = r.getHeight()/img.getHeight(); |
||
114 | yOffset = img.getHeight()*0.5*yScale ;
|
||
115 | |||
116 | 20768 | jdominguez | } |
117 | 22082 | vcaballero | |
118 | |||
119 | AffineTransform at = AffineTransform.getTranslateInstance(xOffset, yOffset); |
||
120 | at.concatenate(AffineTransform.getScaleInstance(xScale, yScale));
|
||
121 | g.drawRenderedImage(img, at); |
||
122 | 20768 | jdominguez | } |
123 | } |
||
124 | |||
125 | public boolean isSuitableFor(ISymbol symbol) { |
||
126 | // TODO Implement it
|
||
127 | throw new Error("Not yet implemented!"); |
||
128 | |||
129 | } |
||
130 | |||
131 | 22644 | vcaballero | public void drawOutline(Graphics2D g, Rectangle r) throws SymbolDrawingException { |
132 | 20768 | jdominguez | drawInsideRectangle(g, r); |
133 | } |
||
134 | |||
135 | public String getClassName() { |
||
136 | return getClass().getName();
|
||
137 | } |
||
138 | |||
139 | |||
140 | } |