Results 1 to 6 of 6
  1. #1
    Hazzy is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default Returning a rotated image

    I'm trying to make a method which returns an image to a rotated value and I just can't seem to figure it out.
    Sadly this is as far as I've gotten...
    Java Code:
    	private Image rotateImage(Image img){
    		AffineTransform rotate = new AffineTransform();
    		
    		return img;
    	}
    I've been checking out tutorials on it but I still can't get it.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

  3. #3
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: Returning a rotated image

    Firstly, I think this thread would be more appropriate here (though I'm new here so I'm not entirely sure).

    But anyway, one thing you must change is that you should use BufferedImage in place of Image (the former is a subclass of the latter).

    An object of type AffineTransform represents a transformation (such as rotation) on an image. The first thing you must do is set up this transformation which is done by writing "rotate.rotate(angle,x,y);" where angle is the angle you want to rotate it by (in radians) and (x,y) is the point you want to rotate it about.

    Next, create an object of type AffineTransformOp (let's call it op) sending your AffineTransform to the constructor along with AffineTransformOp.TYPE_[type] where type is either NEAREST_NEIGHBOR, BILINEAR, or BICUBIC (nearest neighbor giving the fastest rotation, bicubic giving the highest quality, and bilinear being somewhere in between).

    Now the last part, I'm not entirely sure, but I think if you just type return op.filter(img,null); that will return the correct image.

  4. #4
    Hazzy is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default Re: Returning a rotated image

    Uhmm k... From looking at those tutorials (again) I've managed to change it to get an image into the buffer and return it. Successfully works so far; still no idea how I get the rotation to work however.

    No matter how many times I look over the tutorials I just don't understand it.

    Java Code:
    	private Image rotateImage(){
    		BufferedImage bImg = null;
    		try {
    			bImg = ImageIO.read(this.getClass().getResource("/res/ship.png"));
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		AffineTransform rotate = new AffineTransform();		
    		rotate.rotate(Math.toRadians(180),8,8);
    		
    		return bImg;
    	}

  5. #5
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: Returning a rotated image

    You aren't doing anything to the image itself. You're setting up an AffineTransform correctly, but you're never actually using it. First you must create an object of type AffineTransformOp as I mentioned in my previous post. Then use the filter() method of said AffineTransformOp to actually perform the rotation.

  6. #6
    Hazzy is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default Re: Returning a rotated image

    I'm starting to understand it now, I was mainly stumped by how the things like drawing worked.
    Java Code:
    Graphics2D g2d = image.createGraphics();
    Now I'm aware that this doesn't create a separate image and I don't need to return anything - Which I was trying to figure out for a while.
    It just tells the 'pen' (graphics) to draw on this 'canvas' (image).
    Thanks for the help :)

Similar Threads

  1. Rotated Text and Antialiasing on Windows
    By neptune692 in forum Java 2D
    Replies: 4
    Last Post: 05-05-2011, 08:41 PM
  2. help returning a min value
    By Meta in forum New To Java
    Replies: 7
    Last Post: 04-11-2011, 12:28 AM
  3. Help with returning
    By Da1dmoney in forum Eclipse
    Replies: 3
    Last Post: 12-15-2010, 02:38 AM
  4. Replies: 7
    Last Post: 10-06-2009, 05:58 AM
  5. Replies: 0
    Last Post: 01-08-2008, 05:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •