如何使用特定的 RGB 设置形状中的像素?
例如,我有一个多边形,我需要用特定的 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();
...
此外,图像背景为黑色,设置像素始终为白色。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
调用 Graphics.setClip(Shape) ,然后进行绘图操作。请参阅此处的示例。
Call
Graphics.setClip(Shape)
followed by the drawing operations. See here for an example.图形.fillPolygon()
Graphics.fillPolygon()