Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Basic image rotation method

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Thanked 0 Times in 0 Posts

    Default Basic image rotation method

    Hey everyone, i am new here and to java. I am currently just starting to learn java and have a problem trying to figure out a simple method. I am writing methods to rotate images, i can do left and right, but cannot make it rotate 180 degree's. I thought it would be as easy as copying and pasting a few lines or multiplying some of the ints from one of my other methods(bottom method is from my rotate right method), any advice or suggestions?

      public void copyPictureLeftRotation(Picture sourcePic)
      Picture targetPicture = this;
      Pixel sourcePixel = null;
      Pixel targetPixel = null;
      int targetX, targetY = 0;
      int width = sourcePic.getWidth();
      int height = sourcePic.getHeight();
    // loop through the rows
      for (int sourceX = 0; sourceX < width; sourceX++)
    // loop through the colomns
        for (int sourceY = 0; sourceY < height; sourceY++)
    // set the target pixel color to the source pixel color
          sourcePixel = sourcePic.getPixel(sourceX,sourceY);
          targetX = sourceY;
          targetY = width - 1 - sourceX;
          targetPixel = targetPicture.getPixel(targetX,targetY);
    Last edited by helloworld922; November 15th, 2010 at 02:09 AM.

  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Madgaon, Goa, India
    Thanked 48 Times in 46 Posts

    Default Re: Basic image rotation method

    Your code uses Picture and Pixel classes which are not part of the standard JDK and about which we know nothing except the names. If you wrote those classes, post the code here. If you got them from somewhere else, that's the place to ask for help.


  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Thanked 619 Times in 561 Posts
    Blog Entries

    Default Re: Basic image rotation method

    You can easily rotate a picture using an affine transformation (built into the standard JDK). Note that you'll have to take into account the fact that rotating the image at a non-right angle multiple will need a larger resultant image to fit the axis-aligned bounding box.

    // angle is specified in radians, angle is measured from positive x axis
    // I'm pretty sure this will work, I didn't actually try it
    public BufferedImage rotateImage(BufferedImage original, double angle)
        BufferedImage result = new BufferedImage((int)(original.getWidth() * Math.abs(Math.cos(angle))), (int)(original.getHeight() * Math.abs(Math.sin(angle))));
        Graphics2D g = result.createGraphics();
        AffineTransform transform = new AffineTransform();
        g.drawImage(original, transform, null);
        return result;

Similar Threads

  1. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM
  2. Help, Want to make a BASIC min value Method with Generics
    By Iglesias in forum Collections and Generics
    Replies: 6
    Last Post: September 12th, 2010, 10:16 PM
  3. Pixel Alteration in an image/image rotation
    By bguy in forum Java Theory & Questions
    Replies: 3
    Last Post: November 1st, 2009, 10:50 AM
  4. Some basic questions.
    By trips in forum Java Theory & Questions
    Replies: 5
    Last Post: July 21st, 2009, 02:15 AM
  5. Problem with sprite rotation in graphics
    By Katotetu in forum Algorithms & Recursion
    Replies: 0
    Last Post: May 8th, 2009, 07:27 PM