quadrantRotate旋转的位图

quadrantRotate旋转的位图

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

问题描述

我要旋转的位图关于其的中心点,然后将其绘制成一个更大的图形上下文

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

该位图 40×40像素。图形上下文是 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.

我在做什么错了?

感谢

推荐答案

您转型事项的顺序。基本上,你的榜样code是说90度旋转图像,然后进行翻译......

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

所以,使用code(旋转,再转换成)产生...

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旋转的位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 16:50