0

I am making a game in Java. I made a planet seen from outer space and I want to make it appear like the planet is slowly rotating. But I don't know how to rotate a image. I need a simple command that rotates my image 1 degree around its own center once. Any help?


This is what I want to do: Image

  • *"Simple image rotation"* By 'simple' DYM spoon feed the answer to you? [What have you tried?](http://www.whathaveyoutried.com/) – Andrew Thompson Jun 22 '12 at 09:29
  • 1
    whats your frontend? html/html5/android/2dgraphics? – Kshitij Jun 22 '12 at 09:30
  • Use an `AffineTransform` (presuming Java 2D). Here is an example of [animated rotation](http://stackoverflow.com/a/11117371/418556). It uses shapes and areas, but the principle is the same. – Andrew Thompson Jun 22 '12 at 09:33
  • I looked at your example but it seems that this will rotate the entire image. I don't want my background to rotate only the planet. As seen http://imageshack.us/photo/my-images/266/examplea.png/ – Thobias Nordgaard Jun 22 '12 at 09:46

2 Answers2

1

Take a look at these tutorials:

ArtemStorozhuk
  • 8,715
  • 4
  • 35
  • 53
0

What you are describing is not rotating an image, but changing an image to represent a 3D rotation of the object in the image.

Ideally you wouldn't be working with this as an image but rather as a 3D object with a different camera angle. Then you would simply rotate the camera around the object and display the resulting image to the user.

However if you're set on doing this as an image, then you need to create a different images representing various states of rotation of your planet and have a separate thread that would replace the displayed image with the next one in sequence, at repeated intervals. Search the web for "java image animation" - there are plenty of tutorials on how to do this.

If you want to rotate an image in 2d space, you can use something like this:

Image image = ...
Graphics2D g2d = ...; // 
g2d.translate(170, 0); // If needed
g2d.rotate(1);  // Rotate the image by 1 radian
//or g2d.rotate(180.0/3.14); to rotate by 1 degree
g2d.drawImage(image, 0, 0, 200, 200, observer);
Aleks G
  • 56,435
  • 29
  • 168
  • 265