The AlphaComposite class implements basic alpha compositing rules for combining source and destination colors to achieve blending and transparency effects with graphics and images. The specific rules implemented by this class are the basic set of 12 rules described in T. Porter and T. Duff, "Compositing Digital Images", SIGGRAPH 84, 253-259. The rest of this documentation assumes some familiarity with the definitions and concepts outlined in that paper.

This class extends the standard equations defined by Porter and Duff to include one additional factor. An instance of the AlphaComposite class can contain an alpha value that is used to modify the opacity or coverage of every source pixel before it is used in the blending equations.

It is important to note that the equations defined by the Porter and Duff paper are all defined to operate on color components that are premultiplied by their corresponding alpha components. Since the ColorModel and Raster classes allow the storage of pixel data in either premultiplied or non-premultiplied form, all input data must be normalized into premultiplied form before applying the equations and all results might need to be adjusted back to the form required by the destination before the pixel values are stored.

Also note that this class defines only the equations for combining color and alpha values in a purely mathematical sense. The accurate application of its equations depends on the way the data is retrieved from its sources and stored in its destinations. See Implementation Caveats for further information.

The following factors are used in the description of the blending equation in the Porter and Duff paper:

Factor  Definition
Asthe alpha component of the source pixel
Csa color component of the source pixel in premultiplied form
Adthe alpha component of the destination pixel
Cda color component of the destination pixel in premultiplied form
Fsthe fraction of the source pixel that contributes to the output
Fdthe fraction of the destination pixel that contributes to the output
Arthe alpha component of the result
Cra color component of the result in premultiplied form

Using these factors, Porter and Duff define 12 ways of choosing the blending factors Fs and Fd to produce each of 12 desirable visual effects. The equations for determining Fs and Fd are given in the descriptions of the 12 static fields that specify visual effects. For example, the description for SRC_OVER specifies that Fs = 1 and Fd = (1-As). Once a set of equations for determining the blending factors is known they can then be applied to each pixel to produce a result using the following set of equations:

 	Fs = f(Ad)
 	Fd = f(As)
 	Ar = As*Fs + Ad*Fd
 	Cr = Cs*Fs + Cd*Fd

The following factors will be used to discuss our extensions to the blending equation in the Porter and Duff paper:

Factor  Definition
Csr one of the raw color components of the source pixel
Cdr one of the raw color components of the destination pixel
Aac the "extra" alpha component from the AlphaComposite instance
Asr the raw alpha component of the source pixel
Adrthe raw alpha component of the destination pixel
Adf the final alpha component stored in the destination
Cdf the final raw color component stored in the destination

Preparing Inputs

The AlphaComposite class defines an additional alpha value that is applied to the source alpha. This value is applied as if an implicit SRC_IN rule were first applied to the source pixel against a pixel with the indicated alpha by multiplying both the raw source alpha and the raw source colors by the alpha in the AlphaComposite. This leads to the following equation for producing the alpha used in the Porter and Duff blending equation:

 	As = Asr * Aac 
All of the raw source color components need to be multiplied by the alpha in the AlphaComposite instance. Additionally, if the source was not in premultiplied form then the color components also need to be multiplied by the source alpha. Thus, the equation for producing the source color components for the Porter and Duff equation depends on whether the source pixels are premultiplied or not:
 	Cs = Csr * Asr * Aac     (if source is not premultiplied)
 	Cs = Csr * Aac           (if source is premultiplied) 
No adjustment needs to be made to the destination alpha:
 	Ad = Adr 

The destination color components need to be adjusted only if they are not in premultiplied form:

 	Cd = Cdr * Ad    (if destination is not premultiplied) 
 	Cd = Cdr         (if destination is premultiplied) 

Applying the Blending Equation

The adjusted As, Ad, Cs, and Cd are used in the standard Porter and Duff equations to calculate the blending factors Fs and Fd and then the resulting premultiplied components Ar and Cr.

