简体   繁体   English

Java Graphics2D AffineTransform图像旋转

[英]Java Graphics2D AffineTransform Image Rotation

I am trying to simply rotate an image in a for loop like so: 我试图简单地在for循环中旋转图像,如下所示:

class MyCanvas extends JComponent {

AffineTransform identity = new AffineTransform();
Image arrow;
Double angle = -180.0;

public void spin() {
    angle += 10.0;
    for(int i = 0; i < 10; i++) {
        repaint();
        System.out.println(i);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    arrow = Toolkit.getDefaultToolkit().getImage("red-arrow-right-th.png");
    // Rotate + translate
    AffineTransform trans = new AffineTransform();
    trans.setTransform(identity);
    trans.translate(getWidth()/2, getHeight()/2);
    trans.rotate(Math.toRadians(angle));
    System.out.println(trans);
    g2.drawImage(arrow, trans, this);
    g2.finalize();
}
}

However when I run call spin() in main, it appears to apply only a single rotation, whilst still printing out the loop correctly. 但是当我在main中运行调用spin()时,它似乎只应用一次旋转,同时仍然正确地打印出循环。 What am I overlooking something? 我忽略了什么?

I've transformed your code using the recommendation of MadProgrammer: 我使用MadProgrammer的推荐改造了你的代码:

  • Don't override paint , override paintComponent . 不要覆盖paint ,覆盖paintComponent
  • Call super.paint before performing any custom painting, 在执行任何自定义绘画之前调用super.paint
  • Never call finalize on anything and especially not on objects you didn't create yourself. 永远不要对任何事情进行finalize ,特别是对自己没有创建的对象。
  • Use a Swing Timer 使用Swing Timer

Note the following 请注意以下内容

  • A qualified this is used to access the ImageRotationView instance from the ActionListener inner class. 限定this用于从ActionListener内部类访问ImageRotationView实例。

  • AffineTransform.getRotateInstance returns a transform that rotates coordinates around an anchor point. AffineTransform.getRotateInstance返回一个围绕锚点旋转坐标的变换。

  • Speed can be optimized but it works correctly like this. 速度可以优化,但它可以像这样正常工作。
  • This class works as a standalone application 此类作为独立应用程序运行
  • A file named dice.png should be present in the base directory. 名为dice.png的文件应存在于基目录中。

.

import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.Timer;


public class ImageRotationFrame {

    public static void main(String[] args) {
        new ImageRotationFrame();
    }

    public ImageRotationFrame() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Testing");
                frame.setSize(400, 400);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new ImageRotationComponent());
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });

    }

    private class ImageRotationComponent extends JComponent {

        Image arrow;
        double angle = 0.0;

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            angle += 0.4;
            AffineTransform trans = AffineTransform.getRotateInstance(angle, getWidth() / 2, getHeight() / 2);
            ((Graphics2D) g).drawImage(arrow, trans, this);
        }

        public ImageRotationComponent() {
            try {
                arrow = ImageIO.read(new File("dice.png"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            int delay = 500; //milliseconds
            ActionListener taskPerformer = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent evt) {
                    ImageRotationComponent.this.repaint();
                }
            };
            new Timer(delay, taskPerformer).start();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM