在Java中将形状对象转换为图像对象
如何将矩形2D.Double等形状对象转换为图像对象在Java中将形状对象转换为图像对象,java,image,shape,Java,Image,Shape,如何将矩形2D.Double等形状对象转换为图像对象 这样,我就可以使用形状对象来替换鼠标或鼠标。在缓冲图像中执行操作,如图所示。您必须创建一个图像对象,该对象在正确的位置包含正确的像素 一种方法是这样的: public Image makeImage(Shape s) { Rectangle r = s.getBounds(); Image image = new BufferedImage(r.width, r.height, BufferedImage.TYPE_BYTE_
这样,我就可以使用形状对象来替换鼠标或鼠标。在
缓冲图像中执行操作,如图所示。您必须创建一个图像对象,该对象在正确的位置包含正确的像素
一种方法是这样的:
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;
}
不过,您可能希望使用另一种颜色模型,使您的形状以透明背景显示,而不是黑白或其他颜色