Search code examples
javaswinggraphics2d

Show a 0.01 difference in rectangle position?


Today i needed to visualize a large data set on screen by using doubles or floats to position elements. When using pixels i can simply draw a rectangle with the following code:

gimg.draw(new Rectangle2D.Double(1, 1, 50, 50));

But when i try drawing two rectangles with the following code a problem appears.

gimg.setColor(Color.white);
gimg.draw(new Rectangle2D.Double(1.0, 1.0, 50, 50));
gimg.setColor(Color.gray);
gimg.draw(new Rectangle2D.Double(1.1, 1.1, 50.1, 50.1));

The 2 rectangles are drawn over each other, in other words, you can't see a white and a gray rectangle because the gray one is drawn over the white one.

I'm using a 0.1 increment. After a bit of testing i seem to be able to view partial rectangles when using a 0.5 increment. The problem here is only i also need to make rectangles visible which only differ 0.01 in position.

I suspect i'm missing something really important here. Can anybody offer me a helping hand?


Solution

  • You should consider using a 0.1 : 1 scale instead, because pixels can't be divided into tenths (or any fraction for that matter) without losing accuracy / presentability. Imagine the tiny pixels on your screen - you're trying to divide a 1px line into 10 distinguishable sections. That's not possible, unfortunately.