|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
TheAlphaCompositeclass encapsulates various compositing styles, which determine how overlapping objects are rendered. AnAlphaCompositecan also have an alpha value that specifies the degree of transparency: alpha = 1.0 is totally opaque, alpha = 0.0 totally transparent (clear).AlphaCompositesupports most of the standard Porter-Duff compositing rules shown in the following table.
Source-over ( SRC_OVER)
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)
If pixels in the source and the destination overlap, only the source pixels in the overlapping area are rendered. Source-out ( SRC_OUT)
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)
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)
If pixels in the source and the destination overlap, the alpha from the source is applied to the destination pixels in the overlapping area. 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. Destination-out ( DST_OUT)
If pixels in the source and the destination overlap, the alpha from the source is applied to the destination pixels in the overlapping area. 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. Clear ( CLEAR)
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
Graphics2D, you create anAlphaCompositeobject and pass it into thesetCompositemethod.
This program illustrates the effects of various compositing style and alpha combinations.
This is a picture of the applet's GUI. To run the applet, click the picture. The applet will appear in a new browser window.
Composite.java. contains the full code for this applet.
A new
AlphaCompositeobject ac is constructed by callingAlphaComposite.getInstanceand specifying the desired compositing rule.AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC);When a different compositing rule or alpha value is selected,
AlphaComposite.getInstanceis called again, and the newAlphaCompositeis assigned to ac. The selected alpha is applied in addition to the per-pixel alpha value and is passed as a second parameter toAlphaComposite.getInstance.ac = AlphaComposite.getInstance(getRule(rule), alpha);The composite attribute is modified by passing the
AlphaCompositeobject toGraphics 2DsetComposite. The objects are rendered into aBufferedImageand are later copied to the screen, so the composite attribute is set on theGraphics2Dcontext for theBufferedImage:BufferedImage buffImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); Graphics2D gbi = buffImg.createGraphics(); ... gbi.setComposite(ac);
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.