Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Losing double values in a Point[ ] array

 
Ian Jackson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want a Point that uses doubles, try using java.awt.geom.Point2D.Double instead.
[ November 30, 2007: Message edited by: Jim Yingst ]
 
Ian Jackson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks to you both. I will try the class you suggest.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic