1

I try to learn java and I got a homework to write a class. There have got to be private double variables "pan" and "tilt" and methods, which return these variables (getPan and getTilt). Moreover there should be implemented mouseMoved method, which should update the values of pan and tilt using two formulae. So I wrote this piece of code:

package comp102x.project.task;

import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import comp102x.project.view.GameScreen;

public class AimListener implements MouseMotionListener{
    private double pan;
    private double tilt;

    public double getPan(){
        return pan;
    }

    public double getTilt(){
        return tilt;
    }

    public void mouseMoved(MouseEvent e){
        pan = e.getX() / GameScreen.WIDTH * 180 - 90;
        tilt = e.getY() / GameScreen.HEIGHT * 90;
    }

    public void mouseDragged(MouseEvent e){
    }
}

Unfortunatelly, it looks that the getX and getY methods always return 0. (Pan is -90 and tilt is 0. Always.) What am I supposed to do? Is there constructor missing? How should it look if so? Thanks all for help.

Vit Henych
  • 111
  • 4
  • That is because you are dividing integers when you do `e.getX() / GameScreen.WIDTH`. That calculation will be 0 is `e.getX()` returns a number less than `GameScreen.WIDTH`. – Jesper Dec 07 '16 at 12:30
  • Thanks. It solved my problem. I should have thought of that. – Vit Henych Dec 07 '16 at 12:44

0 Answers0