Copying (for the purpose of pre processed rotation) BufferedImages

Ok, quick question; I want to rotate 2D images for my game. As you probably know doing this real time is slow. So in order to overcome that problem, I want to take ImageA, rotate it, and ‘copy’ it to ImageB. There will be an array of ‘ImageBs’ wich will contain an ImageA rotated by x degrees. Im am doing this in JAVA and I just don’t know what to do; here is my code, I have successfully ‘copied’ ImageA to ImageB, but when I rotate and then copy… well the screen blinks from black to grey and then turns grey… I don’t know what is the matter with what I am doing (Oh and this is a small test file, my actual game has over a dozen files; ScreenManager is basically a class that gives me a graphical context to the screen and handles some backbuffering for me.


import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.*;
import javax.swing.*;
import java.awt.geom.AffineTransform;

public class hard
{

public static void main(String [] args)
{
AffineTransform rTrans = new AffineTransform();
rTrans.rotate(90);
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_4BYTE_ABGR );/*possible problem here, I just don't know what type of image my .png is*/
Image im = loadImage("Images/38.png");
Graphics2D g2 = image.createGraphics();
System.out.println(im);

g2.drawImage(im,rTrans,null);//This doesn't WORK
/*If I replace the previous line with:
g2.drawImage(im, 0,0,null); it works just fine, and I have thought
about it 'rotating off the screen' but nope, and it isn't a null
value either */
ScreenManager screen = new ScreenManager();
while(true){
Graphics2D g = screen.getGraphics();
screen.cls();
System.out.println(image);
g.drawImage(image, 100,100, null);
g.dispose();
screen.update();
//System.exit(1);
}
}




protected static Image loadImage(String fileName)


{
return new ImageIcon(fileName).getImage();
}
}

I do something similar to what you’re trying to do in a game I wrote.

Here’s my rotation method (it returns a new BufferedImage for whatever rotation it is at):

public BufferedImage rotateImg(double theta)
    {
        BufferedImage ret = new BufferedImage(base.getWidth(), base.getHeight(), BufferedImage.TYPE_INT_ARGB_PRE);
        
        Graphics2D bg = ret.createGraphics(); //Get the graphics
        
        AffineTransform s1 = AffineTransform.getRotateInstance(theta, ret.getWidth() / 2.0, ret.getHeight() / 2.0); //Get a transform to rotate the image
        
        bg.drawImage(base, s1, null); //Draw the image
        
        return ret;
    }

base is the image already loaded, theta is the amount to rotate the image (in radians, not degrees).

Looking between my code and your code, it seems that you aren’t centering the origin you rotate about. So it’s rotating about the point (0, 0) when you want it to rotate about the poitn (50, 50). Effectively it rotates however much 90 radians is, and that probably isn’t in the range of your image.

And about the image type, the types don’t have to be the same, the image data will be converted to the appropriate type. It’s just faster if they’re the same.

Hope this solves your dilemma ;D

Crazyc94, thanks alot; it works perfectly now. I never would have figured that beast out on my own :smiley:

Took me 3 weeks to figure that out on my own ::slight_smile:

Anyways, good luck with your you project :slight_smile:

three weeks ? :’(