Search code examples
javaswinggraphics2d

Drawing a 8x8 rectangle becomes a 9x9 rectangle on screen?


g2.fill(new Rectangle2D.Double(0, 0, 8, 8));

This nicely fills a 8x8 rectangle.

But strange things happen when I try to draw a 8x8 rectangle border:

g2.draw(new Rectangle2D.Double(0, 0, 8,8));

This draws a rectangle 9x9 rectangle.

enter image description here

But I specified that it should be 8 width and 8 height.

I have a default stroke width 1.

Am I overlooking something? Maybe a better question is: can I turn this off so that I get a 8x8 rectangle when calling draw?


Solution

  • The documentation of the Graphics2D class, in the section titled "Rendering Compatibility Issues", says:

    The JDK(tm) 1.1 rendering model is based on a pixelization model that specifies that coordinates are infinitely thin, lying between the pixels. Drawing operations are performed using a one-pixel wide pen that fills the pixel below and to the right of the anchor point on the path. The JDK 1.1 rendering model is consistent with the capabilities of most of the existing class of platform renderers that need to resolve integer coordinates to a discrete pen that must fall completely on a specified number of pixels.

    It goes on to say:

    Java 2D API maintains compatibility with JDK 1.1 rendering behavior, such that legacy operations and existing renderer behavior is unchanged under Java 2D API. Legacy methods that map onto general draw and fill methods are defined,

    So basically, this means that if Java renders a line from (0,0) to (0,8), it will draw it in the pixels under the 0 coordinate. The line from (0,8) to (8,8) will go in the pixl to the right of the 8 x coordinate.

    0┌───┬───┬───┬───┬───┬───┬───┬───┬───┐
     │ █ │ █ │ █ │ █ │ █ │ █ │ █ │ █ │ █ │
    1├───┼───┼───┼───┼───┼───┼───┼───┼───┤
     │ █ │   │   │   │   │   │   │   │ █ │
    2├───┼───┼───┼───┼───┼───┼───┼───┼───┤
     │ █ │   │   │   │   │   │   │   │ █ │
    3├───┼───┼───┼───┼───┼───┼───┼───┼───┤
     │ █ │   │   │   │   │   │   │   │ █ │
    4├───┼───┼───┼───┼───┼───┼───┼───┼───┤
     │ █ │   │   │   │   │   │   │   │ █ │
    5├───┼───┼───┼───┼───┼───┼───┼───┼───┤
     │ █ │   │   │   │   │   │   │   │ █ │
    6├───┼───┼───┼───┼───┼───┼───┼───┼───┤
     │ █ │   │   │   │   │   │   │   │ █ │
    7├───┼───┼───┼───┼───┼───┼───┼───┼───┤
     │ █ │   │   │   │   │   │   │   │ █ │
    8├───┼───┼───┼───┼───┼───┼───┼───┼───┤
     │ █ │ █ │ █ │ █ │ █ │ █ │ █ │ █ │ █ │
     └───┴───┴───┴───┴───┴───┴───┴───┴───┘
     0   1   2   3   4   5   6   7   8      
    

    Thus, using draw around an 8x8 rectangle draws two lines inside the rectangle, and two outside it.