Everyone has use an AffineTransformOp on a Raster??
I want to rotate an image that use a class that implements Paint and PaintContents so based on a BufferedImage and a Raster, so I think I have to use an AffineTransformOp; but using it I could get the rotation; maybe I have made a mistake, so if someone can help... thanks!!
09-09-2002, 07:26 PM
o_sam_o
I was having a problem simular to yours involving rotating images. The code I've attached is what I used to solve my problems. It uses swing and panels and rotates the image around the centre point, I wasn’t using swing and have to change the point at which it rotated around but this code did help me a lot. I got it from some website I have now forgotten, however I found the website though goolge searching for rotate image and java.
public void rotate() {
//rotate 5 degrees at a time
currentAngle+=5.0;
if (currentAngle >= 360.0) {
currentAngle = 0;
}
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
AffineTransform origXform = g2d.getTransform();
AffineTransform newXform = (AffineTransform)(origXform.clone());
//center of rotation is center of the panel
int xRot = this.getWidth()/2;
int yRot = this.getHeight()/2;
newXform.rotate(Math.toRadians(currentAngle), xRot, yRot);
g2d.setTransform(newXform);
//draw image centered in panel
int x = (getWidth() - image.getWidth(this))/2;
int y = (getHeight() - image.getHeight(this))/2;
g2d.drawImage(image, x, y, this);
g2d.setTransform(origXform);
}
public Dimension getPreferredSize() {
return new Dimension (image.getWidth(this), image.getHeight(this));
}
public static void main(String[] args) {
JFrame f = new JFrame();
Container cp = f.getContentPane();
cp.setLayout(new BorderLayout());
Image testImage =
Toolkit.getDefaultToolkit().getImage("gumby.gif");
final RotatePanel rotatePanel = new RotatePanel(testImage);
JButton b = new JButton ("Rotate");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
rotatePanel.rotate();
}
});
cp.add(rotatePanel, BorderLayout.CENTER);
cp.add(b, BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
}
}
09-10-2002, 10:58 AM
Lupin III
Thanks for the code, but I have already tried to use it, without result!
The problem is that I use objects that have as attribute a PuzzlePaint, which is a class that extend Paint and PaintContext!!
When I have to paint them, no problem but when I have to rotate them the shape rotates but the image remains the same!! so if these objects are puzzleparts the piece rotates but the image remains the same!!!
So I have tried to make some changes to the attribute that extends Paint; PuzzlePaint is based on BufferedImage and Raster. There is a transformation to rotate BufferedImage and another one to rotate Raster but it doesn't work!!!
Do you know anything about it???
09-10-2002, 09:17 PM
o_sam_o
Much to advanced for me, Sorry. :confused:
09-11-2002, 04:09 AM
Lupin III
Do you know someone that can help me?
09-11-2002, 09:40 PM
o_sam_o
Sorry i dont know anyone that can help. Im having my own problems now, im using the above code to rotate an image in an applet, however most machine dont have a jvm that can run that code. Do you or anyone else know how rotate an image another way ?? Which will work on most machines