Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

thuned

[java] how to rotate an image?

This topic is 6325 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

how can you rotate an image? for example, i load a gif image and display it. what do i need to import and what functions can i use to rotate the image. thuned life is unfair, take advantage of it. UNMB2 - if the link doesn't work, try clicking it Edited by - thuned on February 18, 2001 2:14:32 AM

Share this post


Link to post
Share on other sites
Advertisement
anyone? too be more specific, i want it rotated around the zaxis
so if i rotate it 180 degrees it''s upsidedown instead of a "line"

life is unfair, take advantage of it.
UNMB2 - if the link doesn''t work, try clicking it

Share this post


Link to post
Share on other sites
There''s probably some Java2D functions you can use, but if you''re wanting to do it the old fashioned way, here''s some really old code I''ve dug out - can''t remember where I originally appropriated it from though. Dunno if it will do what you want but it might be helpful...?

Basically convert your image to pixels (use MemoryImageSource/PixelGrabber/whatever), then call this function:


public final byte[] rotate ( byte iPixels[], int degree, int w, int h, boolean imageClear ) {
if (iPixels.length < 1)
return null;

byte p[] = new byte[iPixels.length];

//
//the following is to avoid repeated calculations
//
double theta = degree * RADIANS_CALC; //rotation
double tx = w / 2.0; //translation (to rotate about center)
double ty = h / 2.0;
double cos_theta = Math.cos(theta);
double sin_theta = Math.sin(theta);
double dx = tx * (1.0 - cos_theta) + ty * sin_theta;
double dy = ty * (1.0 - cos_theta) - tx * sin_theta;

int i = 0;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int xp = (int)((cos_theta * x - sin_theta * y + dx) + 0.5);
int yp = (int)((sin_theta * x + cos_theta * y + dy) + 0.5);

if (imageClear) {
if (yp < 0)
yp = 0;
else if (yp >= h)
yp = h-1;

if (xp < 0)
xp = 0;
else if (xp >= w)
xp = w-1;
}

if (yp >= 0 && yp < h && xp >= 0 && xp < w) {
int dst = yp * w + xp;
p = iPixels[dst];
}

i++;
}
}

return p;
}



Convert the returned array back into an image.

Jason

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!