I would like to load an image and control the placement/movement in a JPanel with precision (float). After a gazzilion searches and paging through books it seems that I can only position an image using (int).

Is there a trick I can use... Like somehow attaching the image to a rect, or another image method I can use?

I'm writing a simple app to simulate gravity on an object for a Physics project

The problem occurs in the Maths when the ball is at the top of the fall... because I have to cast to (int) for movement, it stalls until the value is greater than 0.5 or less than -0.5... So it just hangs there.

You want to draw an image at sub-pixel locations? I think Project SceneGraph has that built-in. I'm not sure about JavaFX, I saw one demo (the puzzle demo) it looked like some pixel-snapping was going on.For straight Java2D, this method that might work by using transforms:Graphics2D#drawImage(Image, AffineTransform, ImageObserver)You may have to set up some RenderingHints to get it to work correctly (bilinear filtering, etc)

All the masses, positions, and velocities of the planets are doubles. Then when I draw it I just say g.fillOval(int)x,(int)y,(int)radius*2,(int)radius*2).

There is no reason to draw at a sub-pixel level. Your monitor can only show things on a pixel-by-pixel basis; nothing can be drawn in between two pixels. SimonH is right - as long as you keep everything stored as a float or double and then merely draw as int, it will look fine.

I think his problem is the visual imperfection of very small position changes, so the ball "hops" to the next pixel from time to time. Either way is to accept this limitation or use subpixel placement, that recalculates the real pixels by multisampling.

There is no reason to draw at a sub-pixel level. Your monitor can only show things on a pixel-by-pixel basis; nothing can be drawn in between two pixels.

Well.... not exactly. Drawing at sub-pixel level makes anti-aliased images appear to have smoother animation when moving slowly. It can be accomplished with bilinear interpolation. Of course, it makes pixel-art look like crap.

To answer the original question: use affine transform to place the image with high precision. Also make sure to set bilinear rendering hint (the default is nearest neighbor). But be aware that with earlier jdks (pre-1.6) performance will suck.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org