Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does java.awt.Point provide methods to set and get doubles but store x and y as int's?

As you can see in the Oracle Documentation for java.awt.Point, x and y are stored as int. However, getX and getY return double. While there is a setLocation method that takes 2 double types, there is no constructor that does. Furthermore, the double gets truncated to an int internally anyway.

Is there a good reason for this? You might avoid a cast on setLocation by having a method that takes double types, but you have the opposite problem when you call getX and getY. There's also a misrepresentation of the precision of the Point class by returning a double from getX and getY.

like image 279
Luke Avatar asked Mar 05 '12 16:03

Luke


1 Answers

Because it extends from java.awt.geom.Point2D which is used in Graphics2D abstract class. It was implemented this way for compatibility reason since before this, java only supported java.awt.Graphics where all methods are int based.

like image 128
Konstantin Solomatov Avatar answered Oct 03 '22 13:10

Konstantin Solomatov