John zukoski (jaz@zukowski.net ),
President, JZ ventures, Inc.
Summary:Two-dimen1_graphics programming with the Java language just got a little better.AlphaComposite
Class, which previusly supported only eight of the 12 Porter-Duff rules for digital
Image compositing, now supports all 12. In this installmentMagic with Merlin, John zukoski describes all 12 rules and offers an interactive program to demonstrate how they operate.
Back in 1984, Thomas Porter and Tom Duff wrote a paper entitled "compositing digital images" that described 12 Rules combining two images. Support for theseCompositingRules is found inAlphaComposite
Class,
First introduced in version 1.2 of the Java language. Version 1.4, currently in Beta 2, supports all 12 rules.
Support for compositing is necessary for applications like games that include multiple image types, including background images, player-character images, and the like. while it is easy to always draw the player in front of the background, if the player were
To jump behind a tree, you wowould want to show the character image faded out somewhat behind the tree image. This is where compositing comes in handy.
Porter and Duff's 12 Rules
TheAlphaComposite
Class has 12 constants, one for each rule. it can be tricky to visualize how the rule is applied, so I 've provided an overview of each rule in action. the actual combinations will vary if
The images aren't opaque. The demonstration program, shown in Listing 1 and available for download in the resources section,
Allows you to try out different levels of transparency.
Note: those rules newly added with the Merlin release are indicated with an asterisk (*).
Rule 1.AlphaComposite.CLEAR
: Nothing will be drawn in the combined image.
Alphacomposite. Clear
Rule 2.AlphaComposite.SRC
: SRC stands for source; only the source image will be in the combined image.
Alphacomposite. SRC
Rule 3.AlphaComposite.DST
*: DST stands for destination; only the destination image will be in the combined image.
Alphacomposite. dst
Rule 4.AlphaComposite.SRC_OVER
: The Source image will be drawn over the destination image.
Alphacomposite. src_over
Rule 5.AlphaComposite.DST_OVER
: The destination image will be drawn over the source image.
Alphacomposite. dst_over
Rule 6.AlphaComposite.SRC_IN
: Only the part of the source image that overlaps the destination image will be drawn.
Alphacomposite. src_in
Rule 7.AlphaComposite.DST_IN
: Only the part of the destination image that overlaps the source image will be drawn.
Alphacomposite. dst_in
Rule 8.AlphaComposite.SRC_OUT
: Only the part of the source image that doesn't overlap the destination image will be drawn.
Alphacomposite. src_out
Rule 9.AlphaComposite.DST_OUT
: Only the part of the destination image that doesn't overlap the source image will be drawn.
Alphacomposite. dst_out
Rule 10.AlphaComposite.SRC_ATOP
*: The part of the source image that overlaps the destination image will be drawn with the part of the destination image that doesn't overlap
Source image.
Alphacomposite. src_atop
Rule 11.AlphaComposite.DST_ATOP
*: The part of the destination image that overlaps the source image will be drawn with the part of the source image that doesn't overlap the destination
Image.
Alphacomposite. dst_atop
Rule 12.AlphaComposite.XOR
*: The part of the source image that doesn't overlap the destination image will be drawn with the part of the destination image that doesn't overlap
The source image.
Alphacomposite. XOR
Back to Top
A complete example
The following Program is an interactive demonstration of the Alpha compositing rules. Just alter the opacity for each triangle and select a rule to use to see the effect of blending the images.
import java.awt.*;import java.awt.event.*;import java.awt.geom.*;import java.awt.image.*;import java.lang.reflect.*;import javax.swing.*;import javax.swing.event.*;import java.util.*;public class CompositeIt extends JFrame { JSlider sourcePercentage = new JSlider(); JSlider destinationPercentage = new JSlider(); JComboBox alphaComposites = new JComboBox(); DrawingPanel drawingPanel = new DrawingPanel(); public CompositeIt() { super("Porter-Duff"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel contentPane = (JPanel) this.getContentPane(); Dictionary labels = new Hashtable(); labels.put(new Integer(0), new JLabel("0.0")); labels.put(new Integer(25), new JLabel("0.25")); labels.put(new Integer(33), new JLabel("0.33")); labels.put(new Integer(50), new JLabel("0.50")); labels.put(new Integer(67), new JLabel("0.67")); labels.put(new Integer(75), new JLabel("0.75")); labels.put(new Integer(100), new JLabel("1.00")); sourcePercentage.setOrientation(JSlider.VERTICAL); sourcePercentage.setLabelTable(labels); sourcePercentage.setPaintTicks(true); sourcePercentage.setPaintLabels(true); sourcePercentage.setValue(100); sourcePercentage.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { int sourceValue = sourcePercentage.getValue(); drawingPanel.setSourcePercentage(sourceValue/100.0f); } }); destinationPercentage.setOrientation(JSlider.VERTICAL); destinationPercentage.setLabelTable(labels); destinationPercentage.setPaintTicks(true); destinationPercentage.setPaintLabels(true); destinationPercentage.setValue(100); destinationPercentage.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { int destinationValue = destinationPercentage.getValue(); drawingPanel.setDestinationPercentage(destinationValue/100.0f); } }); String rules[] = { "CLEAR", "DST", "DST_ATOP", "DST_IN", "DST_OUT", "DST_OVER", "SRC", "SRC_ATOP", "SRC_IN", "SRC_OUT", "SRC_OVER", "XOR"}; ComboBoxModel model = new DefaultComboBoxModel(rules); alphaComposites.setModel(model); alphaComposites.setSelectedItem("XOR"); alphaComposites.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String alphaValue = alphaComposites.getSelectedItem().toString(); Class alphaClass = AlphaComposite.class; try { Field field = alphaClass.getDeclaredField(alphaValue); int rule = ((Integer)field.get(AlphaComposite.Clear)).intValue(); drawingPanel.setCompositeRule(rule); } catch (Exception exception) { System.err.println("Unable to find field"); } } }); contentPane.add(sourcePercentage, BorderLayout.WEST); contentPane.add(destinationPercentage, BorderLayout.EAST); contentPane.add(alphaComposites, BorderLayout.SOUTH); contentPane.add(drawingPanel, BorderLayout.CENTER); pack(); } public static void main(String args[]) { new CompositeIt().show(); } class DrawingPanel extends JPanel { GeneralPath sourcePath, destPath; BufferedImage source, dest; float sourcePercentage = 1, destinationPercentage = 1; int compositeRule = AlphaComposite.XOR; Dimension dimension = new Dimension(200, 200); public DrawingPanel() { sourcePath = new GeneralPath(); sourcePath.moveTo(0, 0); sourcePath.lineTo(150, 0); sourcePath.lineTo(0, 200); sourcePath.closePath(); source = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB); destPath = new GeneralPath(); destPath.moveTo(200, 0); destPath.lineTo(50, 0); destPath.lineTo(200, 200); destPath.closePath(); dest = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB); } public void setSourcePercentage(float value) { sourcePercentage = value; repaint(); } public void setDestinationPercentage(float value) { destinationPercentage = value; repaint(); } public void setCompositeRule(int value) { compositeRule = value; repaint(); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D)g; Graphics2D sourceG = source.createGraphics(); Graphics2D destG = dest.createGraphics(); destG.setComposite(AlphaComposite.Clear); destG.fillRect(0, 0, 200, 200); destG.setComposite(AlphaComposite.getInstance( AlphaComposite.XOR, destinationPercentage)); destG.setPaint(Color.magenta); destG.fill(destPath); sourceG.setComposite(AlphaComposite.Clear); sourceG.fillRect(0, 0, 200, 200); sourceG.setComposite(AlphaComposite.getInstance( AlphaComposite.XOR, sourcePercentage)); sourceG.setPaint(Color.green); sourceG.fill(sourcePath); destG.setComposite(AlphaComposite.getInstance(compositeRule)); destG.drawImage(source, 0, 0, null); g2d.drawImage(dest, 0, 0, this); } public Dimension getPreferredSize() { return dimension; } }} |