3

EDIT: OH!!!!! it works!!! It seems that at one point it was fixed However the wrong HTML file was opening up so the wrong code was running. I feel stupid, that should have been obvious.

But THANKYOU!!!

Its so awesome to actually get some help with this stuff. whenever i ask for some help anywhere else or even ask my teacher im usually ignored or get useless advice. (end of edit)

Im making a game for my final project in a java class. I just got mouse aiming to work using AffineTransform, however When ever the player object rotates to 90 degrees(or a multiple of it), it does this weird stutter thing. Heres the code im specifically concerned with.

    g2.drawImage(img, x_pos,y_pos,this);
    AffineTransform oldTransform = g2.getTransform();

g2.setTransform(AffineTransform.getRotateInstance(radAngle,x_pos +   (img.getWidth() / 2),y_pos+(img.getHeight() / 2)));

Could someone please help me figure out how to fix this? my project is due tomorrow so im slim on time. Here are the images i use

http://i56.tinypic.com/iolys3_th.png http://i53.tinypic.com/27hif_th.png http://i56.tinypic.com/21jayoh.jpg

Heres the code.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.lang.Math.*;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import javax.imageio.ImageIO;
import java.io.*;
import java.net.URL;

public class Game extends Applet implements Runnable, KeyListener, MouseMotionListener
{

    int x_pos = 250;
    int y_pos = 250;
    float x_speed = 0;
    float y_speed = 0;
    int radius = 20;
    int appletsize_x = 800;
    int appletsize_y = 600;

    int x = 0;
    int y = 0;
    int up = 0;
    int down= 0;
    int left = 0;
    int right= 0;

    int mouse_x;
    int mouse_y;
    int tracking_angle;

    private BufferedImage dbImage;
    private Graphics dbg;
    private  Image curser;
    BufferedImage img = null;
    BufferedImage round = null;

    AffineTransform at = new AffineTransform();
    double radAngle;
    public void init()
    {
            try {
               URL url = new URL(getCodeBase(), "Player.png");
               img = ImageIO.read(url);
            } catch (IOException e) {System.out.println("Cant find player image");
        }
                        try {
                           URL url = new URL(getCodeBase(), "round.png");
                           round = ImageIO.read(url);}
             catch (IOException e) {}

        setBackground (Color.blue);
        setFocusable(true);
        addKeyListener( this );
        curser = getImage(getDocumentBase(), "mouse.png");
        addMouseMotionListener(this);
        try
                {
                    Toolkit tk = Toolkit.getDefaultToolkit();
                    Cursor c = tk.createCustomCursor( curser,new Point( 5, 5 ), "Inodrop" );
                    setCursor( c );
                }
                catch( IndexOutOfBoundsException x )
                {}
    }

    public class Shot {

        int x_loc = -50;
        int y_loc = -50;
        public Shot(){
            if(x_loc < 0){
                x_loc = x_pos;}
            if(y_loc < 0){
                y_loc = y_pos;}

                paint(dbg);}
        public void paint(Graphics g){
            System.out.println("hi");

             Graphics2D g2d = (Graphics2D)g;

            Graphics g2D = round.getGraphics();
            g2d.drawImage(round, x_loc,y_loc,null);}}

    public void start ()
    {

        Thread th = new Thread (this);

        th.start ();

    }

    public void stop()
    {

    }

    public void destroy()
    {

    }
    public void mouseMoved(MouseEvent e){
        //get position of mouse
        mouse_x = e.getX();
        mouse_y = e.getY();
        double x_dist = mouse_x - x_pos;
        double y_dist = mouse_y - y_pos;


        if (x_dist == 0) {
          radAngle = 90;
        } else if ((x_dist == 0) && (y_dist == 0)) {
          radAngle = 0;
        } else {
          radAngle = Math.atan(y_dist / x_dist);
        }


        tracking_angle = (int)(Math.sin(radAngle) * 100);
        //System.out.println(Math.toRadians(tracking_angle));
        }
    public void mouseDragged(MouseEvent e){}


    public void keyReleased(KeyEvent r)
    {
        //Left
        if (r.getKeyCode()  == 39 ){
            x = 0;
            left = 0;
            Shot shoot = new Shot();
            }
        //Right
        if (r.getKeyCode() == 37){
            x = 0;
            right = 0;
            }
        //Down
        if (r.getKeyCode() == 38 ) {
            //y_speed = 0;
            down = 0;}
        //Up
        if (r.getKeyCode() == 40 ) {
            //y_speed = 0;
            up = 0;}
            //move();
}
    public void keyTyped(KeyEvent t){}
    public void keyPressed(KeyEvent r){


        //Left
        if (r.getKeyCode()  == 39 ){
            left = 1;}
        //Right
        if (r.getKeyCode() == 37){
            right = 1;}
        //Down
        if (r.getKeyCode() == 38 ) {
            down = 1;}
        //Up
        if (r.getKeyCode() == 40 ) {
            up = 1;}
            //move();
}


