Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-12-2008, 06:11 AM
Member
 
Join Date: Aug 2008
Posts: 2
Rep Power: 0
ChipChamp is on a distinguished road
Default 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!
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 08-14-2008, 12:31 AM
Member
 
Join Date: Aug 2008
Posts: 2
Rep Power: 0
ChipChamp is on a distinguished road
Default
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:

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);		
	}
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-14-2008, 02:17 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,389
Rep Power: 3
hardwired is on a distinguished road
Default
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:
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 setTransform 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 may not be used after
// your are finsihed with it. Restoring the original transform
// with setTransform is usually necessary in Swing drawing,
// preferably done in the paintComponent method, in which
// the graphics context is later passed to the paintChildren and
// paintBorder methods (see JComponent api Method Detail
// for the paint 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 setTransform calls and use
// the g2.rotate/translate/etc. – type methods to
// transform/draw/reverse_transform the graphics context as
// shown above.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how u rotate the arrow mark as the line moves accordingly sandhyau AWT / Swing 1 02-16-2008 11:22 PM
image removing Triss New To Java 3 01-20-2008 08:27 PM
Converting multiple banded image into single banded image... Image enhancement archanajathan Advanced Java 0 01-08-2008 05:29 PM
Image resizing alley Java 2D 2 11-13-2007 10:10 AM
how to set an image size valery New To Java 1 08-06-2007 08:27 PM


All times are GMT +2. The time now is 11:13 PM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org