如何使用 AffineTransform.quadrantRotate 旋转位图? [英] How to use AffineTransform.quadrantRotate to rotate a bitmap?

查看:23
本文介绍了如何使用 AffineTransform.quadrantRotate 旋转位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想围绕其中心点旋转位图,然后将其绘制到更大的图形上下文中.

I want to rotate a bitmap about its center point, and then draw it into a larger graphics context.

位图是 40x40 像素.图形上下文是 500x500 像素.这就是我正在做的:

The bitmap is 40x40 pixels. The graphics context is 500x500 pixels. This is what I'm doing:

BufferedImage bi = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();

AffineTransform at = new AffineTransform();
at.quadrantRotate(1, -20, -20); // rotate 90 degrees around center point.
at.translate(100, 40); // I want to put its top-left corner at 100,40.

g.drawImage(smallerBitmap, at, null);

...

我可能错误地使用了 quadrantRotate() - 如果我删除那条线,我的图像至少会正确地绘制在 100,40 位置.

I'm probably using quadrantRotate() incorrectly - if I remove that line, my image gets drawn at position 100,40 correctly at least.

我做错了什么?

谢谢

推荐答案

转换的顺序很重要.基本上你的示例代码是说将图像旋转 90 度,然后翻译它......"

The order of your transformation matters. Basically your example code is saying "rotate the image by 90 degrees AND then translate it...."

因此,使用您的代码(旋转,然后翻译)会产生...

So, using your code (rotate, then translate) produces...

切换顺序(平移然后旋转)产生

Switching the order (translate then rotate) produces

...

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class TestRotation100 {

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

    public TestRotation100() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                final TestPane testPane = new TestPane();
                final JSlider slider = new JSlider(0, 3);
                slider.addChangeListener(new ChangeListener() {
                    @Override
                    public void stateChanged(ChangeEvent e) {
                        testPane.setQuad(slider.getValue());
                    }
                });
                slider.setValue(0);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(testPane);
                frame.add(slider, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage img;
        private int quad = 0;

        public TestPane() {
            try {
                img = ImageIO.read(new File("/Users/swhitehead/Dropbox/MegaTokyo/Rampage_Small.png"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        public void setQuad(int quad) {
            this.quad = quad;
            repaint();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();

            AffineTransform at = new AffineTransform();
            at.translate(100, 40);
            at.quadrantRotate(quad, img.getWidth() / 2, img.getHeight() / 2);

            g2d.drawImage(img, at, this);

            g2d.dispose();
        }

    }

}

这篇关于如何使用 AffineTransform.quadrantRotate 旋转位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