0

Maybe I'm crazy, but I want to incorporate some logic I learned in my XNA Game Development class into android/java game programming. For example, I'm using Point rather than Vector2 to manipulate the location of graphics programmatically, and adjust graphic size based on an Android device's dpi and orientation.

I thought finding the center of the graphic would be a great asset when laying out the GUI and determining which folder (hdpi, ldpi, or mdpi) the graphics should be taken from. Unfortunately, Eclipse seems to have a problem with the plus sign. This is interesting because I am trying to perform a simple mathematical operation.:

The operator + is undefined for the argument type(s) android.graphics.Point, android.graphics.Point

//Java  code was both copied & pasted as well as written to make sure Visual Studio
public Point Center()
{
    return new Point(_frameWidth/2, _frameHeight/2)+_location;
}

//Original XNA code
//Determines the coordinates of a sprite's center
public Vector2 Center
{
    get
    {
        return _location + new Vector2(_frameWidth / 2, _frameHeight / 2);
    }
}
Shortstuff81000
  • 469
  • 1
  • 3
  • 15

2 Answers2

2

And it's true; you can't add arbitrary types in Java--the error message is correct.

You get some syntactic sugar for string concatenation, and that's it--anything else you add had better be a primitive or mappable object wrapper for the same (e.g., I'm looking at you, BigDecimal, no +).

Whatever _location is, if you're trying to add it to a Point or Vector2, it won't work using +. You may add individual components, use vector/point methods or utility libraries, etc.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
0

It's not an issue with Eclipse; Java does not have operator overloading (besides for a very small set of cases).

Why doesn't Java offer operator overloading?

An interesting article on this:

http://java.dzone.com/articles/why-java-doesnt-need-operator

Community
  • 1
  • 1
  • Problem solved - I added the X and Y values of the two points in seperate statements. Getting out of c# and into Java will be an interesting experience, but I'm ready for the challenge. Thanks! public Point Center() { int x = _frameWidth/2+_location.x; int y = _frameHeight/2+_location.y; return new Point(x, y); } – Shortstuff81000 Feb 15 '12 at 13:37