Revision 45524 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.symbology/org.gvsig.symbology.lib/org.gvsig.symbology.lib.impl/src/main/java/org/gvsig/symbology/fmap/mapcontext/rendering/symbol/style/ImageStyle.java
ImageStyle.java | ||
---|---|---|
44 | 44 |
|
45 | 45 |
/** |
46 | 46 |
* Controls the style of an image to be correctly painted. This class controls |
47 |
* aspects like the source path of the image, creates a rectangle to paint inside
|
|
48 |
* the image, draws the outline of the image and so on. |
|
49 |
*
|
|
50 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
|
47 |
* aspects like the source path of the image, creates a rectangle to paint |
|
48 |
* inside the image, draws the outline of the image and so on.
|
|
49 |
* |
|
50 |
* @author gvSIG Team
|
|
51 | 51 |
*/ |
52 |
@SuppressWarnings("UseSpecificCatch") |
|
52 | 53 |
public class ImageStyle extends BackgroundFileStyle { |
54 |
|
|
53 | 55 |
public static final String IMAGE_STYLE_PERSISTENCE_DEFINITION_NAME = "ImageStyle"; |
54 | 56 |
private static final String SOURCE = "source"; |
55 | 57 |
|
56 |
private BufferedImage img; |
|
57 |
/** |
|
58 |
* Creates a rectangle with the dimensions of the buffered image |
|
59 |
* @return Rectangle |
|
60 |
*/ |
|
61 |
public Rectangle getBounds() { |
|
62 |
if (img == null) return new Rectangle(); |
|
63 |
return new Rectangle(new Dimension(img.getWidth(), img.getHeight())); |
|
64 |
} |
|
65 |
/** |
|
66 |
* Defines the source (file) from where the buffered image will be taken. |
|
67 |
* @param f,File |
|
68 |
*/ |
|
69 |
public void setSource(URL url) throws IOException { |
|
70 |
source = url; |
|
71 |
img = ImageIO.read(url); |
|
72 |
} |
|
58 |
private BufferedImage img; |
|
73 | 59 |
|
74 |
public void drawInsideRectangle(Graphics2D g, Rectangle r, boolean keepAspectRatio) { |
|
75 |
if (img != null) { |
|
60 |
/** |
|
61 |
* Creates a rectangle with the dimensions of the buffered image |
|
62 |
* |
|
63 |
* @return Rectangle |
|
64 |
*/ |
|
65 |
@Override |
|
66 |
public Rectangle getBounds() { |
|
67 |
if (img == null) { |
|
68 |
return new Rectangle(); |
|
69 |
} |
|
70 |
return new Rectangle(new Dimension(img.getWidth(), img.getHeight())); |
|
71 |
} |
|
76 | 72 |
|
77 |
double xOffset = 0; |
|
78 |
double yOffset = 0; |
|
79 |
double xScale = 1; |
|
80 |
double yScale = 1; |
|
81 |
if (keepAspectRatio) { |
|
82 |
double scale; |
|
83 |
if (img.getWidth()>img.getHeight()) { |
|
84 |
scale = r.getWidth()/img.getWidth(); |
|
85 |
yOffset = 0.5*(r.getHeight() - img.getHeight()*scale); |
|
86 |
} else { |
|
87 |
scale = r.getHeight()/img.getHeight(); |
|
88 |
xOffset = 0.5*(r.getWidth() - img.getWidth()*scale); |
|
89 |
} |
|
90 |
xScale = yScale = scale; |
|
73 |
/** |
|
74 |
* Defines the source (file) from where the buffered image will be taken. |
|
75 |
* |
|
76 |
* @param url |
|
77 |
*/ |
|
78 |
@Override |
|
79 |
public void setSource(URL url) throws IOException { |
|
80 |
source = url; |
|
81 |
img = ImageIO.read(url); |
|
82 |
} |
|
91 | 83 |
|
92 |
} else { |
|
93 |
xScale = r.getWidth()/img.getWidth(); |
|
94 |
yScale = r.getHeight()/img.getHeight(); |
|
95 |
yOffset = img.getHeight()*0.5*yScale ; |
|
84 |
@Override |
|
85 |
public void drawInsideRectangle(Graphics2D g, Rectangle r, boolean keepAspectRatio) { |
|
86 |
if (img != null) { |
|
96 | 87 |
|
97 |
} |
|
88 |
double xOffset = 0; |
|
89 |
double yOffset = 0; |
|
90 |
@SuppressWarnings("UnusedAssignment") |
|
91 |
double xScale = 1; |
|
92 |
@SuppressWarnings("UnusedAssignment") |
|
93 |
double yScale = 1; |
|
94 |
if (keepAspectRatio) { |
|
95 |
double scale; |
|
96 |
if (img.getWidth() > img.getHeight()) { |
|
97 |
scale = r.getWidth() / img.getWidth(); |
|
98 |
yOffset = 0.5 * (r.getHeight() - img.getHeight() * scale); |
|
99 |
} else { |
|
100 |
scale = r.getHeight() / img.getHeight(); |
|
101 |
xOffset = 0.5 * (r.getWidth() - img.getWidth() * scale); |
|
102 |
} |
|
103 |
xScale = yScale = scale; |
|
98 | 104 |
|
105 |
} else { |
|
106 |
xScale = r.getWidth() / img.getWidth(); |
|
107 |
yScale = r.getHeight() / img.getHeight(); |
|
108 |
yOffset = img.getHeight() * 0.5 * yScale; |
|
99 | 109 |
|
100 |
AffineTransform at = AffineTransform.getTranslateInstance(xOffset, yOffset); |
|
101 |
at.concatenate(AffineTransform.getScaleInstance(xScale, yScale)); |
|
102 |
g.drawRenderedImage(img, at); |
|
103 |
} |
|
104 |
} |
|
110 |
} |
|
105 | 111 |
|
106 |
public boolean isSuitableFor(ISymbol symbol) { |
|
107 |
// TODO Implement it |
|
108 |
throw new Error("Not yet implemented!"); |
|
112 |
AffineTransform at = AffineTransform.getTranslateInstance(xOffset, yOffset); |
|
113 |
at.concatenate(AffineTransform.getScaleInstance(xScale, yScale)); |
|
114 |
g.drawRenderedImage(img, at); |
|
115 |
} |
|
116 |
} |
|
109 | 117 |
|
110 |
} |
|
118 |
@Override |
|
119 |
public boolean isSuitableFor(ISymbol symbol) { |
|
120 |
// TODO Implement it |
|
121 |
throw new Error("Not yet implemented!"); |
|
111 | 122 |
|
112 |
public void drawOutline(Graphics2D g, Rectangle r) throws SymbolDrawingException { |
|
113 |
drawInsideRectangle(g, r); |
|
114 |
} |
|
123 |
} |
|
115 | 124 |
|
116 |
public void loadFromState(PersistentState state) |
|
117 |
throws PersistenceException { |
|
118 |
try { |
|
119 |
String sourceSymbolInLibrary = state.getString(SOURCE_SYMBOL_IN_LIBRARY); |
|
120 |
if (sourceSymbolInLibrary != null){ |
|
121 |
setSource(new URL(getSymbolLibraryURL().toString()+sourceSymbolInLibrary)); |
|
122 |
} else { |
|
123 |
setSource(state.getURL(SOURCE)); |
|
124 |
} |
|
125 |
@Override |
|
126 |
public void drawOutline(Graphics2D g, Rectangle r) throws SymbolDrawingException { |
|
127 |
drawInsideRectangle(g, r); |
|
128 |
} |
|
129 |
|
|
130 |
@Override |
|
131 |
public void loadFromState(PersistentState state) |
|
132 |
throws PersistenceException { |
|
133 |
try { |
|
134 |
String sourceSymbolInLibrary = state.getString(SOURCE_SYMBOL_IN_LIBRARY); |
|
135 |
if (sourceSymbolInLibrary != null) { |
|
136 |
setSource(new URL(getSymbolLibraryURL().toString() + sourceSymbolInLibrary)); |
|
137 |
} else { |
|
138 |
setSource(state.getURL(SOURCE)); |
|
139 |
} |
|
125 | 140 |
} catch (Exception e) { |
126 | 141 |
throw new PersistenceCantSetSourceException(e); |
127 | 142 |
} |
128 |
}
|
|
143 |
}
|
|
129 | 144 |
|
130 |
public void saveToState(PersistentState state) throws PersistenceException { |
|
131 |
if (isLibrarySymbol()){ |
|
132 |
state.set(SOURCE_SYMBOL_IN_LIBRARY, getSourceSymbolInLibrary()); |
|
133 |
} else { |
|
134 |
state.setNull(SOURCE_SYMBOL_IN_LIBRARY); |
|
135 |
} |
|
145 |
@Override |
|
146 |
public void saveToState(PersistentState state) throws PersistenceException { |
|
147 |
if (isLibrarySymbol()) { |
|
148 |
state.set(SOURCE_SYMBOL_IN_LIBRARY, getSourceSymbolInLibrary()); |
|
149 |
} else { |
|
150 |
state.setNull(SOURCE_SYMBOL_IN_LIBRARY); |
|
151 |
} |
|
136 | 152 |
state.set(SOURCE, source); |
137 |
}
|
|
138 |
|
|
153 |
}
|
|
154 |
|
|
139 | 155 |
public static class RegisterPersistence implements Callable { |
140 | 156 |
|
157 |
@Override |
|
141 | 158 |
public Object call() throws Exception { |
142 | 159 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
143 |
if( manager.getDefinition(IMAGE_STYLE_PERSISTENCE_DEFINITION_NAME)==null ) {
|
|
160 |
if (manager.getDefinition(IMAGE_STYLE_PERSISTENCE_DEFINITION_NAME) == null) {
|
|
144 | 161 |
DynStruct definition = manager.addDefinition( |
145 |
ImageStyle.class, |
|
146 |
IMAGE_STYLE_PERSISTENCE_DEFINITION_NAME, |
|
147 |
IMAGE_STYLE_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
|
|
148 |
null,
|
|
149 |
null |
|
162 |
ImageStyle.class,
|
|
163 |
IMAGE_STYLE_PERSISTENCE_DEFINITION_NAME,
|
|
164 |
IMAGE_STYLE_PERSISTENCE_DEFINITION_NAME + " Persistence definition",
|
|
165 |
null,
|
|
166 |
null
|
|
150 | 167 |
); |
151 | 168 |
|
152 | 169 |
// Extend the Style base definition |
... | ... | |
159 | 176 |
} |
160 | 177 |
} |
161 | 178 |
} |
162 |
|
|
163 |
|
Also available in: Unified diff