Name: jk109818 Date: 06/25/2002
FULL PRODUCT VERSION :
java version "1.3.1_03"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1_03-b03)
Java HotSpot(TM) Client VM (build 1.3.1_03-b03, mixed mode)
FULL OPERATING SYSTEM VERSION :
4NT 3.01A Windows NT 5.00
A DESCRIPTION OF THE PROBLEM :
Trying to line up labels drawn with Graphics.drawString(
String, int x, int y) with lines drawn with
Graphics.drawLine(int x, int y, int, int) doesn't work for
large x & y values (i.e. greater than 2^24). Looking at
sun.java2d.SunGraphics2D.drawString shows that the int
parameters are being cast to float and losing precision.
Graphics.drawString( String, int, int) should not cast
arguments to float. If range is an issue, they should
either be cast to double or long.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Look at source for sun.java2d.SunGraphics2D.drawString(
String, int, int)
2. Note that arguments are cast to float
This bug can be reproduced always.
(Review ID: 153922)
Here's a sample that shows the problem. The numbers should centered under
the ticks, but as one moves the scroll handle to the right (past 1/5 or so)
the numbers are no longer precisely centered under the ticks. With the
scroll handle at the far right, labels are far from ticks and some labels
are superimposed because they are "clumping" around values that can be
exactly represented by float.
This problem occurs because drawString casts int x and y parameters to
float, and float numbers only have 24 manitssa bits compared to 32 bits for
ints, so precision is lost with large numbers, making it impossible to
correctly label lines drawn with drawLine.
Note that the problem is still noticeable if one comments out the line to
center the label under a tick.
I made 2 changes to make the problem more apparent:
- Changed labels to be left justified under tick, rather than centered.
It's easier to see now when a tick doesn't line up with the start of a
label. (Left the code to center it as a comment.)
- Set the scrollbar's unit increment to be the same as the tick increment.
Now click-and-hold on the scroll bar arrow shows that the ticks are evenly
spaced, but the labels aren't (once you get past 1/8 or so of the scroll
Here's is the updated file:
(See attached file: DrawStringTest.java)