Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中将形状对象转换为图像对象_Java_Image_Shape - Fatal编程技术网

在Java中将形状对象转换为图像对象

在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_

如何将矩形2D.Double等形状对象转换为图像对象


这样,我就可以使用形状对象来替换鼠标或鼠标。

缓冲图像中执行操作,如图所示。

您必须创建一个图像对象,该对象在正确的位置包含正确的像素

一种方法是这样的:

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;
}
不过,您可能希望使用另一种颜色模型,使您的形状以透明背景显示,而不是黑白或其他颜色