Results 1 to 6 of 6
Thread: Returning a rotated image
- 10-11-2011, 09:58 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 6
- Rep Power
- 0
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...
I've been checking out tutorials on it but I still can't get it.Java Code:private Image rotateImage(Image img){ AffineTransform rotate = new AffineTransform(); return img; }
- 10-11-2011, 11:10 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
Re: Returning a rotated image
See the following links. Pseudo-code: create a BufferedImage, get its graphics, apply the transform, draw the image, destroy graphics, return BufferedImage
Transforming Shapes, Text, and Images (The Java™ Tutorials > 2D Graphics > Advanced Topics in Java2D)
Creating and Drawing to an Image (The Java™ Tutorials > 2D Graphics > Working with Images)
Creating and Drawing to an Image (The Java™ Tutorials > 2D Graphics > Working with Images)
- 10-11-2011, 11:11 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 83
- Rep Power
- 0
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.
- 10-12-2011, 12:03 AM #4
Member
- Join Date
- Jul 2011
- Posts
- 6
- Rep Power
- 0
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; }
- 10-13-2011, 03:23 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 83
- Rep Power
- 0
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.
- 10-15-2011, 08:40 PM #6
Member
- Join Date
- Jul 2011
- Posts
- 6
- Rep Power
- 0
Re: Returning a rotated image
I'm starting to understand it now, I was mainly stumped by how the things like drawing worked.
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.Java Code:Graphics2D g2d = image.createGraphics();
It just tells the 'pen' (graphics) to draw on this 'canvas' (image).
Thanks for the help :)
Similar Threads
-
Rotated Text and Antialiasing on Windows
By neptune692 in forum Java 2DReplies: 4Last Post: 05-05-2011, 08:41 PM -
help returning a min value
By Meta in forum New To JavaReplies: 7Last Post: 04-11-2011, 12:28 AM -
Help with returning
By Da1dmoney in forum EclipseReplies: 3Last Post: 12-15-2010, 02:38 AM -
Rotated Shape Object Line weight is not retaining properly in printing
By dorairaj in forum AWT / SwingReplies: 7Last Post: 10-06-2009, 05:58 AM -
Converting multiple banded image into single banded image... Image enhancement
By archanajathan in forum Advanced JavaReplies: 0Last Post: 01-08-2008, 05:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks