在java中创建alpha图像(creating alpha images in java)

编程入门 行业动态 更新时间:2024-10-22 20:39:31
在java中创建alpha图像(creating alpha images in java)

我即将开始研究一个项目,该项目将生成一些带有alpha(渐变)的PNG,并希望在java中以编程方式绘制它们。

对于(一个简单的)示例,我想绘制一个框,添加一个阴影,然后将其保存到PNG文件,然后可以将其覆盖在其他图形上。

这可能与标准的JRE系统库有关吗? 哪些库会使这种操作变得简单?

谢谢。

I'm about to start work on a project which will generate some PNGs with alpha (gradients) and hoping to draw them programmatically within java.

For (a simple) example, I would like to draw a box, add a drop-shadow and then save this to a PNG file which can then be overlayed on some other graphic.

Is this possible with the standard JRE system libraries? which libraries would make this kind of operation simple?

thanks.

最满意答案

这可能与标准的JRE系统库有关吗?

是的,它是可能的并且非常简单。 下面的代码生成此图像(透明png):

截图

public static void main(String[] args) throws IOException { int x = 50, y = 50, w = 300, h = 200; // draw the "shadow" BufferedImage img = new BufferedImage(400, 300, BufferedImage.TYPE_INT_ARGB); Graphics g = img.getGraphics(); g.setColor(Color.BLACK); g.fillRect(x + 10, y + 10, w, h); // blur the shadow BufferedImageOp op = getBlurredOp(); img = op.filter(img, null); // draw the box g = img.getGraphics(); g.setColor(Color.RED); g.fillRect(x, y, w, h); // write it to disk ImageIO.write(img, "png", new File("test.png")); } private static BufferedImageOp getBlurredOp() { float[] matrix = new float[400]; for (int i = 0; i < 400; i++) matrix[i] = 1.0f/400.0f; return new ConvolveOp(new Kernel(20, 20, matrix), ConvolveOp.EDGE_NO_OP, null); }

哪些库会使这种操作变得简单?

我会说这取决于你的其他用例。 对于像盒子和椭圆形这样的简单形状,我会选择上面的解决方案,不需要库。

Is this possible with the standard JRE system libraries?

Yes, it is possible and is pretty simple aswell. The code below produces this image (transparent png):

screenshot

public static void main(String[] args) throws IOException { int x = 50, y = 50, w = 300, h = 200; // draw the "shadow" BufferedImage img = new BufferedImage(400, 300, BufferedImage.TYPE_INT_ARGB); Graphics g = img.getGraphics(); g.setColor(Color.BLACK); g.fillRect(x + 10, y + 10, w, h); // blur the shadow BufferedImageOp op = getBlurredOp(); img = op.filter(img, null); // draw the box g = img.getGraphics(); g.setColor(Color.RED); g.fillRect(x, y, w, h); // write it to disk ImageIO.write(img, "png", new File("test.png")); } private static BufferedImageOp getBlurredOp() { float[] matrix = new float[400]; for (int i = 0; i < 400; i++) matrix[i] = 1.0f/400.0f; return new ConvolveOp(new Kernel(20, 20, matrix), ConvolveOp.EDGE_NO_OP, null); }

Which libraries would make this kind of operation simple?

I would say that it depends on your other use cases. For simple shapes like boxes and ovals I would go for the solution above, no library is needed.

更多推荐

本文发布于:2023-07-30 23:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340349.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   alpha   java   creating   images

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!