Ignore:
Timestamp:
2009-01-18T17:27:32+01:00 (15 years ago)
Author:
ulfl
Message:

turn restriction display in mappaint (very experimental)

will only work with standard restrictions, selection display still strange, ...

but will display icons rotated according to the direction of the from way :-)))

enable this with mappaint.restriction=true (default disabled)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r1169 r1295  
    22package org.openstreetmap.josm.tools;
    33
     4import java.awt.Component;
    45import java.awt.Cursor;
    56import java.awt.Graphics;
     7import java.awt.Graphics2D;
    68import java.awt.GraphicsConfiguration;
    79import java.awt.GraphicsEnvironment;
    810import java.awt.Image;
    911import java.awt.Point;
     12import java.awt.RenderingHints;
    1013import java.awt.Toolkit;
    1114import java.awt.Transparency;
     
    224227        }
    225228    }
     229
     230/* from: http://www.jidesoft.com/blog/2008/02/29/rotate-an-icon-in-java/
     231* License: "feel free to use"
     232*/
     233final 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    }
    226294}
Note: See TracChangeset for help on using the changeset viewer.