例如,我有一个多边形,我需要用特定的 RGB 填充它。我该怎么做?我尝试将形状转换为图像,但无法使用 BufferedImage 中的 setRGB 方法设置像素(像素颜色没有改变!):
...
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();
...
此外,图像背景为黑色,并且设置的像素始终为白色。
最佳答案
调用Graphics.setClip(Shape)
,然后进行绘图操作。请参阅此处 an example .
关于java - 如何使用特定的 RGB 设置形状中的像素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6436886/