Rectangle2D.DoubleのようなShapeオブジェクトをImageオブジェクトに変換するにはどうすればよいですか?
このようにして、マウスカーソルの代わりにShapeオブジェクトを使用できます。
Do draw(shape)
in a BufferedImage
, as shown here.
適切な位置に適切なピクセルを含む画像オブジェクトを作成する必要があります。
1 つの方法は次のようになります。
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;
}
ただし、別のカラー モデルを使用して、白地に黒などではなく、透明な背景でシェイプを表示することもできます。