Preparing Results

The results only need to be adjusted if they are to be stored back into a destination buffer that holds data that is not premultiplied, using the following equations:

 	Adf = Ar
 	Cdf = Cr                 (if dest is premultiplied)
 	Cdf = Cr / Ar            (if dest is not premultiplied) 
Note that since the division is undefined if the resulting alpha is zero, the division in that case is omitted to avoid the "divide by zero" and the color components are left as all zeros.

Performance Considerations

For performance reasons, it is preferrable that Raster objects passed to the compose method of a CompositeContext object created by the AlphaComposite class have premultiplied data. If either the source Raster or the destination Raster is not premultiplied, however, appropriate conversions are performed before and after the compositing operation.

Implementation Caveats

@version
10 Feb 1997
AlphaComposite object that implements the opaque CLEAR rule with an alpha of 1.0f.
See Also
Both the color and the alpha of the destination are cleared (Porter-Duff Clear rule). Neither the source nor the destination is used as input.

Fs = 0 and Fd = 0, thus:

 	Ar = 0
 	Cr = 0
AlphaComposite object that implements the opaque DST rule with an alpha of 1.0f.
@since
1.4
See Also
The destination is left untouched (Porter-Duff Destination rule).

Fs = 0 and Fd = 1, thus:

 	Ar = Ad
 	Cr = Cd
@since
1.4
The part of the destination lying inside of the source is composited over the source and replaces the destination (Porter-Duff Destination Atop Source rule).

Fs = (1-Ad) and Fd = As, thus:

 	Ar = As*(1-Ad) + Ad*As = As
 	Cr = Cs*(1-Ad) + Cd*As
@since
1.4
The part of the destination lying inside of the source replaces the destination (Porter-Duff Destination In Source rule).

Fs = 0 and Fd = As, thus:

 	Ar = Ad*As
 	Cr = Cd*As
The part of the destination lying outside of the source replaces the destination (Porter-Duff Destination Held Out By Source rule).

Fs = 0 and Fd = (1-As), thus:

 	Ar = Ad*(1-As)
 	Cr = Cd*(1-As)
The destination is composited over the source and the result replaces the destination (Porter-Duff Destination Over Source rule).

Fs = (1-Ad) and Fd = 1, thus:

 	Ar = As*(1-Ad) + Ad
 	Cr = Cs*(1-Ad) + Cd
AlphaComposite object that implements the opaque DST_ATOP rule with an alpha of 1.0f.
@since
1.4
See Also
AlphaComposite object that implements the opaque DST_IN rule with an alpha of 1.0f.
See Also
AlphaComposite object that implements the opaque DST_OUT rule with an alpha of 1.0f.
See Also
AlphaComposite object that implements the opaque DST_OVER rule with an alpha of 1.0f.
See Also
AlphaComposite object that implements the opaque SRC rule with an alpha of 1.0f.
See Also
The source is copied to the destination (Porter-Duff Source rule). The destination is not used as input.

Fs = 1 and Fd = 0, thus:

 	Ar = As
 	Cr = Cs
The part of the source lying inside of the destination is composited onto the destination (Porter-Duff Source Atop Destination rule).

Fs = Ad and Fd = (1-As), thus:

 	Ar = As*Ad + Ad*(1-As) = Ad
 	Cr = Cs*Ad + Cd*(1-As)
@since
1.4
The part of the source lying inside of the destination replaces the destination (Porter-Duff Source In Destination rule).

Fs = Ad and Fd = 0, thus:

 	Ar = As*Ad
 	Cr = Cs*Ad
The part of the source lying outside of the destination replaces the destination (Porter-Duff Source Held Out By Destination rule).

Fs = (1-Ad) and Fd = 0, thus:

 	Ar = As*(1-Ad)
 	Cr = Cs*(1-Ad)
The source is composited over the destination (Porter-Duff Source Over Destination rule).

