Rotating an image nuisance

Alright guys i'm trying to make an Asteroids type game and I need to be able to rotate an image around so that the front of the ship follows my mouse. I have looked for a few hours now and have found a couple of things but none that satisfy my needs.

If anyone knows how to do this please share!

thanks in advance

here is the code i have now

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;

    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()
        Image ship = shipIcon.getImage();

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

        public Asteroids() {

            setSize(500, 500);

            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();
            g.drawImage(dbImage, 0, 0, this);

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

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

        public void keyTyped(KeyEvent e) {


        public void keyPressed(KeyEvent e) {


        public void keyReleased(KeyEvent e) {


        public void mouseDragged(MouseEvent e) {


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

        public void mouseClicked(MouseEvent e) {


        public void mousePressed(MouseEvent e) {


        public void mouseReleased(MouseEvent e) {


        public void mouseEntered(MouseEvent e) {


        public void mouseExited(MouseEvent e) {



You can use Java2D. It allows to rotate the image and do a lot lot of other cool operations on the image. It also have got hardware acceleration support (through DirectX or OpenGL).

Java2D is built-in in JRE.

Need Your Help

“self” as method attribute

python properties attributes self

I am attempting to teach myself Python at the moment and I am trying to work my way through a python program, which is essentially a pacman game. Whilst doing so, I discovered the following class c...

Yii transaction not rollbacked

php exception activerecord yii transactions

i have the following piece of code and i pass it some data to generate an exception and test if the transaction rollback is happening. It seems it's not and i'm not sure why.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.