0

For example, I have a polygon and I need to fill it with the specific RGB. How can I do it? I tried to convert shape to image, but then I can't set a pixel with setRGB method from BufferedImage(pixel color wasn't changing!):

...
    Rectangle2D r = pgnProjection.getBounds();
    BufferedImage rectBuffIm = new BufferedImage(r.getBounds().width, r.getBounds().height, 
                                        BufferedImage.TYPE_BYTE_BINARY);
    for(int i = rectBuffIm.getWidth()/2, j = rectBuffIm.getHeight()/2; rectBuffIm.getWidth()>i && rectBuffIm.getHeight()>j; j++, i++)
        rectBuffIm.setRGB(i, j, rgb);

    Graphics2D gr2D = rectBuffIm.createGraphics();
    gr2D.translate(-pgnProjection.getBounds().x, -pgnProjection.getBounds().y);
    gr2D.draw(pgnProjection);
    gr2D.dispose();
...

Also, image background was black, and set pixels were always white.

ilya8891
  • 127
  • 1
  • 1
  • 12

2 Answers2

1

Call Graphics.setClip(Shape) followed by the drawing operations. See here for an example.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

Graphics.fillPolygon()

Suraj Chandran
  • 24,433
  • 12
  • 63
  • 94