File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes rotate image java2d Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "rotate image java2d" Watch "rotate image java2d" New topic
Author

rotate image java2d

Brandon Broschinsky
Ranch Hand

Joined: Aug 21, 2004
Posts: 41
I am working on a project where I need to rotate multiple images. I have no problem rotating the image multiple ways, but with affine transform the corners get cut off when rotating at weird angles, and if enlarge the size of the buffered image to remedy this, then the image size gets larger, although some of it is nonvisible space. does anyone know of any way to rotate an image with more control, i.e. getting down to the pixel level, and would this method allow me more control over the image rotation and painting?

thanks,
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38793
    
  23
How far have you got with your AffineTransform.
I haven't used it for some time, but as far as I remember, you can
  • rotate the Graphics2D object,
  • OR rotate the AffineTransform,
  • OR setToRotation on the AffineTransform.
  • As far as I remember, setToRotation() sets only one Transform, so any other transforms are lost.


    You can of course use the matrix formulae, which you will find on the API.

    CR
    Brandon Broschinsky
    Ranch Hand

    Joined: Aug 21, 2004
    Posts: 41
    I have tried the first two methods, rotating the graphics object, and the rotating the transform. I just tried the setToRotation and it yielded the same results as rotating the transform. Basically the image is rotating but it draws the rotated image over the original in the same frame, if that makes sense?

    This is how I am doing it.

    private BufferedImage rotateImage(BufferedImage bi){
    Graphics2D g2d = bi.createGraphics();
    AffineTransform at = new AffineTransform();
    at.setToRotation(-Math.PI/15.0);
    g2d.drawImage(bi,at,this);
    g2d.transform(at);

    g2d.dispose();
    return bi;
    }

    I am passing in an image I have already drawn, and updating and repainting it.

    thanks,
    Nabi-B ux
    Greenhorn

    Joined: May 27, 2006
    Posts: 2
    hi iam new in Javaranch
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38793
        
      23
    Does that work? That is the real question. "The proof of the pudding is in the eating."


    If not, try passing the Graphics (or graphics2D) object from your paintComponent() method to your rotateImage() method. Or clone the Graphics2D object and pass it to the rotateImage() method.

    If everything else works, and you are getting your image appearing twice: I presume you have super.paintComponent(); as the first line of your paintComponent() method? Try transforming the g2d object before you draw the image.

    CR
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: rotate image java2d