【问题标题】:How I can set pixel in the Shape with the specific RGB?如何使用特定的 RGB 在形状中设置像素?
【发布时间】:2011-06-22 16:41:01
【问题描述】:

例如,我有一个多边形,我需要用特定的 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();
...

另外,图像背景是黑色的,设置的像素总是白色的。

【问题讨论】:

    标签: java set rgb shape


    【解决方案1】:

    调用Graphics.setClip(Shape),然后进行绘图操作。在此处查看an example

    【讨论】:

      【解决方案2】:

      【讨论】:

      • 我必须填充不同颜色的像素而不是一种颜色。
      猜你喜欢
      • 2016-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-04
      • 2017-06-08
      • 1970-01-01
      • 1970-01-01
      • 2012-08-02
      相关资源
      最近更新 更多