The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
The AlphaComposite class encapsulates various compositing styles, which determine how overlapping objects are rendered.AlphaComposite类封装了各种合成样式,这些样式决定了重叠对象的渲染方式。An AlphaComposite还可以具有指定透明度的alpha值:alpha = 1.0表示完全不透明,alpha = 0.0表示完全透明(透明)。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 supports most of the standard Porter-Duff compositing rules shown in the following table.AlphaComposite支持下表所示的大多数标准Porter-Duff合成规则。
Source-over (SRC_OVER)![]() |
|
Source-in (SRC_IN)![]() |
|
Source-out (SRC_OUT)![]() |
|
Destination-over (DST_OVER)![]() |
|
Destination-in (DST_IN)![]() |
|
Destination-out (DST_OUT)![]() |
|
Clear (CLEAR)![]() |
To change the compositing style used by the 要更改Graphics2D class, create an AlphaComposite object and pass it into the setComposite method.Graphics2D类使用的合成样式,请创建AlphaComposite对象并将其传递到setComposite方法中。
This program illustrates the effects of various compositing style and alpha combinations.该程序演示了各种合成样式和alpha组合的效果。
. Composite.javacontains 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, 当选择不同的合成规则或alpha值时,将再次调用AlphaComposite.getInstance is called again, and the new AlphaComposite is assigned to ac.AlphaComposite.getInstance,并将新的AlphaComposite分配给ac。The selected alpha is applied in addition to the per-pixel alpha value and is passed as a second parameter to 除了每像素alpha值之外,还将应用选定的alpha,并将其作为第二个参数传递给AlphaComposite.getInstance.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);