    public void run ()
    {

        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);


        while (true)
        {
            if (left  == 1 && x_speed < 11){
                x = 0;
                x_speed += 1;
                }
            //Right
            if (right == 1 && x_speed > -11){
                x = 0;
                 x_speed -= 1;
                }
            //Down
            if (down == 1  && y_speed > -11) {
                y_speed -= 1;}
            //Up
            if (up == 1  && y_speed < 11) {
            y_speed += 1;}
        if( x == 0 && x_speed > 0){
            x_speed -=.2;}
        if( x == 0 && x_speed < 0){
            x_speed +=.2;}
        if( y == 0 && y_speed > 0){
            y_speed -=.2;}
        if( y == 0 && y_speed < 0){
            y_speed +=.2;}



            if (x_pos > appletsize_x - radius && x_speed > 0)
            {

                x_pos = radius;
            }

            else if (x_pos < radius && x_speed < 0)
            {

                x_pos = appletsize_x + radius ;
            }
            //System.out.println(y_pos);
            if (y_pos > appletsize_y - radius && y_speed > 0){
                y_speed = 0;}
            else if ( y_pos < radius && y_speed < 0  ){
                    y_speed = 0;}

            x_pos += (int)x_speed;
            y_pos += (int)y_speed;



            repaint();

            try
            {


                Thread.sleep (15);


            }
            catch (InterruptedException ex)
            {

            }


            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
        }
    }


    public void update (Graphics g)
    {

        if (dbImage == null)
        {
            dbImage = new BufferedImage(this.getSize().width, this.getSize().height, BufferedImage.TYPE_INT_RGB);
            dbg = dbImage.getGraphics ();
        }


        dbg.setColor (getBackground ());
        dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);


        dbg.setColor (getForeground());
        paint (dbg);

        g.drawImage (dbImage, 0, 0, this);


    }

    public void paint (Graphics g)
    {
        //g = img.getGraphics();
        Graphics2D g2 = (Graphics2D)g;


        g2.drawImage(img, x_pos,y_pos,this);
        AffineTransform oldTransform = g2.getTransform();

        g2.setTransform(AffineTransform.getRotateInstance(radAngle,x_pos + (img.getWidth() / 2),y_pos+(img.getHeight() / 2)));



        System.out.println(img.getWidth());




    }
}
4

2 回答 2

3
    double x_dist = mouse_x - x_pos;
    double y_dist = mouse_y - y_pos;
    double radAngle = Math.atan(y_dist / x_dist);
    tracking_angle = (int) (Math.sin(radAngle) * 100);

I think the error is somewhere in this part. You're definitely dividing by 0 here if x_dist is 0. Better do something like this:

if (x_dist == 0) {
    radAngle = 90;
} else {
    radAngle = Math.atan(y_dist / x_dist);
}

EDIT: Additionally, I think you should throw away the tracking_angle line completely and just do this later:

g2.setTransform(AffineTransform.getRotateInstance(rad_angle,
  x_pos + (img.getWidth() / 2),y_pos+(img.getHeight() / 2)));

Also you should change that new code you posted in the comments:

double radAngle;
if (x_dist == 0) {
  radAngle = 90;
} else if (y_dist == 0) {
  radAngle = 90;
} else {
  radAngle = Math.atan(y_dist / x_dist);
}

You are solving this for x_dist == 0 or y_dist == 0 (which isn't an edge case), but not for the case that both are 0 where you simply can't compute an angle and I think you should go with 0. So use this instead:

double radAngle;
if (x_dist == 0) {
  radAngle = 90;
} else if ((x_dist == 0) && (y_dist == 0)) {
  radAngle = 0;
} else {
  radAngle = Math.atan(y_dist / x_dist);
}

Also, as trashgod pointed out, you're ignoring exceptions, for example:

    try {
        URL url = new URL(getCodeBase(), "Player.png");
        img = ImageIO.read(url);
    } catch (IOException e) {
    }

You should not just continue in such cases but f.e. display an useful error message and quit the program, in any case do something instead of just catching the exception and continue as if nothing happened.

于 2011-05-05T23:43:10.520 回答
2

I haven't tested this, but try:

double radAngle = Math.atan2(y_dist, x_dist);
于 2011-05-06T00:11:15.170 回答