The Java Program: Composite.java
1 import java.awt.*;
2 import java.awt.image.BufferedImage;
3 import java.awt.RenderingHints;
4 import java.awt.event.*;
5 import java.awt.geom.*;
6
7 import javax.swing.*;
8
9 public class Composite extends JPanel implements ItemListener {
10
11 private static final JFrame f = new JFrame ("A Pear from shapes");
12
13 public static void main (String[] args) {
14 final class WindowClosingListener extends WindowAdapter {
15 public void windowClosing (WindowEvent evt) { close(); }
16 }
17 f.addWindowListener (new WindowClosingListener ());
18 f.getContentPane().add(new Composite());
19 f.setSize(320, 240);
20 f.setVisible(true);
21 }
22
23 private static void close () {
24 f.setVisible (false);
25 f.dispose();
26 System.exit(0);
27 }
28
29 final JPanel controls = new JPanel ();
30 JLabel alphaLabel, rulesLabel;
31 JComboBox alphas, rules;
32 int rule = 0;
33
34 public Composite() {
35 setLayout (new BorderLayout());
36 add (controls, BorderLayout.NORTH);
37
38 final GridBagLayout layOut = new GridBagLayout();
39 controls.setLayout(layOut);
40
41 final GridBagConstraints l = new GridBagConstraints();
42 l.weightx = 1.0;
43 l.fill = GridBagConstraints.BOTH;
44 l.gridwidth = GridBagConstraints.RELATIVE;
45 alphaLabel = new JLabel();
46 alphaLabel.setText("Alphas");
47 Font newFont = getFont().deriveFont(1);
48 alphaLabel.setFont(newFont);
49 alphaLabel.setHorizontalAlignment(JLabel.CENTER);
50 layOut.setConstraints(alphaLabel, l);
51 controls.add(alphaLabel);
52 GridBagConstraints c = new GridBagConstraints();
53
54 l.gridwidth = GridBagConstraints.REMAINDER;
55 rulesLabel = new JLabel();
56 rulesLabel.setText("Rules");
57 newFont = getFont().deriveFont(1);
58 rulesLabel.setFont(newFont);
59 rulesLabel.setHorizontalAlignment(JLabel.CENTER);
60 layOut.setConstraints(rulesLabel, l);
61 controls.add(rulesLabel);
62
63 GridBagConstraints a = new GridBagConstraints();
64 a.gridwidth = GridBagConstraints.RELATIVE;
65 a.weightx = 1.0;
66 a.fill = GridBagConstraints.BOTH;
67 alphas = new JComboBox();
68 layOut.setConstraints(alphas, a);
69 alphas.addItem("1.0");
70 alphas.addItem("0.75");
71 alphas.addItem("0.50");
72 alphas.addItem("0.25");
73 alphas.addItem("0.0");
74 alphas.addItemListener(this);
75 controls.add(alphas);
76
77 a.gridwidth = GridBagConstraints.REMAINDER;
78 rules = new JComboBox();
79 layOut.setConstraints(rules, a);
80 rules.addItem("SRC");
81 rules.addItem("DST_IN");
82 rules.addItem("DST_OUT");
83 rules.addItem("DST_OVER");
84 rules.addItem("SRC_IN");
85 rules.addItem("SRC_OVER");
86 rules.addItem("SRC_OUT");
87 rules.addItem("CLEAR");
88 rules.addItemListener(this);
89 controls.add(rules);
90
91 final GridBagConstraints fC = new GridBagConstraints();
92 fC.fill = GridBagConstraints.BOTH;
93 fC.weightx = 1.0;
94 fC.weighty = 1.0;
95 fC.gridwidth = GridBagConstraints.REMAINDER;
96 layOut.setConstraints(controls, fC);
97
98 validate();
99 }
100
101 AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC);
102 float alpha = 1.0f;
103
104 public void itemStateChanged (ItemEvent e){
105 if (e.getStateChange() != ItemEvent.SELECTED) {
106 return;
107 }
108 final Object choice = e.getSource();
109 if (choice == alphas) {
110 final String a = (String)alphas.getSelectedItem();
111 alpha = Float.valueOf(a).floatValue();
112 } else {
113 rule = rules.getSelectedIndex();
114 }
115 changeRule (alpha, rule);
116 }
117
118 public void changeRule (final float a, final int rule) {
119 ac = AlphaComposite.getInstance (getRule(rule), a);
120 repaint();
121 }
122
123 public int getRule (final int rule){
124 int alphaComp=0;
125 switch (rule) {
126 case 0: alphaComp = AlphaComposite.SRC; break;
127 case 1: alphaComp = AlphaComposite.DST_IN; break;
128 case 2: alphaComp = AlphaComposite.DST_OUT; break;
129 case 3: alphaComp = AlphaComposite.DST_OVER; break;
130 case 4: alphaComp = AlphaComposite.SRC_IN; break;
131 case 5: alphaComp = AlphaComposite.SRC_OVER; break;
132 case 6: alphaComp = AlphaComposite.SRC_OUT; break;
133 case 7: alphaComp = AlphaComposite.CLEAR; break;
134 }
135 return alphaComp;
136 }
137
138 public void paintComponent (final Graphics g) {
139 super.paintComponent (g);
140 final Graphics2D g2 = (Graphics2D) g;
141 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
142
143 final Dimension d = getSize();
144 final int w = d.width;
145 final int h = d.height;
146
147 // Creates the buffered image.
148 final BufferedImage buffImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
149 final Graphics2D gbi = buffImg.createGraphics();
150
151 // Clears the previously drawn image.
152 g2.setColor(Color.white);
153 g2.fillRect(0, 0, d.width, d.height);
154
155 final int rectx = w/4;
156 final int recty = h/4;
157
158 // Draws the rectangle and ellipse into the buffered image.
159 gbi.setColor(new Color(0.0f, 0.0f, 1.0f, 1.0f));
160 gbi.fill(new Rectangle2D.Double(rectx, recty, 150, 100));
161 gbi.setColor(new Color(1.0f, 0.0f, 0.0f, 1.0f));
162 gbi.setComposite(ac);
163 gbi.fill(new Ellipse2D.Double(rectx+rectx/2,recty+recty/2,150,100));
164
165 // Draws the buffered image.
166 g2.drawImage(buffImg, null, 0, 0);
167 }
168 }