Fs = 1 and Fd = (1-As), thus:

 	Ar = As + Ad*(1-As)
 	Cr = Cs + Cd*(1-As)
AlphaComposite object that implements the opaque SRC_ATOP rule with an alpha of 1.0f.
@since
1.4
See Also
AlphaComposite object that implements the opaque SRC_IN rule with an alpha of 1.0f.
See Also
AlphaComposite object that implements the opaque SRC_OUT rule with an alpha of 1.0f.
See Also
AlphaComposite object that implements the opaque SRC_OVER rule with an alpha of 1.0f.
See Also
AlphaComposite object that implements the opaque XOR rule with an alpha of 1.0f.
@since
1.4
See Also
The part of the source that lies outside of the destination is combined with the part of the destination that lies outside of the source (Porter-Duff Source Xor Destination rule).

Fs = (1-Ad) and Fd = (1-As), thus:

 	Ar = As*(1-Ad) + Ad*(1-As)
 	Cr = Cs*(1-Ad) + Cd*(1-As)
@since
1.4
Creates a context containing state that is used to perform the compositing operation. In a multi-threaded environment, several contexts can exist simultaneously for a single Composite object.
Parameters
srcColorModelthe {@link ColorModel} of the source
dstColorModelthe ColorModel of the destination
hintsthe hint that the context object uses to choose between rendering alternatives
Return
the CompositeContext object used to perform the compositing operation.
Determines whether the specified object is equal to this AlphaComposite.

The result is true if and only if the argument is not null and is an AlphaComposite object that has the same compositing rule and alpha value as this object.

