This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
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

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Losing double values in a Point[ ] array " Watch "Losing double values in a Point[ ] array " New topic

Losing double values in a Point[ ] array

Ian Jackson

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

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
Jim Yingst

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

Joined: Nov 30, 2007
Posts: 2
Many thanks to you both. I will try the class you suggest.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Losing double values in a Point[ ] array
It's not a secret anymore!