GridBagConstraints fC = new GridBagConstraints();
fC.fill = GridBagConstraints.BOTH;
fC.weightx = 1.0;
fC.weighty = 1.0;
fC.gridwidth = GridBagConstraints.REMAINDER;
comp = new CompPanel();
layOut.setConstraints(comp, fC);
getContentPane().add(comp);
validate();
}
/*
* Detects a change in either of the Choice components. Resets the variable corresponding
* to the Choice whose state is changed. Invokes changeRule in CompPanel with the current
* alpha and composite rules.
*/
public void itemStateChanged(ItemEvent e){
if ( e.getStateChange() != ItemEvent.SELECTED ) {
return;
}
public static void main(String s[]) {
JFrame f = new JFrame("Composite");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
JApplet applet = new Composite();
f.getContentPane().add("Center", applet);
applet.init();
f.pack();
f.setSize(new Dimension(300,300));
f.show();
}
}
class CompPanel extends JPanel {
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC);
float alpha = 1.0f;
public CompPanel(){}
// Resets the alpha and composite rules with selected items.
public void changeRule(String a, int rule) {
alpha = Float.valueOf(a).floatValue();
ac = AlphaComposite.getInstance(getRule(rule), alpha);
repaint();
}
// Gets the requested compositing rule.
public int getRule(int rule){
int alphaComp = 0;
switch ( rule ) {
case 0: alphaComp = AlphaComposite.SRC; break;
case 1: alphaComp = AlphaComposite.DST_IN; break;
case 2: alphaComp = AlphaComposite.DST_OUT; break;
case 3: alphaComp = AlphaComposite.DST_OVER; break;
case 4: alphaComp = AlphaComposite.SRC_IN; break;
case 5: alphaComp = AlphaComposite.SRC_OVER; break;
case 6: alphaComp = AlphaComposite.SRC_OUT; break;
case 7: alphaComp = AlphaComposite.CLEAR; break;
}
return alphaComp;
}
public void paintComponent(Graphics g) {
super.paintComponent( g );
Graphics2D g2 = (Graphics2D) g;
Dimension d = getSize();
int w = d.width;
int h = d.height;
// Creates the buffered image.
BufferedImage buffImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D gbi = buffImg.createGraphics();