title: Java Graphics.fillPolygon: How to also render right and bottom edges?
body: <p>When drawing polygons, Java2D leaves off the right and bottom edges.  I understand why this is done.  However, I would like to draw something that includes those edges.  One thing that occurred to me was to follow <code>fillPolygon</code> with <code>drawPolygon</code> with the same coordinates, but this appears to leave a gap.  (See the little triangular image at the bottom.)  There are two possibilities, but I can't tell which.  To enable antialiasing, I'm doing this:</p>
  
  <pre><code>renderHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
  renderHints.put(RenderingHints.KEY_RENDERING,
                  RenderingHints.VALUE_RENDER_QUALITY);
  g2d.setRenderingHints(renderHints);
  </code></pre>
  
  <p>One possibility is that the antialiasing is not being done on the alpha channel, so the gap is caused by overdraw.  In that case, if the alpha channel were what was being antialiased, the edges would abut properly.  The other possibility is that there is just a gap here.</p>
  
  <p>How can I fix this?</p>
  
  <p>Also, I'm not sure, but it appears that the polygon outline may actually be TOO BIG.  That is, it may be going further out than the right and bottom edges that I want to include.</p>
  
  <p>Thanks.</p>
  
  <p><img src="https://i.stack.imgur.com/9bo9A.png" alt="enter image description here"></p>
  
  <p>-- UPDATE --</p>
  
  <p>Based on a very nice suggestion by Hovercraft Full of Eels, I have made a compilable example:</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Graphics2D;
  import java.awt.RenderingHints;
  import java.awt.image.BufferedImage;
  
  import javax.swing.ImageIcon;
  import javax.swing.JLabel;
  import javax.swing.JOptionPane;
  
  public class polygon {
     private static final int WIDTH = 20;
  
     public static void main(String[] args) {
        BufferedImage img = new BufferedImage(WIDTH, WIDTH, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = img.createGraphics();
        int[] xPoints = {WIDTH / 3, (2*WIDTH) / 3, WIDTH / 3};
        int[] yPoints = {0, WIDTH / 2, WIDTH};
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(Color.green);
        g2.drawLine(0, WIDTH-1, WIDTH, WIDTH-1);
        g2.drawLine(0, 0, WIDTH, 0);
        g2.drawLine(WIDTH/3, 0, WIDTH/3, WIDTH);
        g2.drawLine((2*WIDTH/3), 0, (2*WIDTH/3), WIDTH);
        g2.setColor(Color.black);
        g2.drawPolygon(xPoints, yPoints, xPoints.length);
        g2.setColor(Color.black);
        g2.fillPolygon(xPoints, yPoints, xPoints.length);
        g2.dispose();
  
        ImageIcon icon = new ImageIcon(img);
        JLabel label = new JLabel(icon);
  
        JOptionPane.showMessageDialog(null, label);
     }
  }
  </code></pre>
  
  <p>If you leave the filled polygon red, you get the image below (zoomed by 500%), which shows that the polygon does not extend all the way to the right edge.  That is, the vertical green line is corresponds to <code>x=(2*WIDTH)/2</code>, and although the red polygon includes that coordinate, it does not paint any pixels there.</p>
  
  <p><img src="https://i.stack.imgur.com/ObSzO.png" alt="enter image description here"></p>
  
  <p>To see the gap problem, I changed <code>red</code> in the program to <code>black</code>.  In this image, you can see a subtle gap on the lower right side, where the outline drawn by <code>drawPolygon</code> does not quite meet up with what was drawn with <code>fillPolygon</code>.</p>
  
  <p><img src="https://i.stack.imgur.com/UyFTZ.png" alt="enter image description here"></p>