开发者

Convert Shape object to Image object in Java

How do I convert a Shape object like Rectangle2D.Double to an Image object?

This way I can use a Shape object for a mous开发者_如何学运维ecursor replacement.


Do draw(shape) in a BufferedImage, as shown here.


You'll have to create an image object which contains the right pixels at the right locations.

One way would be something like this:

public Image makeImage(Shape s) {
    Rectangle r = s.getBounds();
    Image image = new BufferedImage(r.width, r.height, BufferedImage.TYPE_BYTE_BINARY);
    Graphics2D gr = image.createGraphics();
    // move the shape in the region of the image
    gr.translate(-r.x, -r.y);
    gr.draw(s);
    gr.dispose();
    return image;
}

You may want to use another color model, though, to have your shape show up with transparent background, instead of black-on-white or otherwise around.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