• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Losing double values in a Point[ ] array

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many thanks to you both. I will try the class you suggest.
 
reply
    Bookmark Topic Watch Topic
  • New Topic