Documentation

The Java™ Tutorials
Hide TOC
Compositing Graphics合成图形
Trail: 2D Graphics
Lesson: Advanced Topics in Java2D

Compositing Graphics合成图形

The AlphaComposite class encapsulates various compositing styles, which determine how overlapping objects are rendered.AlphaComposite类封装了各种合成样式,这些样式决定了重叠对象的渲染方式。An AlphaComposite can also have an alpha value that specifies the degree of transparency: alpha = 1.0 is totally opaque, alpha = 0.0 totally transparent (clear).AlphaComposite还可以具有指定透明度的alpha值:alpha = 1.0表示完全不透明,alpha = 0.0表示完全透明(透明)。AlphaComposite supports most of the standard Porter-Duff compositing rules shown in the following table.AlphaComposite支持下表所示的大多数标准Porter-Duff合成规则。

Compositing Rule合成规则 Description描述
Source-over (SRC_OVER)
Source-over compositing
If pixels in the object being rendered (the source) have the same location as previously rendered pixels (the destination), the source pixels are rendered over the destination pixels.如果正在渲染的对象(源)中的像素与先前渲染的像素(目标)具有相同的位置,则源像素将在目标像素上渲染。
Source-in (SRC_IN)
Source-in compositing
If pixels in the source and the destination overlap, only the source pixels in the overlapping area are rendered.如果源和目标中的像素重叠,则仅渲染重叠区域中的源像素。
Source-out (SRC_OUT)
Source-out compositing
If pixels in the source and the destination overlap, only the source pixels outside of the overlapping area are rendered.如果源和目标中的像素重叠,则仅渲染重叠区域之外的源像素。The pixels in the overlapping area are cleared.重叠区域中的像素被清除。
Destination-over (DST_OVER)
Destination-over compositing
If pixels in the source and the destination overlap, only the source pixels outside of the overlapping area are rendered.如果源和目标中的像素重叠,则仅渲染重叠区域之外的源像素。The pixels in the overlapping area are not changed.重叠区域中的像素不变。
Destination-in (DST_IN)
Destination-in compositing
If pixels in the source and the destination overlap, the alpha from the source is applied to the destination pixels in the overlapping area.如果源和目标中的像素重叠,则源的alpha将应用于重叠区域中的目标像素。If the alpha = 1.0, the pixels in the overlapping area are unchanged; if the alpha is 0.0, pixels in the overlapping area are cleared.如果alpha=1.0,则重叠区域中的像素不变;如果alpha为0.0,则重叠区域中的像素将被清除。
Destination-out (DST_OUT)
Destination-out compositing
If pixels in the source and the destination overlap, the alpha from the source is applied to the destination pixels in the overlapping area.如果源和目标中的像素重叠,则源的alpha将应用于重叠区域中的目标像素。If the alpha = 1.0, the pixels in the overlapping area are cleared; if the alpha is 0.0, the pixels in the overlapping area are unchanged.如果alpha=1.0,则清除重叠区域中的像素;如果alpha为0.0,则重叠区域中的像素不变。
Clear (CLEAR)
Clear with overlap compositing
If the pixels in the source and the destination overlap, the pixels in the overlapping area are cleared.如果源和目标中的像素重叠,则重叠区域中的像素被清除。

To change the compositing style used by the Graphics2D class, create an AlphaComposite object and pass it into the setComposite method.要更改Graphics2D类使用的合成样式,请创建AlphaComposite对象并将其传递到setComposite方法中。

Example: Composite示例:合成

This program illustrates the effects of various compositing style and alpha combinations.该程序演示了各种合成样式和alpha组合的效果。


Note:注意  If you don't see the applet running, you need to install at least the Java SE Development Kit (JDK) 7 release.如果您没有看到小程序正在运行,则至少需要安装JavaSE开发工具包(JDK)7版本

Composite.java. contains the full code for this applet.包含此小程序的完整代码。

A new AlphaComposite object ac is constructed by calling AlphaComposite.getInstance and specifying the desired compositing rule.通过调用AlphaComposite.getInstance并指定所需的合成规则来构造新的AlphaComposite对象ac

AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC);

When a different compositing rule or alpha value is selected, AlphaComposite.getInstance is called again, and the new AlphaComposite is assigned to ac.当选择不同的合成规则或alpha值时,将再次调用AlphaComposite.getInstance,并将新的AlphaComposite分配给acThe selected alpha is applied in addition to the per-pixel alpha value and is passed as a second parameter to AlphaComposite.getInstance.除了每像素alpha值之外,还将应用选定的alpha,并将其作为第二个参数传递给AlphaComposite.getInstance

ac = AlphaComposite.getInstance(getRule(rule), alpha);

The composite attribute is modified by passing the AlphaComposite object to Graphics 2D setComposite.通过将AlphaComposite对象传递给Graphics 2D setComposite,可以修改复合属性。The objects are rendered into a BufferedImage and are later copied to the screen, so the composite attribute is set on the Graphics2D context for the BufferedImage:对象渲染到BuffereImage中,然后复制到屏幕上,因此在Graphics2D上下文中为BuffereImage设置合成属性:

BufferedImage buffImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D gbi = buffImg.createGraphics();
...
gbi.setComposite(ac);

Previous page: Clipping the Drawing Region
Next page: Controlling Rendering Quality