import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.lang.reflect.Field;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.event.ChangeListener;

/**
 * @author Remi Forax
 *
 */
public class AlphaCompositionExample extends JComponent {

  protected void paintComponent(Graphics graphics) {
    super.paintComponent(graphics);

    Graphics2D g= (Graphics2D)graphics;

    g.clearRect(0, 0, getWidth(), getHeight());

    g.drawImage(image,0,0,null);
  }
  
  public void setComposite(Composite composite) {
    Graphics2D g2=(Graphics2D)image.getGraphics();
    
    g2.setColor(Color.CYAN);
    g2.fillRect(0, 0, image.getWidth(), image.getHeight());
    
    g2.setColor(Color.ORANGE);
    g2.fill(ellipse);
    
    if (composite!=null)
      g2.setComposite(composite);
      
    g2.setColor(Color.RED);
    g2.fill(rectangle);
    
    g2.dispose();
    
    paintImmediately(0,0,getWidth(),getHeight());
  }
  
  private final BufferedImage image=
    new BufferedImage(200,200,BufferedImage.TYPE_INT_ARGB);
  
  private static final Shape ellipse=new Ellipse2D.Float(50,50,100,100);
  private static final Shape rectangle=new Rectangle(100,100,100,100);
  
  static AlphaComposite getFieldValue(String name) {
    try {
      Field field=AlphaComposite.class.getField(name);
      return (AlphaComposite)field.get(null);
    } catch (NoSuchFieldException e) {
      throw new AssertionError(e);
    } catch (IllegalArgumentException e) {
      throw new AssertionError(e);
    } catch (IllegalAccessException e) {
      throw new AssertionError(e);
    }
  }

  void applyComposite(JCheckBox check,JSlider slider,JComboBox combo) {
    String rule=(String)combo.getSelectedItem();
    AlphaComposite composite=getFieldValue(rule);
    if (check.isSelected()) {
      float alpha=slider.getValue()/100.0f;
      composite=AlphaComposite.getInstance(composite.getRule(), alpha);
    }
          
    setComposite(composite);
  }

  public static void main(String[] args) {

    JFrame frame= new JFrame("CompositeExample");
    final AlphaCompositionExample component= new AlphaCompositionExample();
    
     String[] rules=new String[]{
      "Clear", "Dst",     "DstAtop", "DstIn", 
      "DstOut","DstOver", "Src",     "SrcAtop",
      "SrcIn", "SrcOut",  "SrcOver", "Xor"
     }; 
    
    final JSlider slider=new JSlider(0,100);
    
    final JCheckBox checkBox=new JCheckBox("alpha");
    checkBox.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        slider.setEnabled(checkBox.isSelected());
      }
    });
    
    final JComboBox comboBox=new JComboBox(rules);
    
    ActionListener listener=new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        component.applyComposite(checkBox, slider, comboBox);
      }
    };
    
    checkBox.addActionListener(listener);
    comboBox.addActionListener(listener);
    slider.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent e) {
        component.applyComposite(checkBox, slider, comboBox);
      }
    });
    
    JPanel panel=new JPanel();
    panel.add(checkBox);
    panel.add(slider);
    panel.add(comboBox);
    
    frame.getContentPane().add(panel,BorderLayout.NORTH);
    frame.getContentPane().add(component);
    frame.setSize(400,300);
    frame.show();
  }
}
