Need help rotating an image :)

Alrighty!

publicvoid rotate(Graphics g,int direction)

{

Image ship = sprite.getImage();

double turnDirection = Math.toRadians(direction);

AffineTransform AF =new AffineTransform();

Graphics2D g2 = (Graphics2D) g;

//g2.drawImage(ship, 0, 0, null);

g2.drawImage (ship, AF.getRotateInstance(turnDirection, this.x,this.y),null);

}

Trying to rotate a ship in my game.

Any suggestions or help you can give?

Thanks! :)

[775 byte] By [AbstractFirea] at [2007-11-27 4:36:37]
# 1
// if x, y is the origin of the imageAffineTransform at = AffineTransform.getTranslateInstance(x, y);at.rotate(turnRadians, ship.getWidth()/2, ship.getHeight()/2);You want to rotate the image from its center.
crwooda at 2007-7-12 9:46:47 > top of Java-index,Security,Cryptography...
# 2

public void rotate(Graphics g, int direction)

{

AffineTransform at = new AffineTransform();

Graphics2D g2 = (Graphics2D) g;

Image ship = sprite.getImage();

angle = angle + direction*15;

double turnRadians = Math.toRadians(angle);

at.translate(-x, -y);

at.rotate(turnRadians);

at.translate(ship.getWidth(null)/2, ship.getHeight(null)/2);

System.out.println(called);

g2.drawImage (ship, at, null);

}

that didn't help much, experiemented a bit.

AbstractFirea at 2007-7-12 9:46:47 > top of Java-index,Security,Cryptography...
# 3

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.AffineTransform;

import java.awt.image.BufferedImage;

import java.io.*;

import javax.imageio.ImageIO;

import javax.swing.*;

public class RotatingAnImage extends JPanel implements ActionListener {

BufferedImage image;

AffineTransform at;

Point loc = new Point(100,20);

int angle = 0;

public RotatingAnImage(BufferedImage image) {

this.image = image;

at = AffineTransform.getTranslateInstance(loc.x, loc.y);

}

public void actionPerformed(ActionEvent e) {

String ac = e.getActionCommand();

if(ac.equals("CCW"))

setTransform(-1);

if(ac.equals("CW"))

setTransform(1);

repaint();

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.drawRenderedImage(image, at);

}

private void setTransform(int direction) {

int iw = image.getWidth();

int ih = image.getHeight();

angle = angle + direction*15;

double theta = Math.toRadians(angle);

at.setToTranslation(loc.x, loc.y);

at.rotate(theta, iw/2, ih/2);

}

private JPanel getUIPanel() {

String[] ids = { "ccw", "cw" };

JPanel panel = new JPanel();

for(int j = 0; j < ids.length; j++) {

JButton button = new JButton(ids[j]);

button.setActionCommand(ids[j].toUpperCase());

button.addActionListener(this);

panel.add(button);

}

return panel;

}

public static void main(String[] args) throws IOException {

String path = "images/cougar.jpg";

BufferedImage image = ImageIO.read(new File(path));

RotatingAnImage test = new RotatingAnImage(image);

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(test);

f.getContentPane().add(test.getUIPanel(), "Last");

f.setSize(400,400);

f.setLocation(200,200);

f.setVisible(true);

}

}

crwooda at 2007-7-12 9:46:47 > top of Java-index,Security,Cryptography...