File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Losing double values in a Point[ ] array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Losing double values in a Point[ ] array " Watch "Losing double values in a Point[ ] array " New topic
Author

Losing double values in a Point[ ] array

Ian Jackson
Greenhorn

Joined: Nov 30, 2007
Posts: 2
I've been at this one all afternoon. No doubt as a beginner I'm overlooking the obvious:

I'm adding double values to an array of type Point:


The println shows a nice set of randomized double values.

Then I try to retrieve them to do some processing:

Now the println shows me my lovely doubles are now all either 1.0 or 0.0! Why?

I have made sure I'm not trying to initialise the Point[] array with double values (it only initialises with integers), hence the use of the .setLocation() function.

Please help...
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Welcome to JavaRanch!

I assume that the Point type you are using is java.awt.Point. If so, the x and y coordinates are stored as ints.

Although you can provide doubles to the overloaded setLocation method, these values are "rounded to integer values" (to quote the API). So when you get them back using getX and getY, you will get double representations of the rounded ints.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
If you want a Point that uses doubles, try using java.awt.geom.Point2D.Double instead.
[ November 30, 2007: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Ian Jackson
Greenhorn

Joined: Nov 30, 2007
Posts: 2
Many thanks to you both. I will try the class you suggest.
 
jQuery in Action, 2nd edition
 
subject: Losing double values in a Point[ ] array