0

如果您在 Java 中的桌子上旋转硬币,我如何旋转图像,就像硬币旋转一样?

就像这个Gif。

我试过用AffineTransform. 它并没有给我我想要的输出。

从这两个图像中可以看出,剪切通过使图像变大而不是仅仅旋转图像来扭曲图像。

4

1 回答 1

2

警告:你永远不会得到与显示相同的结果,但你可以伪造它

基本上,这个示例简单地将 x 轴从 -1 缩放到 1 并再次缩放...

在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ShearTest {

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

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

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage img;

        private float xScale = 1f;
        private float xDelta = 0.05f;

        public TestPane() {

            try {
                img = ImageIO.read(new File("C:\\hold\\thumbnails\\megatokyo.jpg"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            final Timer timer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    xScale += xDelta;
                    if (xScale > 1) {
                        xDelta *= -1;
                    } else if (xScale < -1) {
                        xDelta *= -1;
                    }
                    repaint();
                }
            });
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return img == null ? new Dimension(200, 200) : new Dimension(img.getWidth(), img.getHeight());
        }

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

                int x = (int)((getWidth() - (img.getWidth() * xScale)) / 2);
                int y = (getHeight() - img.getHeight()) / 2;

                AffineTransform at = new AffineTransform();
                at.translate(x, y);
                at.scale(xScale, 1);

                g2d.setTransform(at);
                g2d.drawImage(img, 0, 0, this);

                g2d.dispose();
            }
        }
    }
}
于 2013-08-23T07:12:41.963 回答