- Timestamp:
- 2011-12-26T15:13:52+01:00 (13 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/tools
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/ImageProvider.java
r4506 r4712 77 77 78 78 /** 79 * Return an image from the specified location. 79 * Return an image from the specified location. Throws a RuntimeException if 80 * the image cannot be located. 80 81 * 81 82 * @param subdir The position of the directory, e.g. 'layer' … … 87 88 if (icon == null) { 88 89 String ext = name.indexOf('.') != -1 ? "" : ".???"; 89 throw new NullPointerException(tr(90 throw new RuntimeException(tr( 90 91 "Fatal: failed to locate image ''{0}''. This is a serious configuration problem. JOSM will stop working.", 91 92 name+ext)); … … 128 129 public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive, boolean sanitize) { 129 130 return getIfAvailable(dirs, id, subdir, name, archive, null, sanitize); 131 } 132 133 public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive, Dimension dim, boolean sanitize) { 134 return getIfAvailable(dirs, id, subdir, name, archive, dim, null, sanitize); 130 135 } 131 136 … … 144 149 * part of the dimension can be -1. Then it will scale the width 145 150 * in the same way as the height. (And the other way around.) 151 * @param maxSize The maximum size of the image. It will shrink the image if necessary, and 152 * keep the aspect ratio. The given width or height can be -1 which means this 153 * direction is not bounded. 154 * If this parameter has a non-null value, the parameter 'dim' will be ignored. 146 155 * @param sanitize If the image should be repainted to a new BufferedImage to work 147 156 * around certain issues. 148 157 */ 149 public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive, Dimension dim, boolean sanitize) { 158 public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, 159 File archive, Dimension dim, Dimension maxSize, boolean sanitize) { 150 160 ImageResource ir = getIfAvailableImpl(dirs, id, subdir, name, archive); 151 161 if (ir == null) 152 162 return null; 153 return ir.getImageIcon(dim == null ? ImageResource.DEFAULT_DIMENSION : dim, sanitize); 154 } 155 156 private static ImageResource getIfAvailableImpl(Collection<String> dirs, String id, String subdir, String name, File archive) { 163 if (maxSize != null) 164 return ir.getImageIconBounded(maxSize, sanitize); 165 else 166 return ir.getImageIcon(dim == null ? ImageResource.DEFAULT_DIMENSION : dim, sanitize); 167 } 168 169 static ImageResource getIfAvailableImpl(Collection<String> dirs, String id, String subdir, String name, File archive) { 157 170 if (name == null) 158 171 return null; -
trunk/src/org/openstreetmap/josm/tools/ImageRequest.java
r4271 r4712 27 27 protected int width = -1; 28 28 protected int height = -1; 29 protected int maxWidth = -1; 30 protected int maxHeight = -1; 29 31 protected boolean sanitize; 30 32 protected boolean required = true; … … 65 67 } 66 68 69 public ImageRequest setMaxWidth(int maxWidth) { 70 this.maxWidth = maxWidth; 71 return this; 72 } 73 74 public ImageRequest setMaxHeight(int maxHeight) { 75 this.maxHeight = maxHeight; 76 return this; 77 } 78 67 79 public ImageRequest setSanitize(boolean sanitize) { 68 80 this.sanitize = sanitize; … … 76 88 77 89 public ImageIcon get() { 78 ImageIcon icon = ImageProvider.getIfAvailable(dirs, id, subdir, name, archive, new Dimension(width, height), sanitize); 79 if (required && icon == null) { 80 String ext = name.indexOf('.') != -1 ? "" : ".???"; 81 throw new NullPointerException(tr("Fatal: failed to locate image ''{0}''. This is a serious configuration problem. JOSM will stop working.", name + ext)); 90 ImageResource ir = ImageProvider.getIfAvailableImpl(dirs, id, subdir, name, archive); 91 if (ir == null) { 92 if (required) { 93 String ext = name.indexOf('.') != -1 ? "" : ".???"; 94 throw new RuntimeException(tr("Fatal: failed to locate image ''{0}''. This is a serious configuration problem. JOSM will stop working.", name + ext)); 95 } else 96 return null; 82 97 } 83 return icon; 98 if (maxWidth != -1 || maxHeight != -1) 99 return ir.getImageIconBounded(new Dimension(maxWidth, maxHeight), sanitize); 100 else 101 return ir.getImageIcon(new Dimension(width, height), sanitize); 84 102 } 85 103 -
trunk/src/org/openstreetmap/josm/tools/ImageResource.java
r4271 r4712 62 62 */ 63 63 public ImageIcon getImageIcon(Dimension dim, boolean sanitized) { 64 if (dim.width < -1 || dim.width == 0 || dim.height < -1 || dim.height == 0) 65 throw new IllegalArgumentException(); 64 66 ImageWrapper iw = imgCache.get(dim); 65 67 if (iw != null) { … … 94 96 } 95 97 } 98 99 /** 100 * Get image icon with a certain maximum size. The image is scaled down 101 * to fit maximum dimensions. (Keeps aspect ratio) 102 * 103 * @param maxSize The maximum size. One of the dimensions (widht or height) can be -1, 104 * which means it is not bounded. 105 */ 106 public ImageIcon getImageIconBounded(Dimension maxSize, boolean sanitized) { 107 if (maxSize.width < -1 || maxSize.width == 0 || maxSize.height < -1 || maxSize.height == 0) 108 throw new IllegalArgumentException(); 109 float realWidth; 110 float realHeight; 111 if (svg != null) { 112 realWidth = svg.getWidth(); 113 realHeight = svg.getHeight(); 114 } else { 115 ImageWrapper base = imgCache.get(DEFAULT_DIMENSION); 116 if (base == null) throw new AssertionError(); 117 ImageIcon icon = new ImageIcon(base.img); 118 realWidth = icon.getIconWidth(); 119 realHeight = icon.getIconHeight(); 120 } 121 int maxWidth = maxSize.width; 122 int maxHeight = maxSize.height; 123 124 if (realWidth <= maxWidth) { 125 maxWidth = -1; 126 } 127 if (realHeight <= maxHeight) { 128 maxHeight = -1; 129 } 130 131 if (maxWidth == -1 && maxHeight == -1) 132 return getImageIcon(DEFAULT_DIMENSION, sanitized); 133 else if (maxWidth == -1) 134 return getImageIcon(new Dimension(-1, maxHeight), sanitized); 135 else if (maxHeight == -1) 136 return getImageIcon(new Dimension(maxWidth, -1), sanitized); 137 else 138 if (realWidth / maxWidth > realHeight / maxHeight) 139 return getImageIcon(new Dimension(maxWidth, -1), sanitized); 140 else 141 return getImageIcon(new Dimension(-1, maxHeight), sanitized); 142 } 96 143 }
Note:
See TracChangeset
for help on using the changeset viewer.