如何创建循环bufferedimage,而不是使用Graphics在Java中创建矩形区域 [英] How to create a circular bufferedimage rather than creating a rectangular one in Java using Graphics

查看:174
本文介绍了如何创建循环bufferedimage,而不是使用Graphics在Java中创建矩形区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个bufferedimage
(圆形类型),但我只能创建矩形类型。然后我想用一些配置在bufferedimage中创建一个椭圆形,最后我想在圆形形状中绘制一个矩形图标,而不是在矩形的bufferedimage上。
现在我可以执行以下操作了:

I need to create a bufferedimage (circular type) but I am able to create only rectangular type. Then I want to create a oval inside the bufferedimage with some configurations and finally i want to draw a rectangular icon inside the circular shape which should be inserted on the circular shape and not on the rectangular bufferedimage. Now i am able to do the following

BufferedImage img = "SomeImage.png";
BufferedImage bfImage = new BufferedImage(200,200,BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics       = bfImage.createGraphics();
graphics.fillOval(0, 0, 200, 200); 
graphics.drawImage(img, 0, 0, null);

(这会在bfImage对象内创建一个圆形的椭圆)
现在我需要绘制 img,其形状为100 * 100大小的矩形。
我可以使用
绘制当我这样做时,我的最终图像正在绘制在矩形BfImage中,我不想要。我想要在圆形椭圆中绘制图像img不应该超出圆形椭圆形的边界。
简而言之,不是在矩形bfImage上绘制最终图像,我可以在圆形bfImage上直接绘制图像。
任何使用图形在Java2D中执行此操作的逻辑。

(This creates a circular oval inside the bfImage object) Now i need to draw "img" which is rectangular in shape of say 100*100 size. This i can draw using When i am doing this my final image is getting drawn in the Rectangular BfImage, which i dont want.I want the image "img" to be drawn in the circular oval and it should not come outside the boundaries of the circular oval shape. In short instead of drawing my final image on the rectangular bfImage can i have a circular bfImage on which i can directly draw my image. Any logic to do this in Java2D using graphics.

推荐答案

我从来没有遇到过圆形图像二维数组的意义是不是的矩形形式。如果你所关心的是圆圈外的像素不可见,只需将这些像素的alpha设置为0即可。一个简单的方法是首先用ARGB(0,0,0,0)填充整个矩形图像,然后绘制任何你想要的。

I've never encountered a "circular image" in the sense of a two dimensional array that is not in rectangular form. If all you are concerned about is that the pixels outside the circle are not visible, just set the alpha to 0 for those pixels. An easy way to do this is to fill the entire rectangular image with ARGB(0,0,0,0) first and then draw whatever else you want.

另外,而不是如果你打算把这个缓冲区保存为图像文件,你必须确保你导出/保存为PNG或TIFF格式,支持透明度。

Also, not that if you intent to persist this buffer as an image file, you must make sure that you export/save to a format like PNG or TIFF that supports transparency.

这篇关于如何创建循环bufferedimage,而不是使用Graphics在Java中创建矩形区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