0

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

3
Contributors
14
Replies
15
Views
5 Years
Discussion Span
Last Post by mKorbel
Featured Replies
  • 2
    Ezzaral 2,714   5 Years Ago

    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[CODE] 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, … Read More

0

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?

2

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.

0

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?

0

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

0

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?

0

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

0

@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.

0

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.

0

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).

0

@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.

0

@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

Edited by mKorbel: n/a

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.