I have a program, and I would like to rotate the image (4 directions)

I am wondering if there is an easy way to rotate the image. here is the code, and where I would like to rotate the image.

import java.awt.*;
import java.awt.image.ImageObserver;
import java.io.*;

import javax.imageio.ImageIO;

public class Player {

	int x = 50;
	int y = 50;
	boolean up = false, down = false, left = false, right = false;
	Image playerImage;
	Image playerDrawn;
	Rectangle playerRect = new Rectangle (x, y, 32, 32);
	
	Player (int playerNumber){
		try {
			playerImage = ImageIO.read(new File ("Images/soldier" + playerNumber + ".png"));
			playerDrawn = playerImage;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	
	public void updateRectangle (){
		playerRect = new Rectangle (x, y, 32, 32);
	}
	public void updateImage(){
		
	}
	
	public void shoot(){
		
	}
	
	
}

Thanks for any help

could you help me. The code I posted is from a different class would I be able to store the Graphics2D image back into the playerDrawn Image variable?

Sure, just get the graphics context of playerDrawn and draw the rotated version onto that. You could write a small method to do that for you like so

private Image getRotatedInstance(Image img, double theta){
        BufferedImage rotatedImg = new BufferedImage(img.getWidth(this), img.getHeight(this), BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = (Graphics2D)rotatedImg.getGraphics();
        g2.rotate(theta, img.getWidth(this)/2, img.getHeight(this)/2);
        g2.drawImage(img, 0, 0, this);
        return rotatedImg;
    }

There are some nuances of transformation and rotation that I'll leave for you to study.

The method I posted does virtually the same operations as those in the paintComponent() method of the class you linked to. I'm not sure which other difficulties you mean?

Ezzaral you doesn't take as flamewar, this codesnipped can works, but I have not much good experiencies with those way(s),

I wasn't taking it in offense, I just don't know which part you feel needs to be done differently. From what I could tell looking over it, the code that you linked to does the exact same that my method does, but mine just returns a rotated image, where yours is a component that displays the image.

The rotation part seems identical. Did I miss something there?

right, two diff.'s used Container and processes must be started (wrapped)from javax.swing.Timer

@mKorbel: Yes, your component would be useful if he wants to rotate the image as an animation. I don't know if he intends to do that or not. I was only providing a code fragment to rotate an image and keep that rotated version as a separate instance.

Evidently neither answer really makes any difference because the OP hasn't bothered to say another word on the matter and I wasted my time writing code for him.

Thanks for the help. the image would be rotated every time it needs to be (4 way directional turn according to which way the player is traveling).

then why I didn't see there your up-vote for post by her majesty @Ezzaral ???,

@mKorbel: I'm not really concerned about an up vote and I'm not sure what exactly you are inferring with the 'her majesty' comment. It is a matter of asking for help, receiving it, and then never acknowledging that the posts had even been read or if they solved the issue. It's solely a matter of basic courtesy when others expend their time to help out.

@Ezzaral that's my view and I can't to bend it somehow, in due all my respect, nothing else, all stuff that isn't worth I letting the whales

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.