开发者

Creating and rendering an image dynamically with transparent background for use with drawImage

开发者 https://www.devze.com 2022-12-29 02:54 出处:网络
Could someone provide an example of h开发者_StackOverflow社区ow to dynamically create an image in Java, draw lines et cetera on it, and then draw the image so that areas not painted will remain transp

Could someone provide an example of h开发者_StackOverflow社区ow to dynamically create an image in Java, draw lines et cetera on it, and then draw the image so that areas not painted will remain transparent in the drawing process?


One could use a BufferedImage with an image type that supports transparency such as BufferedImage.TYPE_INT_ARGB:

BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);

One can draw on the BufferedImage by calling BufferedImage.createGraphics to obtain a Graphics2D object, then perform some drawing:

BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);

Graphics2D g = img.createGraphics();
g.drawLine(0, 0, 10, 10);  // draw a line.
g.dispose(); 

Then, since BufferedImage is a subclass of Image that can be used to draw onto another Image using one of the Graphics.drawImage that accepts an Image.

0

精彩评论

暂无评论...
验证码 换一张
取 消