我将对java 2D进行一些讲述,当然是原创,不过也是参考自sun公司自己编写的java tutorial。
下面是这个指南的地址,以及一些来自官方的翻译版本

关于Composite的官方版本(英文)
AlphaComposite,是关于两个目标重叠的混合处理类,该类提供了一个getInstance的方法,其中的两个参数为rule和alpha,第二个参数将由调用者设置一个alpha值,即是透明度的设置,而第一个参数则是混合方式,也可以翻译为一种规则,并分为以下几种方式

SRC
SRC_IN
SRC_OUT
SRC_OVER
按照为都认识,所谓源模式,也就是将源(Source,以下简称src)放在上面,目标放在下面(Destination,以下简称dst),in便是忽略掉src再dst的外面部分,out则是忽略掉里面部分,over则是覆盖在dst的上面
DST_IN
DST_OUT
DST_OVER
所谓目标模式,就是与源模式相反,将源模式放在下面,将目标模式放在上面,in便是忽略掉dst都与源的飞重叠部分,而out便是忽略掉dst的重叠部分,over自然是将dst覆盖在上面
CLEAR
顾名思义,就是清楚掉一部分。。。但是清楚的确实源部分,这个需要记忆一下
下面是sun提供的演示



这个是sun自己写的演示,所以比较具有权威性,我看过代码来,完全使用对象方式绘制,而问题就在于使用Graphics中的非对象方法时不能实现覆盖。
  1package com.ciash.test;
  2
  3import java.awt.AlphaComposite;
  4import java.awt.Color;
  5import java.awt.Dimension;
  6import java.awt.Font;
  7import java.awt.Graphics;
  8import java.awt.Graphics2D;
  9import java.awt.GridBagConstraints;
 10import java.awt.GridBagLayout;
 11import java.awt.event.ItemEvent;
 12import java.awt.event.ItemListener;
 13import java.awt.event.WindowAdapter;
 14import java.awt.event.WindowEvent;
 15import java.awt.geom.Ellipse2D;
 16import java.awt.geom.Rectangle2D;
 17import java.awt.image.BufferedImage;
 18
 19import javax.swing.JApplet;
 20import javax.swing.JComboBox;
 21import javax.swing.JFrame;
 22import javax.swing.JLabel;
 23import javax.swing.JPanel;
 24
 25
 26public class Composite extends JApplet implements ItemListener {
 27    /**
 28     * 字段描述:
 29     */

 30    private static final long serialVersionUID = 1741029974281299482L;
 31    CompPanel comp;
 32    JLabel alphaLabel, rulesLabel;
 33    JComboBox alphas, rules;
 34    String alpha = "1.0";
 35    int rule = 0;
 36
 37    // Initializes the layout of the components.
 38    public void init() {
 39        GridBagLayout layOut = new GridBagLayout();
 40        getContentPane().setLayout(layOut);
 41
 42        GridBagConstraints l = new GridBagConstraints();
 43        l.weightx = 1.0;
 44        l.fill = GridBagConstraints.BOTH;
 45        l.gridwidth = GridBagConstraints.RELATIVE;
 46        alphaLabel = new JLabel();
 47        alphaLabel.setText("Alphas");
 48        Font newFont = getFont().deriveFont(1);
 49        alphaLabel.setFont(newFont);
 50        alphaLabel.setHorizontalAlignment(JLabel.CENTER);
 51        layOut.setConstraints(alphaLabel, l);
 52        getContentPane().add(alphaLabel);   
 53        GridBagConstraints c = new GridBagConstraints();
 54        getContentPane().setLayout(layOut);
 55
 56        l.gridwidth = GridBagConstraints.REMAINDER;
 57        rulesLabel = new JLabel();
 58        rulesLabel.setText("Rules");
 59        newFont = getFont().deriveFont(1);
 60        rulesLabel.setFont(newFont);
 61        rulesLabel.setHorizontalAlignment(JLabel.CENTER);
 62        layOut.setConstraints(rulesLabel, l);
 63        getContentPane().add(rulesLabel);   
 64
 65        GridBagConstraints a = new GridBagConstraints();
 66        a.gridwidth = GridBagConstraints.RELATIVE;
 67        a.weightx = 1.0;
 68        a.fill = GridBagConstraints.BOTH;
 69        alphas = new JComboBox();
 70        layOut.setConstraints(alphas, a);
 71        alphas.addItem("1.0");
 72        alphas.addItem("0.75");
 73        alphas.addItem("0.50");
 74        alphas.addItem("0.25");
 75        alphas.addItem("0.0");
 76        alphas.addItemListener(this);
 77        getContentPane().add(alphas);
 78
 79        a.gridwidth = GridBagConstraints.REMAINDER;
 80        rules = new JComboBox();
 81        layOut.setConstraints(rules, a);
 82        rules.addItem("SRC");
 83        rules.addItem("DST_IN");
 84        rules.addItem("DST_OUT");
 85        rules.addItem("DST_OVER");
 86        rules.addItem("SRC_IN");
 87        rules.addItem("SRC_OVER");
 88        rules.addItem("SRC_OUT");
 89        rules.addItem("CLEAR");
 90        rules.addItemListener(this);
 91        getContentPane().add(rules);
 92
 93        GridBagConstraints fC = new GridBagConstraints(); 
 94        fC.fill = GridBagConstraints.BOTH;
 95        fC.weightx = 1.0;
 96        fC.weighty = 1.0;
 97        fC.gridwidth = GridBagConstraints.REMAINDER;
 98        comp = new CompPanel();
 99        layOut.setConstraints(comp, fC);
100        getContentPane().add(comp); 
101
102        validate();
103    }

104
105/*
106 * Detects a change in either of the Choice components.  Resets the variable corresponding
107 * to the Choice whose state is changed.  Invokes changeRule in CompPanel with the current
108 * alpha and composite rules.
109*/

110    public void itemStateChanged(ItemEvent e){
111        if ( e.getStateChange() != ItemEvent.SELECTED ) {
112            return;
113        }

114
115        Object choice = e.getSource();
116        if ( choice == alphas ) {
117            alpha = (String)alphas.getSelectedItem();
118        }
 else {
119            rule = rules.getSelectedIndex();
120        }

121        comp.changeRule(alpha, rule);
122    }

123
124    public static void main(String s[]) {
125        JFrame f = new JFrame("Composite");
126        f.addWindowListener(new WindowAdapter() {
127            public void windowClosing(WindowEvent e) {System.exit(0);}
128        }
);
129        JApplet applet = new Composite();
130        f.getContentPane().add("Center", applet);
131        applet.init();
132        f.pack();
133        f.setSize(new Dimension(300,300));
134        f.setVisible(true);
135    }

136}

