Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

rotate image java2d

 
Brandon Broschinsky
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49396
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 41
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi iam new in Javaranch
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 49396
    62
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic