使用 Java 中的算法更改绘图的角度/位置

2023-12-24

你好,我很好奇如何解决这个问题:我用 Java 中的 fillArc、drawArc 方法创建了一个 pacman,现在我的屏幕上有一个 pacman 家伙,无论它朝哪个方向走,它总是向右看。我的问题是..有没有办法在Java中改变对象的角度或水平翻转它?

我尝试使用 AffineTransform 但我没有得到我想要的文档...我应该如何使用 switch 语句来实现这一点?我尝试执行以下操作,但我陷入了这部分,因为我不知道如何继续。

DrawPacMan pacman = new DrawPacMan();
DrawPacMan ghost1 = new DrawPacMan();
DrawPacMan ghost2 = new DrawPacMan();

AffineTransform pac = new AffineTransform();

public void setPacManView(int waarde) {
    // set the view of pacman
    switch (waarde) {
    case 0 :
        // here one view of pacman
        break;
    case 1 :
        // here one view of pacman
        break;
    case 2 :
        // here one view of pacman
        break;
    case 3 :
        // here one view of pacman
        break;

    }
}

public void paintComponent(Graphics g) {

    super.paintComponent(g);
    // pacman movement
    diameter = 75;  
    pacman.drawPacMan(g, getHorPlaats(), getVerPlaats(), diameter, Color.yellow);

    // ghosts movement
    int g1x;
    for(g1x = 0; g1x < 10; g1x++) {

        pacman.drawGhost(g, g1x, 40, diameter, Color.red);

    }
    pacman.drawGhost(g, 170, 70, diameter, Color.blue);


}

尝试一些像...

该演示旨在允许图像通过虚拟天使(角度 360)旋转,但基本概念是相同的......

public class TestFlipImage {

    protected static final String IMAGE_PATH = "/path/to/your/image";

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

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

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());

                BufferedImage image = null;
                try {
                    image = ImageIO.read(new File(IMAGE_PATH));
                } catch (IOException ex) {
                }

                JPanel mainPane = new JPanel(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                mainPane.add(new ImagePane(image, 0));
                mainPane.add(new ImagePane(image, 90));
                mainPane.add(new ImagePane(image, 180));

                frame.add(mainPane);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ImagePane extends JPanel {

        private BufferedImage masterImage;
        private BufferedImage renderedImage;

        public ImagePane(BufferedImage image, int angle) {
            masterImage = image;
            applyRotation(angle);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(renderedImage.getWidth(), renderedImage.getHeight());
        }

        @Override
        public Dimension getMinimumSize() {
            return getPreferredSize();
        }

        protected int getVirtualAngle(int angle) {
            float fRotations = (float) angle / 360f;
            int rotations = (int) (fRotations - (fRotations / 1000));

            int virtual = angle - (rotations * 360);

            if (virtual < 0) {
                virtual = 360 + virtual;
            }

            return virtual;
        }

        // The code is designed to rotate an image through 90 degree
        // angles, but it can handle angle's less then 0 and greater then
        // 360 degrees
        public void applyRotation(int angle) {
            // This will only work for angles of 90 degrees...

            // Normalize the angle to make sure it's only between 0-360 degrees
            int virtualAngle = getVirtualAngle(angle);
            Dimension size = new Dimension(masterImage.getWidth(), masterImage.getHeight());
            int masterWidth = masterImage.getWidth();
            int masterHeight = masterImage.getHeight();

            double x = 0; //masterWidth / 2.0;
            double y = 0; //masterHeight / 2.0;

            switch (virtualAngle) {
                case 0:
                    break;
                case 180:
                    break;
                case 90:
                case 270:
                    size = new Dimension(masterImage.getHeight(), masterImage.getWidth());
                    x = (masterHeight - masterWidth) / 2.0;
                    y = (masterWidth - masterHeight) / 2.0;
                    break;
            }
            renderedImage = new BufferedImage(size.width, size.height, masterImage.getTransparency());
            Graphics2D g2d = renderedImage.createGraphics();

            AffineTransform at = AffineTransform.getTranslateInstance(x, y);

            at.rotate(Math.toRadians(virtualAngle), masterWidth / 2.0, masterHeight / 2.0);
            g2d.drawImage(masterImage, at, null);

            g2d.dispose();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g;
            int width = getWidth() - 1;
            int height = getHeight() - 1;

            int x = (width - renderedImage.getWidth()) / 2;
            int y = (height - renderedImage.getHeight()) / 2;

            g2d.drawImage(renderedImage, x, y, this);
        }
    }
}

额外的

您可能还想看看AffineTransform.rotate() - 如何同时缩放、旋转和缩放? https://stackoverflow.com/questions/11911610/affinetransform-rotate-how-do-i-xlate-rotate-and-scale-at-the-same-time/11911758#11911758wch 讨论了一种在水平和垂直轴上翻转图像的方法

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Java 中的算法更改绘图的角度/位置 的相关文章

随机推荐