Changeset 8903 in josm for trunk/src/org/openstreetmap/josm/tools
- Timestamp:
- 2015-10-18T23:57:31+02:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/ImageProvider.java
r8846 r8903 12 12 import java.awt.Image; 13 13 import java.awt.Point; 14 import java.awt.Rectangle; 14 15 import java.awt.RenderingHints; 15 16 import java.awt.Toolkit; … … 56 57 57 58 import org.openstreetmap.josm.Main; 59 import org.openstreetmap.josm.data.osm.OsmPrimitive; 58 60 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 61 import org.openstreetmap.josm.gui.mappaint.ElemStyle; 62 import org.openstreetmap.josm.gui.mappaint.MapImage; 63 import org.openstreetmap.josm.gui.mappaint.MapPaintStyles; 64 import org.openstreetmap.josm.gui.mappaint.NodeElemStyle; 65 import org.openstreetmap.josm.gui.mappaint.Range; 66 import org.openstreetmap.josm.gui.mappaint.StyleCache.StyleList; 59 67 import org.openstreetmap.josm.io.CachedFile; 60 68 import org.openstreetmap.josm.plugins.PluginHandler; … … 1266 1274 CheckParameterUtil.ensureParameterNotNull(type, "type"); 1267 1275 return get("data", type.getAPIName()); 1276 } 1277 1278 /** 1279 * @param primitive Object for which an icon shall be fetched. The icon is chosen based on tags. 1280 * @param iconSize Target size of icon. Icon is padded if required. 1281 * @return Icon for {@code primitive} that fits in cell. 1282 * @since 8903 1283 */ 1284 public static ImageIcon getPadded(OsmPrimitive primitive, Rectangle iconSize) { 1285 // Check if the current styles have special icon for tagged nodes. 1286 if (primitive instanceof org.openstreetmap.josm.data.osm.Node) { 1287 Pair<StyleList, Range> nodeStyles = MapPaintStyles.getStyles().generateStyles(primitive, 100, false); 1288 for (ElemStyle style : nodeStyles.a) { 1289 if (style instanceof NodeElemStyle) { 1290 NodeElemStyle nodeStyle = (NodeElemStyle) style; 1291 MapImage icon = nodeStyle.mapImage; 1292 if (icon != null) { 1293 int backgroundWidth = iconSize.height; 1294 int backgroundHeight = iconSize.height; 1295 int iconWidth = icon.getWidth(); 1296 int iconHeight = icon.getHeight(); 1297 BufferedImage image = new BufferedImage(backgroundWidth, backgroundHeight, 1298 BufferedImage.TYPE_INT_ARGB); 1299 double scaleFactor = Math.min(backgroundWidth / (double) iconWidth, backgroundHeight 1300 / (double) iconHeight); 1301 BufferedImage iconImage = icon.getImage(false); 1302 Image scaledIcon; 1303 final int scaledWidth; 1304 final int scaledHeight; 1305 if (scaleFactor < 1) { 1306 // Scale icon such that it fits on background. 1307 scaledWidth = (int) (iconWidth * scaleFactor); 1308 scaledHeight = (int) (iconHeight * scaleFactor); 1309 scaledIcon = iconImage.getScaledInstance(scaledWidth, scaledHeight, Image.SCALE_SMOOTH); 1310 } else { 1311 // Use original size, don't upscale. 1312 scaledWidth = iconWidth; 1313 scaledHeight = iconHeight; 1314 scaledIcon = iconImage; 1315 } 1316 image.getGraphics().drawImage(scaledIcon, (backgroundWidth - scaledWidth) / 2, 1317 (backgroundHeight - scaledHeight) / 2, null); 1318 1319 return new ImageIcon(image); 1320 } 1321 } 1322 } 1323 } 1324 1325 // Use generic default icon. 1326 return ImageProvider.get(primitive.getDisplayType()); 1268 1327 } 1269 1328
Note:
See TracChangeset
for help on using the changeset viewer.