-1

I have a problem. I'd change double on Integer but I have this error:

Cannot invoke intValue() on the primitive type double

Thanks for help!

import java.awt.AWTException;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Robot;

public class Clicker {

    void SetX(int xCoordinate) {
        this.xCoordinate = xCoordinate;
    }

    void SetY(int yCoordinate) {
        this.yCoordinate = yCoordinate;
    }

    static int xCoordinate= 30;
    static int yCoordinate = 40;

    public static void main (String[] args) {

        PointerInfo inf = MouseInfo.getPointerInfo();
        Point p = inf.getLocation();

        System.out.println(p.getX());

        int xzCoordinate = p.getX().intValue();
    }
}
dur
  • 15,689
  • 25
  • 79
  • 125
Brade
  • 17
  • 7
  • Or this one http://stackoverflow.com/questions/8631652/strange-behaviour-with-object-intvalue (maybe better) – Tunaki Apr 20 '16 at 20:01

3 Answers3

0

Use: Math.floor(p.getX()) Math.ceil(p.getX()) or Math.round(p.getX()) depending on which one suits you.

Zircon
  • 4,677
  • 15
  • 32
0

There is no cast in your code.

getX() seems to return a primitive double and the compiler tells you that you can't invoke intValue() on a double.

Therefore use a cast

int xzCoordinate = (int)p.getX();

or one of the rounding methods provided by Math (see answer of @Zircon)

wero
  • 32,544
  • 3
  • 59
  • 84
0

That is not how to convert a primitive double to an int. Java's Point objects store their values as ints, but for some reason the getter methods return doubles. You can cast getX() to an int

int xzCoordinate = (int) p.getX();

Or you can just use the field, which is public.

int xzCoordinate = p.x;
rgettman
  • 176,041
  • 30
  • 275
  • 357