You mentioned that you draw the polygon. In this case, you have to take into account that you are drawing with the line just between (!) the pixels. And if the line has a width of 1.0 (with the default stroke), this will cause 5x5 pixels to be touched. In contrast to that, when you really fill the polygon, then it should really occupy only 4x4 pixels.
In any case, the result computed by the Area
should be in line with the definition of "insideness" in the Shape class.
The following image shows the "pixels" enlarged: The red pixels in the center are the pixels that are contained in a rectangular polygon (position (2,2), size (4,4)). The blue pixels are the pixels that are contained in the Area
from which the polygon was subtracted. IF there were any pixels contained in both shapes, then these would be painted in magenta.
The green, semi-transparent "overlay" shows what drawing the polygon actually would produce (if the display was capable to show this): The lines are at the border between the pixels, but with a width of 1.0.

The code:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ContainsTest
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new ContainsTestPanel());
f.setSize(500, 500);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
class ContainsTestPanel extends JPanel
{
@Override
protected void paintComponent(Graphics gr)
{
super.paintComponent(gr);
Graphics2D g = (Graphics2D)gr;
g.setColor(Color.WHITE);
g.fillRect(0,0,getWidth(), getHeight());
int[] xpoints={2,6,6,2};
int[] ypoints={2,2,6,6};
int npoints=4;
Polygon p=new Polygon(xpoints,ypoints,npoints);
Area visible = new Area(p);
Area blackArea = new Area(new Rectangle(0,0,8,8));
blackArea.subtract(visible);
int size = 50;
for (int x=0; x<8; x++)
{
for (int y=0; y<8; y++)
{
Point2D point = new Point(x,y);
boolean inPoly = p.contains(point);
boolean inArea = blackArea.contains(point);
System.out.println(x+" "+y+" inPoly: "+inPoly+" inArea "+inArea);
if (!inPoly && !inArea)
{
g.setColor(Color.GRAY);
}
if (inPoly && !inArea)
{
g.setColor(Color.RED);
}
if (!inPoly && inArea)
{
g.setColor(Color.BLUE);
}
if (inPoly && inArea)
{
g.setColor(Color.MAGENTA);
}
int rx = x * size;
int ry = y * size;
g.fillRect(rx, ry, size, size);
g.setColor(Color.BLACK);
g.drawString(x+","+y, rx, ry);
g.drawRect(rx, ry, size, size);
}
}
//g.setStroke(new BasicStroke(1.0f/size));
AffineTransform at = g.getTransform();
g.scale(size, size);
g.setColor(new Color(0,255,0,128));
g.draw(p);
g.setTransform(at);
}
}
If you only want to handle pixels in rectangles, there might be a "manual" solution for what you want to achieve.