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
# 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();
}
};
}