Parameters
objthe Object to test for equality
Return
true if obj equals this AlphaComposite; false otherwise.
Returns the alpha value of this AlphaComposite. If this AlphaComposite does not have an alpha value, 1.0 is returned.
Return
the alpha value of this AlphaComposite.
Returns the runtime class of an object. That Class object is the object that is locked by static synchronized methods of the represented class.
Return
The java.lang.Class object that represents the runtime class of the object. The result is of type {@code Class} where X is the erasure of the static type of the expression on which getClass is called.
Creates an AlphaComposite object with the specified rule.
Parameters
rulethe compositing rule
Throws
IllegalArgumentExceptionif rule is not one of the following: {@link #CLEAR}, {@link #SRC}, {@link #DST}, {@link #SRC_OVER}, {@link #DST_OVER}, {@link #SRC_IN}, {@link #DST_IN}, {@link #SRC_OUT}, {@link #DST_OUT}, {@link #SRC_ATOP}, {@link #DST_ATOP}, or {@link #XOR}
Creates an AlphaComposite object with the specified rule and the constant alpha to multiply with the alpha of the source. The source is multiplied with the specified alpha before being composited with the destination.
Parameters
rulethe compositing rule
alphathe constant alpha to be multiplied with the alpha of the source. alpha must be a floating point number in the inclusive range [0.0, 1.0].
Throws
IllegalArgumentExceptionif alpha is less than 0.0 or greater than 1.0, or if rule is not one of the following: {@link #CLEAR}, {@link #SRC}, {@link #DST}, {@link #SRC_OVER}, {@link #DST_OVER}, {@link #SRC_IN}, {@link #DST_IN}, {@link #SRC_OUT}, {@link #DST_OUT}, {@link #SRC_ATOP}, {@link #DST_ATOP}, or {@link #XOR}
Returns the compositing rule of this AlphaComposite.
Return
the compositing rule of this AlphaComposite.
Returns the hashcode for this composite.
Return
a hash code for this composite.
Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. A thread waits on an object's monitor by calling one of the wait methods.

The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object. The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.

This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's monitor in one of three ways:

  • By executing a synchronized instance method of that object.
  • By executing the body of a synchronized statement that synchronizes on the object.
  • For objects of type Class, by executing a synchronized static method of that class.

Only one thread at a time can own an object's monitor.

Throws
IllegalMonitorStateExceptionif the current thread is not the owner of this object's monitor.
Wakes up all threads that are waiting on this object's monitor. A thread waits on an object's monitor by calling one of the wait methods.

The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object. The awakened threads will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened threads enjoy no reliable privilege or disadvantage in being the next thread to lock this object.

This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.

Throws
IllegalMonitorStateExceptionif the current thread is not the owner of this object's monitor.
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

 getClass().getName() + '@' + Integer.toHexString(hashCode())
 
Return
a string representation of the object.
Causes current thread to wait until another thread invokes the method or the method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0).

The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:

     synchronized (obj) {
         while (<condition does not hold>)
             obj.wait();
         ... // Perform action appropriate to condition
     }
 
This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.
Throws
IllegalMonitorStateExceptionif the current thread is not the owner of the object's monitor.
InterruptedExceptionif another thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown.
Causes current thread to wait until either another thread invokes the method or the method for this object, or a specified amount of time has elapsed.

The current thread must own this object's monitor.

This method causes the current thread (call it T) to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. Thread T becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:

  • Some other thread invokes the notify method for this object and thread T happens to be arbitrarily chosen as the thread to be awakened.
  • Some other thread invokes the notifyAll method for this object.
  • Some other thread interrupts thread T.
  • The specified amount of real time has elapsed, more or less. If timeout is zero, however, then real time is not taken into consideration and the thread simply waits until notified.
The thread T is then removed from the wait set for this object and re-enabled for thread scheduling. It then competes in the usual manner with other threads for the right to synchronize on the object; once it has gained control of the object, all its synchronization claims on the object are restored to the status quo ante - that is, to the situation as of the time that the wait method was invoked. Thread T then returns from the invocation of the wait method. Thus, on return from the wait method, the synchronization state of the object and of thread T is exactly as it was when the wait method was invoked.

A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied. In other words, waits should always occur in loops, like this one:

     synchronized (obj) {
         while (<condition does not hold>)
             obj.wait(timeout);
         ... // Perform action appropriate to condition
     }
 
(For more information on this topic, see Section 3.2.3 in Doug Lea's "Concurrent Programming in Java (Second Edition)" (Addison-Wesley, 2000), or Item 50 in Joshua Bloch's "Effective Java Programming Language Guide" (Addison-Wesley, 2001).

If the current thread is interrupted by another thread while it is waiting, then an InterruptedException is thrown. This exception is not thrown until the lock status of this object has been restored as described above.

Note that the wait method, as it places the current thread into the wait set for this object, unlocks only this object; any other objects on which the current thread may be synchronized remain locked while the thread waits.

This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.

Parameters
timeoutthe maximum time to wait in milliseconds.
Throws
IllegalArgumentExceptionif the value of timeout is negative.
IllegalMonitorStateExceptionif the current thread is not the owner of the object's monitor.
InterruptedExceptionif another thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown.
Causes current thread to wait until another thread invokes the method or the method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.

This method is similar to the wait method of one argument, but it allows finer control over the amount of time to wait for a notification before giving up. The amount of real time, measured in nanoseconds, is given by:

 1000000*timeout+nanos

In all other respects, this method does the same thing as the method of one argument. In particular, wait(0, 0) means the same thing as wait(0).

The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until either of the following two conditions has occurred:

  • Another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method.
  • The timeout period, specified by timeout milliseconds plus nanos nanoseconds arguments, has elapsed.

The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:

     synchronized (obj) {
         while (<condition does not hold>)
             obj.wait(timeout, nanos);
         ... // Perform action appropriate to condition
     }
 
This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.
Parameters
timeoutthe maximum time to wait in milliseconds.
nanosadditional time, in nanoseconds range 0-999999.
Throws
IllegalArgumentExceptionif the value of timeout is negative or the value of nanos is not in the range 0-999999.
IllegalMonitorStateExceptionif the current thread is not the owner of this object's monitor.
InterruptedExceptionif another thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown.