Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4593744
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:43:41+00:00 2026-05-21T22:43:41+00:00

EDIT: OH!!!!! it works!!! It seems that at one point it was fixed However

  • 0

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());




    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T22:43:42+00:00Added an answer on May 21, 2026 at 10:43 pm
        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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: From another question I provided an answer that has links to a lot
EDIT: Learned that Webmethods actually uses NLST, not LIST, if that matters Our business
Edit: This was accidentally posted twice. Original: VB.NET Importing Classes I've seen some code
Could someone take a look at this code and find out what's wrong with
I have a function that accepts a char* as one of its parameters. I
I am trying to use the code in this article that lets you know
(Edit: is not just happening when screen is being touched. It seems to happen
Edit: Somehow I knew, after two days messing with this, that I'd figure it
EDIT: Scotty2012 and David Morton's answers don't work for me so I have put
Edit: This question was written in 2008, which was like 3 internet ages ago.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.