Rotating an image...

Hey guys a simple question for you all, I would like to rotate an image as by using the LEFT and RIGHT keys, moving up,down sideways aint a problem but just need this one out the way...Any response is appreciated thank you...
[240 byte] By [Boz-Leea] at [2007-9-28 17:56:05]
# 1
have a look at Graphics2D.rotate() and java.awt.geom.AffineTransform
Abusea at 2007-7-12 15:37:52 > top of Java-index,Other Topics,Java Game Development...
# 2

Here, this code will help you learn alot about AffineTransforms which is exactly what you need.

It will randomly rotate 3 images, enable anti-alisning and even make the pics semi-transparent! :D

import java.applet.*;

import java.awt.*;

import java.awt.geom.*;

import java.util.*;

public class ImageTest extends Applet

{

// the Images to render

private Image[] images;

private AlphaComposite alpha;

// filename description of our images

private final String[] filenames =

{ "image1.jpg", "image2.jpg", "image3.gif" };

public void init()

{

// get the base URL

java.net.URL appletBaseURL = getCodeBase();

alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.50f);

// allocate memory for the images and load 'em in

int n = filenames.length;

images = new Image[n];

for(int i = 0; i < n; i++)

{

images[i] = getImage(appletBaseURL, filenames[i]);

}

}

public void paint(Graphics g)

{

// cast the sent Graphics context to get a usable Graphics2D object

Graphics2D g2d = (Graphics2D)g;

// save an identity transform

final AffineTransform identity = new AffineTransform();

// used to transform our images

AffineTransform at = new AffineTransform();

Random r = new Random();

int width = getSize().width;

int height = getSize().height;

int numImages = filenames.length;

// render 100 images, each with a random transformation

for(int i = 0; i < 100; i++)

{

// clear the transformation

at.setTransform(identity);

// randomly set up the translation and rotation

at.translate(r.nextInt()%width, r.nextInt()%height);

at.rotate(Math.toRadians(360*r.nextDouble()));

at.scale(2*r.nextDouble(), 2*r.nextDouble());

// draw one of the images

g2d.setColor(Color.BLACK);

g2d.setFont(new Font("Helvetica", Font.BOLD, 76));

g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

g2d.drawString("Text Antialiasing On", 50, 50);

g2d.setComposite(alpha);

g2d.drawImage(images[i%numImages], at, this);

}

}

}// ImageTest

// <Applet code=ImageTest width=300 height=300></Applet>

-Adam

EvolvedAnta at 2007-7-12 15:37:52 > top of Java-index,Other Topics,Java Game Development...
# 3
Eh set the size of this font to like 10. I set 76 before while testing to check the antialisning. But it will cover the whole screen with large letters lolg2d.setFont(new Font("Helvetica", Font.BOLD, 76));
EvolvedAnta at 2007-7-12 15:37:52 > top of Java-index,Other Topics,Java Game Development...
# 4
Thanks a lot guys, especially adam, that code has helped me a lot thanks again...
Boz-Leea at 2007-7-12 15:37:52 > top of Java-index,Other Topics,Java Game Development...