Rotate Image Created with createImage() ?

I've been looking around online for a way to do this, but so far the only things I have found are 50+ lines of code. Surely there is a simple way to rotate an image created with the createImage() function?

Here's some example code with all the tedious stuff already written. Can someone show me a simple way to rotate my image?

import java.net.*;

import java.awt.*;

import java.awt.geom.*;

import java.awt.event.*;

import javax.swing.*;

publicclass RotImgextends JAppletimplements MouseListener{

URL base;

MediaTracker mt;

Image myimg;

publicvoid init(){

try{ mt =new MediaTracker(this);

base = getCodeBase();}

catch(Exception ex){}

myimg = getImage(base,"myimg.gif");

mt.addImage(myimg, 1);

try{ mt.waitForAll();}

catch(Exception ex){}

this.addMouseListener(this);

}

publicvoid paint(Graphics g){

super.paint(g);

Graphics2D g2 = (Graphics2D) g;

g2.drawImage(myimg, 20, 20,this);

}

publicvoid mouseClicked(MouseEvent e){

//***** SOME CODE HERE *****//

// Rotate myimg by 5 degrees

//******** END CODE ********//

repaint();

}

publicvoid mouseEntered(MouseEvent e){}

publicvoid mouseExited(MouseEvent e){}

publicvoid mousePressed(MouseEvent e){}

publicvoid mouseReleased(MouseEvent e){}

}

Thanks very much for your help!

null

[3242 byte] By [L4E_WakaMol-Kinga] at [2007-11-27 8:41:38]
# 1
hi!have a look at this, http://www.java2s.com/Code/Java/2D-Graphics-GUI/RotateImage45Degrees.htmregardsAniruddha
Aniruddha-Herea at 2007-7-12 20:40:41 > top of Java-index,Security,Cryptography...
# 2

AffineTransform tx = new AffineTransform();

tx.rotate(radians, bufferedImage.getWidth()/2, bufferedImage.getHeight()/2);

AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);

bufferedImage = op.filter(bufferedImage, null);

Hope this helps

~Matt

OriginalSup3rmana at 2007-7-12 20:40:41 > top of Java-index,Security,Cryptography...
# 3

// <applet code="RotationApplet" width="400" height="400"></applet>

// use: >appletviewer RotationApplet.java

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.AffineTransform;

import java.awt.image.BufferedImage;

import javax.swing.*;

public class RotationApplet extends JApplet {

RotationAppletPanel rotationPanel;

public void init() {

Image image = loadImage();

rotationPanel = new RotationAppletPanel(image);

setLayout(new BorderLayout());

getContentPane().add(rotationPanel);

}

private Image loadImage() {

String path = "images/cougar.jpg";

Image image = getImage(getCodeBase(), path);

MediaTracker mt = new MediaTracker(this);

mt.addImage(image, 0);

try {

mt.waitForID(0);

} catch(InterruptedException e) {

System.out.println("loading interrupted");

}

return image;

}

}

class RotationAppletPanel extends JPanel {

BufferedImage image;

double theta = 0;

final double thetaInc = Math.toRadians(5.0);

public RotationAppletPanel(Image img) {

image = convert(img);

addMouseListener(ml);

}

public void rotate() {

theta += thetaInc;

repaint();

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,

RenderingHints.VALUE_INTERPOLATION_BICUBIC);

double x = (getWidth() - image.getWidth())/2;

double y = (getHeight() - image.getHeight())/2;

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

at.rotate(theta, image.getWidth()/2, image.getHeight()/2);

g2.drawRenderedImage(image, at);

}

private BufferedImage convert(Image src) {

int w = src.getWidth(this);

int h = src.getHeight(this);

int type = BufferedImage.TYPE_INT_RGB; // options

BufferedImage dest = new BufferedImage(w,h,type);

Graphics2D g2 = dest.createGraphics();

g2.drawImage(src,0,0,this);

g2.dispose();

return dest;

}

private MouseListener ml = new MouseAdapter() {

public void mousePressed(MouseEvent e) {

rotate();

}

};

}

crwooda at 2007-7-12 20:40:41 > top of Java-index,Security,Cryptography...
# 4
Thank you for your responses. I will try these different answers out tomorrow night and see which one is best.
L4E_WakaMol-Kinga at 2007-7-12 20:40:41 > top of Java-index,Security,Cryptography...