Results 1 to 5 of 5
Thread: How to rotate an image
- 08-12-2008, 06:11 AM #1
Member
- Join Date
- Aug 2008
- Posts
- 2
- Rep Power
- 0
How to rotate an image
Hey, I'm fairly new to Java, and am attempting to make a game kind of like Asteroids... The problem that I'm having is that I can't figure out how to rotate my ships image... Is there any kind of method to do that, or ... something? :P
(I don't really want to make multiple images of the ship in different rotations)
Thanks!
- 08-14-2008, 12:31 AM #2
Member
- Join Date
- Aug 2008
- Posts
- 2
- Rep Power
- 0
Okay, so I have figured out how to get the image to rotate, but now I have a problem where sometimes when the player is going around (usually near the sides of the applet) the player kinda... stretches out or something. Here are a couple pictures:
Good:
img181.imageshack.us/img181/9941/pic1bo5.png
Bad:
img364.imageshack.us/img364/3410/pic2dm5.png
(apparently it's not letting me post images or something, so you have to copy the URLs...)
It usually goes away after a second or less... But does anyone know what's causing this? Here is my update method, for double buffering, and my paint method:
Java Code:public void update (Graphics g) { if (dbImage == null) { dbImage = createImage (this.getSize().width, this.getSize().height); dbg = dbImage.getGraphics (); } dbg.setColor (getBackground ()); dbg.fillRect (0, 0, this.getSize().width, this.getSize().height); dbg.setColor (getForeground()); paint (dbg); g.drawImage (dbImage, 0, 0, this); } public void paint(Graphics g) { // clear off screen bitmap super.paint(g); // cast graphics to graphics 2D Graphics2D g2 = (Graphics2D) g; AffineTransform tfm = new AffineTransform(); tfm.rotate(0,0,0); g2.setTransform(tfm); g2.drawImage(backImage, 0, 0, this); tfm.rotate(Math.toRadians(player.angle+90), player.x+32, player.y+32); g2.setTransform(tfm); g2.drawImage(tank, player.x, player.y, this); }
- 08-14-2008, 02:17 AM #3
does anyone know what's causing this?
I don't have enough to run/test to replicate your report or
offer a fix for your code.
One possible trouble area does stand out:
Java Code:AffineTransform tfm = new AffineTransform(); // There is no need to do this: tfm.rotate(0,0,0); g2.setTransform(tfm); // unless you are planning to rotate the background image. // In such a case you do not use the [i]setTransform[/i] method. // As mentioned in the Method Detail for this method you use // this only one time to restore the origingal transform after // you are finished with all transform operations on the // graphics context (g). However, this may not be necessary in // AWT drawing since the graphics context [u]may[/u] not be used after // your are finsihed with it. Restoring the original transform // with [i]setTransform[/i] is usually necessary in Swing drawing, // preferably done in the [i]paintComponent[/i] method, in which // the graphics context is later passed to the [i]paintChildren[/i] and // [i]paintBorder[/i] methods (see JComponent api Method Detail // for the [i]paint[/i] method). // The recommended way to do this then would be: // Save the original transform for later restoration: AffineTransform origXform = g2.getTransform(); // apply a rotation transform to the graphics context: g2.rotate(theta, x, y); // draw the background image. g2.drawImage(backImage, 0, 0, this); // Reverse the last transform: g2.rotate(-theta, x, y); // Apply rotation transform to graphics context for player image: g2.rotate(Math.toRadians(player.angle+90), player.x+32, player.y+32); //g2.setTransform(tfm); g2.drawImage(tank, player.x, player.y, this); // and then after you are done with all transform ops, reset // the transform to the original (use this method only one time): g2.setTransform(origXform); // or you could reverse the rotation transform applied above // for the player image. You want the transform at the end of // this method to be the same as it was at the beginning. // Of course, this may be unnecessary, as mentioned above. // If so, you could omit the [i]setTransform[/i] calls and use // the g2.[i]rotate/translate/etc.[/i] – type methods to // transform/draw/reverse_transform the graphics context as // shown above.
- 06-20-2012, 05:24 PM #4
Member
- Join Date
- Jun 2012
- Posts
- 1
- Rep Power
- 0
Re: How to rotate an image
The following function will rotate a buffered image that comes in indeterminate if it is a perfect square or not.
Java Code:public BufferedImage rotate(BufferedImage image) { /* * Affline transform only works with perfect squares. The following * code is used to take any rectangle image and rotate it correctly. * To do this it chooses a center point that is half the greater * length and tricks the library to think the image is a perfect * square, then it does the rotation and tells the library where * to find the correct top left point. The special cases in each * orientation happen when the extra image that doesn't exist is * either on the left or on top of the image being rotated. In * both cases the point is adjusted by the difference in the * longer side and the shorter side to get the point at the * correct top left corner of the image. NOTE: the x and y * axes also rotate with the image so where width > height * the adjustments always happen on the y axis and where * the height > width the adjustments happen on the x axis. * */ AffineTransform xform = new AffineTransform(); if (image.getWidth() > image.getHeight()) { xform.setToTranslation(0.5 * image.getWidth(), 0.5 * image.getWidth()); xform.rotate(_theta); int diff = image.getWidth() - image.getHeight(); switch (_thetaInDegrees) { case 90: xform.translate(-0.5 * image.getWidth(), -0.5 * image.getWidth() + diff); break; case 180: xform.translate(-0.5 * image.getWidth(), -0.5 * image.getWidth() + diff); break; default: xform.translate(-0.5 * image.getWidth(), -0.5 * image.getWidth()); break; } } else if (image.getHeight() > image.getWidth()) { xform.setToTranslation(0.5 * image.getHeight(), 0.5 * image.getHeight()); xform.rotate(_theta); int diff = image.getHeight() - image.getWidth(); switch (_thetaInDegrees) { case 180: xform.translate(-0.5 * image.getHeight() + diff, -0.5 * image.getHeight()); break; case 270: xform.translate(-0.5 * image.getHeight() + diff, -0.5 * image.getHeight()); break; default: xform.translate(-0.5 * image.getHeight(), -0.5 * image.getHeight()); break; } } else { xform.setToTranslation(0.5 * image.getWidth(), 0.5 * image.getHeight()); xform.rotate(_theta); xform.translate(-0.5 * image.getHeight(), -0.5 * image.getWidth()); } AffineTransformOp op = new AffineTransformOp(xform, AffineTransformOp.TYPE_BILINEAR); return op.filter(image, null); }
- 06-20-2012, 07:22 PM #5
Similar Threads
-
how u rotate the arrow mark as the line moves accordingly
By sandhyau in forum AWT / SwingReplies: 1Last Post: 02-16-2008, 11:22 PM -
image removing
By Triss in forum New To JavaReplies: 3Last Post: 01-20-2008, 08:27 PM -
Converting multiple banded image into single banded image... Image enhancement
By archanajathan in forum Advanced JavaReplies: 0Last Post: 01-08-2008, 05:29 PM -
Image resizing
By alley in forum Java 2DReplies: 2Last Post: 11-13-2007, 10:10 AM -
how to set an image size
By valery in forum New To JavaReplies: 1Last Post: 08-06-2007, 08:27 PM


LinkBack URL
About LinkBacks

Bookmarks