DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2002
    Posts
    3

    Angry AffineTransformOp

    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!!

  2. #2
    Join Date
    Sep 2002
    Posts
    13
    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.

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.swing.*;

    public class RotatePanel extends JPanel {
    private Image image;
    private double currentAngle;

    public RotatePanel(Image image) {
    this.image = image;
    MediaTracker mt = new MediaTracker(this);
    mt.addImage(image, 0);
    try {
    mt.waitForID(0);
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    }

    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);
    }
    }
    http://home.pacific.net.au/~cavenagh

  3. #3
    Join Date
    Sep 2002
    Posts
    3

    Unhappy

    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???

  4. #4
    Join Date
    Sep 2002
    Posts
    13
    Much to advanced for me, Sorry.
    http://home.pacific.net.au/~cavenagh

  5. #5
    Join Date
    Sep 2002
    Posts
    3
    Do you know someone that can help me?

  6. #6
    Join Date
    Sep 2002
    Posts
    13
    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

    the applet is at

    home.pacific.net.au/~cavenagh/RR/Car.html

    I know its the rotate code as ive removed it and the applet will run on other machines.
    http://home.pacific.net.au/~cavenagh

  7. #7
    Join Date
    Sep 2002
    Posts
    13

    Exclamation

    Im still dont know how to rotate an image other than using 2D graphics, can anyone help ??
    http://home.pacific.net.au/~cavenagh

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links