Changeset 1295 in josm for trunk/src/org/openstreetmap/josm/tools
- Timestamp:
- 2009-01-18T17:27:32+01:00 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/ImageProvider.java
r1169 r1295 2 2 package org.openstreetmap.josm.tools; 3 3 4 import java.awt.Component; 4 5 import java.awt.Cursor; 5 6 import java.awt.Graphics; 7 import java.awt.Graphics2D; 6 8 import java.awt.GraphicsConfiguration; 7 9 import java.awt.GraphicsEnvironment; 8 10 import java.awt.Image; 9 11 import java.awt.Point; 12 import java.awt.RenderingHints; 10 13 import java.awt.Toolkit; 11 14 import java.awt.Transparency; … … 224 227 } 225 228 } 229 230 /* from: http://www.jidesoft.com/blog/2008/02/29/rotate-an-icon-in-java/ 231 * License: "feel free to use" 232 */ 233 final static double DEGREE_90 = 90.0 * Math.PI / 180.0; 234 235 /** 236 * Creates a rotated version of the input image. 237 * 238 * @param c The component to get properties useful for painting, e.g. the foreground 239 * or background color. 240 * @param icon the image to be rotated. 241 * @param rotatedAngle the rotated angle, in degree, clockwise. It could be any double but we 242 * will mod it with 360 before using it. 243 * 244 * @return the image after rotating. 245 */ 246 public static ImageIcon createRotatedImage(Component c, Icon icon, double rotatedAngle) { 247 // convert rotatedAngle to a value from 0 to 360 248 double originalAngle = rotatedAngle % 360; 249 if (rotatedAngle != 0 && originalAngle == 0) { 250 originalAngle = 360.0; 251 } 252 253 // convert originalAngle to a value from 0 to 90 254 double angle = originalAngle % 90; 255 if (originalAngle != 0.0 && angle == 0.0) { 256 angle = 90.0; 257 } 258 259 double radian = Math.toRadians(angle); 260 261 int iw = icon.getIconWidth(); 262 int ih = icon.getIconHeight(); 263 int w; 264 int h; 265 266 if ((originalAngle >= 0 && originalAngle <= 90) || (originalAngle > 180 && originalAngle <= 270)) { 267 w = (int) (iw * Math.sin(DEGREE_90 - radian) + ih * Math.sin(radian)); 268 h = (int) (iw * Math.sin(radian) + ih * Math.sin(DEGREE_90 - radian)); 269 } 270 else { 271 w = (int) (ih * Math.sin(DEGREE_90 - radian) + iw * Math.sin(radian)); 272 h = (int) (ih * Math.sin(radian) + iw * Math.sin(DEGREE_90 - radian)); 273 } 274 BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); 275 Graphics g = image.getGraphics(); 276 Graphics2D g2d = (Graphics2D) g.create(); 277 278 // calculate the center of the icon. 279 int cx = iw / 2; 280 int cy = ih / 2; 281 282 // move the graphics center point to the center of the icon. 283 g2d.translate(w/2, h/2); 284 285 // rotate the graphics about the center point of the icon 286 g2d.rotate(Math.toRadians(originalAngle)); 287 288 g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); 289 icon.paintIcon(c, g2d, -cx, -cy); 290 291 g2d.dispose(); 292 return new ImageIcon(image); 293 } 226 294 }
Note:
See TracChangeset
for help on using the changeset viewer.
