Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

rotate image java2d  RSS feed

 
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,
 
Marshal
Posts: 59108
180
  • 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,
     
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi iam new in Javaranch
     
    Campbell Ritchie
    Marshal
    Posts: 59108
    180
    • 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
     
    Evil is afoot. But this tiny ad is just an ad:
    Rocket Oven Kickstarter - from the trailboss
    https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!