0

是的,我知道你可以使用 AffineTransformation,但是我希望我的剑图像围绕我制作的角色(在图形中绘制的黑色块)旋转 360 度,而不是仅仅旋转一次。基本上我想要一个像泰拉瑞亚那样的旋转系统。我知道如何获得角色的 x 和 y,所以问题是:如何让它围绕我定义的点旋转?我的代码是这样设置的

    f.addMouseListener(new MouseAdapter(){
    public void mouseClicked(MouseEvent e){
        swordSwinging=true;
    }
});

...

if(swordSwinging){
    //swinging code goes here
}

repaint();
4

2 回答 2

6

你的意思是...

在此处输入图像描述

(请注意,红线是指示与中心角度的指导线,您不需要它;))

花哨的东西在getSwordHandlePoint方法中,它沿着向量计算应该放置手柄的点......

public class TestSword {

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

    public TestSword() {
        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("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new SwordPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class SwordPane extends JPanel {

        private BufferedImage character;
        private BufferedImage sword;
        private double angle = 0;

        public SwordPane() {
            try {
                character = ImageIO.read(new File("character.png"));
                sword = ImageIO.read(new File("Sword.png"));
            } catch (IOException exp) {
                exp.printStackTrace();
            }

            Timer timer = new Timer(100, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    angle += 10;
                    if (angle > 360) {
                        angle -= 360;
                    }
                    repaint();
                }
            });
            timer.setRepeats(true);
            timer.setCoalesce(true);
            timer.start();

        }

        @Override
        public Dimension getPreferredSize() {
            int width = character.getHeight() + sword.getWidth();
            int height = character.getHeight() + sword.getWidth();

            return new Dimension(width * 2, height * 2);
        }

        protected Point getSwordHandlePoint() {

            int radius = 272; // This is the height of the character...

            int x = Math.round(getWidth() / 2);
            int y = Math.round(getHeight() / 2);

            double rads = Math.toRadians(angle - 180); // Make 0 point out to the right...
            // If you add sword.getWidth, you might be able to change the above...
            int fullLength = Math.round((radius / 2f)) - sword.getWidth();

            // Calculate the outter point of the line
            int xPosy = Math.round((float) (x + Math.cos(rads) * fullLength));
            int yPosy = Math.round((float) (y - Math.sin(rads) * fullLength));

            return new Point(xPosy, yPosy);

        }

        @Override
        protected void paintComponent(Graphics g) {

            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g.create();

            int x = (getWidth() - character.getWidth()) / 2;
            int y = (getHeight() - character.getHeight()) / 2;

            g2d.drawImage(character, x, y, this);

            x = getWidth() / 2;
            y = getHeight() / 2;

            Point p = getSwordHandlePoint();
            g2d.setColor(Color.RED);
            g2d.drawLine(x, y, p.x, p.y);

            AffineTransform at = AffineTransform.getTranslateInstance(p.x, p.y);
            at.rotate(Math.toRadians(-angle));
            g2d.setTransform(at);
            g2d.drawImage(sword, 0, 0, this);

            g2d.dispose();

        }
    }
}

现在,我的三角是没有希望的,那么没有希望了。我从网上“借用”了算法并根据自己的需要进行了调整......

于 2012-10-19T10:09:41.990 回答
1

该类Graphics2D有一个方法g2.rotate(...),一次调用一个for loop设置旋转多度,然后g2.drawImage(...)在循环中调用,在每次更改之后(如果它在paintComponent()方法中,并且循环在外部 -repaint()在 for 循环中调用)。

于 2012-10-19T04:37:25.087 回答