3

好吧,伙计们,我正在尝试制作小行星类型的游戏,我需要能够旋转图像,以便船的前部跟随我的鼠标。我现在已经找了几个小时,找到了一些东西,但没有一个能满足我的需求。

如果有人知道如何做到这一点,请分享!

提前致谢

这是我现在拥有的代码

package Asteroids;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

    import javax.swing.ImageIcon;
    import javax.swing.JFrame;

    @SuppressWarnings("serial")
    public class Asteroids extends JFrame implements Runnable, MouseListener,
            MouseMotionListener, KeyListener {

        private Image dbImage;
        private Graphics dbg;
        int x, y, mx, my;
        int a, b, c, degree;
        double scale = 1.0;
        ImageIcon shipIcon = new ImageIcon(this.getClass().getClassLoader()
                .getResource("AstroidsShip.png"));
        Image ship = shipIcon.getImage();

        public static void main(String[] args) {
            Asteroids frame = new Asteroids();
            Thread thread = new Thread(frame);
            thread.start();
        }

        public Asteroids() {
            addMouseListener(this);
            addMouseMotionListener(this);
            addKeyListener(this);

            setTitle("Astroids");
            setSize(500, 500);
            setBackground(Color.WHITE);
            setResizable(false);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);

            x = getWidth() / 2 - 10;
            y = getHeight() - getHeight() / 2;
        }

        public void run() {
            while (true) {
                try {

                } catch (Exception e) {

                }
            }
        }

        public void paint(Graphics g) {
            dbImage = createImage(getWidth(), getHeight());
            dbg = dbImage.getGraphics();
            paintComponent(dbg);
            g.drawImage(dbImage, 0, 0, this);
        }

        public void paintComponent(Graphics g) {
            g.drawString("POS: " + mx + ", " + my, 10, 40);
            System.out.println(getAngle());
            g.drawImage(ship, x, y, this);
            g.drawLine(x + 12, y + 10, mx, my);
            repaint();
        }

        public int getAngle() {
            a = mx - (x + 12);
            b = (y - 10) - my;
            return degree = (int) Math.toDegrees(Math.atan2(b, a));
        }

        @Override
        public void keyTyped(KeyEvent e) {

        }

        @Override
        public void keyPressed(KeyEvent e) {

        }

        @Override
        public void keyReleased(KeyEvent e) {

        }

        @Override
        public void mouseDragged(MouseEvent e) {

        }

        @Override
        public void mouseMoved(MouseEvent e) {
            mx = e.getX();
            my = e.getY();
        }

        @Override
        public void mouseClicked(MouseEvent e) {

        }

        @Override
        public void mousePressed(MouseEvent e) {

        }

        @Override
        public void mouseReleased(MouseEvent e) {

        }

        @Override
        public void mouseEntered(MouseEvent e) {

        }

        @Override
        public void mouseExited(MouseEvent e) {

        }
    }
4

3 回答 3

3

您可以使用Java2D。它允许旋转图像并对图像进行许多其他很酷的操作。它还获得了硬件加速支持(通过 DirectX 或 OpenGL)。

Java2D 内置在 JRE 中。

于 2012-09-08T12:12:48.370 回答
2

在您的 paintComponent 方法中,您可以使代码如下所示:

public void paintComponent(Graphics g) {
    g.drawString("POS: " + mx + ", " + my, 10, 40);
    System.out.println(getAngle());
    Graphics2D g2d = new (Graphics2D)g;
    g2d.translate(X, Y);
    g2d.rotate(DEGREES);
    g2d.drawImage(ship, 0, 0, WIDTH, HEIGHT, this); 
    g.drawLine(x + 12, y + 10, mx, my);
    repaint();
}

对于更复杂的使用,您可以创建一个包含所有船舶图纸计算的方法:

public void paintComponent(Graphics g) {
    g.drawString("POS: " + mx + ", " + my, 10, 40);
    System.out.println(getAngle());
    drawShip(g);
    g.drawLine(x + 12, y + 10, mx, my);
    repaint();
}

public void drawShip(Graphics g){
    Graphics2D g2d = (Graphics2D)g;
    g2d.translate(X, Y);
    g2d.rotate(DEGREES);
    g2d.drawImage(ship, 0, 0, WIDTH, HEIGHT, this);
}
于 2012-09-08T12:24:27.877 回答
2

一种方法是使用其中一种方法中的旋转实例,该方法AffineTransform接受 X,Y 坐标的锚点。

于 2012-09-08T12:47:32.943 回答