137
138
139class CompPanel extends JPanel {
140
141    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC);  
142    float alpha = 1.0f;
143
144    public CompPanel(){}
145
146// Resets the alpha and composite rules with selected items.    
147    public void changeRule(String a, int rule) {
148        alpha = Float.valueOf(a).floatValue();
149        ac = AlphaComposite.getInstance(getRule(rule), alpha);
150        repaint();
151    }

152
153// Gets the requested compositing rule.
154    public int getRule(int rule){
155        int alphaComp = 0;
156        switch ( rule ) {
157        case 0: alphaComp = AlphaComposite.SRC; break;
158        case 1: alphaComp = AlphaComposite.DST_IN; break;
159        case 2: alphaComp = AlphaComposite.DST_OUT; break;
160        case 3: alphaComp = AlphaComposite.DST_OVER; break;
161        case 4: alphaComp = AlphaComposite.SRC_IN; break;
162        case 5: alphaComp = AlphaComposite.SRC_OVER; break;
163        case 6: alphaComp = AlphaComposite.SRC_OUT; break;
164        case 7: alphaComp = AlphaComposite.CLEAR; break;
165        }

166        return alphaComp;
167    }

168
169    public void paintComponent(Graphics g) {
170        super.paintComponent( g );
171        Graphics2D g2 = (Graphics2D) g;
172
173        Dimension d = getSize();
174        int w = d.width;
175        int h = d.height; 
176
177        // Creates the buffered image.
178        BufferedImage buffImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
179        Graphics2D gbi = buffImg.createGraphics();
180
181        // Clears the previously drawn image.
182        g2.setColor(Color.white);
183        g2.fillRect(00, d.width, d.height);
184
185        int rectx = w/4;
186        int recty = h/4;
187
188        // Draws the rectangle and ellipse into the buffered image.
189        gbi.setColor(new Color(0.0f0.0f1.0f1.0f));
190        gbi.fill(new Rectangle2D.Double(rectx, recty, 150100));
191        gbi.setColor(new Color(1.0f0.0f0.0f1.0f));    
192        gbi.setComposite(ac);
193        gbi.fill(new Ellipse2D.Double(rectx+rectx/2,recty+recty/2,150,100));
194
195        // Draws the buffered image.
196        g2.drawImage(buffImg, null00);
197    }

198}
   
199
200