Enhancement to IconsFactory class

This forum is used by users to request and discuss new product features. Please do not use this forum for technical support including bug reports.

Moderator: JIDE Support

Forum rules
Product suggestions only. Please do not use this forum for technical support including bug reports.

Enhancement to IconsFactory class

Postby flyerfred » Sat Feb 23, 2008 4:26 am

Hello,

An interesting feature would be to propose a new method to create an image by changing orientation of an original image, in order to easlily create rotating icons. Can look like:

public static ImageIcon createOrientedImage(Image image, int orientation)

where orientation is a number of degrees, ranging from 0 to 360.

BR - F.
flyerfred
 
Posts: 210
Joined: Wed Oct 17, 2007 12:40 pm
Location: Vienna

Postby JIDE Support » Sat Feb 23, 2008 11:11 am

I just spent an hour or so to write it and test it. The oringal code I post is not good enough but this new version will support all degree. I also get a chance to refresh my Algebra :-)

Feel free to use it in your application. If you make any change, please let me know so that I can include it in the next release.

Code: Select all
   
    final static double DEGREE_90 = 90.0 * Math.PI / 180.0;

    /**
     * Creates a rotated version of the input image.
     *
     * @param c            The component to get properties useful for painting, e.g. the foreground
     *                     or background color.
     * @param icon         the image to be rotated.
     * @param rotatedAngle the rotated angle, in degree, clockwise. It could be any double but we
     *                     will mod it with 360 before using it.
     *
     * @return the image after rotating.
     */
    public static ImageIcon createRotatedImage(Component c, Icon icon, double rotatedAngle) {
        // convert rotatedAngle to a value from 0 to 360
        double originalAngle = rotatedAngle % 360;
        if (rotatedAngle != 0 && originalAngle == 0) {
            originalAngle = 360.0;
        }

        // convert originalAngle to a value from 0 to 90
        double angle = originalAngle % 90;
        if (originalAngle != 0.0 && angle == 0.0) {
            angle = 90.0;
        }

        double radian = Math.toRadians(angle);

        int iw = icon.getIconWidth();
        int ih = icon.getIconHeight();
        int w;
        int h;

        if ((originalAngle >= 0 && originalAngle <= 90) || (originalAngle > 180 && originalAngle <= 270)) {
            w = (int) (iw * Math.sin(DEGREE_90 - radian) + ih * Math.sin(radian));
            h = (int) (iw * Math.sin(radian) + ih * Math.sin(DEGREE_90 - radian));
        }
        else {
            w = (int) (ih * Math.sin(DEGREE_90 - radian) + iw * Math.sin(radian));
            h = (int) (ih * Math.sin(radian) + iw * Math.sin(DEGREE_90 - radian));
        }
        BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        Graphics g = image.getGraphics();
        Graphics2D g2d = (Graphics2D) g.create();

        // calculate the center of the icon.
        int cx = iw / 2;
        int cy = ih / 2;

        // move the graphics center point to the center of the icon.
        g2d.translate(w/2, h/2);

        // rotate the graphcis about the center point of the icon
        g2d.rotate(Math.toRadians(originalAngle));

        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        icon.paintIcon(c, g2d, -cx, -cy);

        g2d.dispose();
        return new ImageIcon(image);
    }


This is the final version after testing. Much more complex than I thought :-(
Last edited by JIDE Support on Fri Feb 29, 2008 11:13 am, edited 1 time in total.
JIDE Software Technical Support Team
JIDE Support
Site Admin
 
Posts: 37219
Joined: Sun Sep 14, 2003 10:49 am

Postby flyerfred » Sat Feb 23, 2008 2:42 pm

:D works fine! Thanks!

BR - F.
flyerfred
 
Posts: 210
Joined: Wed Oct 17, 2007 12:40 pm
Location: Vienna


Return to Product Suggestions

Who is online

Users browsing this forum: No registered users and 9 guests

cron